07. 其他

目录老牛浏览 43讨论 0发表于 更新于

1. Inertia 服务端渲染

Inertia 在服务端渲染时,需要运行服务端,我们新增 Supervisor 配置:

bash
vim /etc/supervisor/conf.d/demo-inertia.ilaoniu.cn.conf

输入以下内容:

ini
[program:demo-inertia]
process_name=%(program_name)s
command=php /home/wwwroot/demo.ilaoniu.cn/current/artisan inertia:start-ssr
autostart=true
autorestart=true
user=deployer
redirect_stderr=true
stdout_logfile=/home/wwwroot/demo.ilaoniu.cn/current/storage/logs/inertia.log

接下来我们需要让 Supervisor 重新加载配置:

bash
supervisorctl update

现在通过以下命令检查是否正常运行:

bash
supervisorctl status

状态是 RUNNING 说明运行正常。

同时,还需要更新部署脚本:

php
task('artisan:inertia:stop', function () {
    run('cd {{release_path}} && {{bin/php}} artisan inertia:stop-ssr');
});

// 停止 inertia ssr node,supervisor 会自动开启新进程
after('deploy:symlink', 'artisan:inertia:stop');

2. 重置 Opcache

2.1 方案一

Deployer 符号链接 current 指向最新发布的目录。

php
current -> releases/3/
releases/
    1/
    2/
    3/

Opcache 使用 SCRIPT_FILENAME 值作为文件路径来缓存,所以当使用符号链接之类的方式来部署时,由于部署后仍然指向 current,所以不会触发更新。我们修改这个变量指向实际的路径即可重新缓存。

/usr/local/nginx/conf/fastcgi.conf

nginx
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php/php-fpm.sock;
    # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}

$document_root 改为 $realpath_root 即可。

2.2 方案二

调用 opcache_reset() 重置 Opcache。

首先安装扩展包:

bash
composer require appstract/laravel-opcache

添加到部署脚本:

php
task('artisan:opcache:clear', function () {
    run('cd {{release_path}} && {{bin/php}} artisan opcache:clear');
});

task('opcache:clear', function () {
    run('cd {{release_path}} && {{bin/php}} -r \'opcache_reset();\'');
});

// 重置 PHP-FPM Opcache
after('deploy:symlink', 'artisan:opcache:clear');

// 重置 CLI Opcache
after('deploy:symlink', 'opcache:clear');

3. Vite 编译失败

在阿里云 ECS 服务器上,使用 Vite 打包代码时出现内存耗尽问题导致失败,而同样配置的腾讯云服务器没有遇到该问题,经排查是 vm.swappiness 配置导致的,默认值为 60,而阿里云将其值改为了 0。

要查看该值,运行以下命令即可:

bash
cat /proc/sys/vm/swappiness

只需要在 /etc/sysctl.conf 中修改 vm.swappiness 或删除这一行使用默认值即可。

4. 支持 HTTP3

在虚拟主机配置文件 server 块中增加:

nginx
# http3
listen 443 quic reuseport;

# http2 and http1.1
listen 443 ssl;
http2 on;
.
.
.
# used to advertise the availability of HTTP/3
add_header Alt-Svc 'h3=":443"; ma=86400';

# sent when quic was used
add_header QUIC-Status $http3;

目前(2025 年 2 月)测试开启 HTTP3 后,偶尔会出现响应时间过长问题(比如 50s,正常请求的响应时间为 60ms),没找到是哪里的原因,关闭后好像没出现了,暂不推荐开启。

点赞
收藏
暂无讨论,快来发起讨论吧~
私信
老牛@ilaoniu
老牛,俗称哞哞。单纯的九零后理工小青年。喜欢折腾,爱玩,爱音乐,爱游戏,爱电影,爱旅游...
最后活跃于