laravel服务器提速
前提
- 服务器 laravel6 升级到 laravel9
- 只有接口, 改动不会太大
- Octane 启用
- Jenkins 方式部署
- 宝塔管理
1.路由升级
- 修改 RouteServiceProvider
// 旧
protected $namespace = 'App\Http\Controllers';
// 新
protected $namespace = '';
- 修改 routes/api.php
// 旧
Route::get('/link_test/index', 'LinkTestController@index');
Route::get('/link_test/root_list', 'LinkTestController@root_list');
// 新
Route::prefix('link_test')->group(function () {
Route::controller(\App\Http\Controllers\Api\LinkTestController::class)->group(function () {
Route::get('/index', 'index');
Route::get('/root_list', 'root_list');
});
});
// 单个
Route::post('/add/test', [\App\Http\Controllers\Api\AddTestController::class, 'category']);
composer
- 没有提到的点开 packagist输入包查询
# 移除
fideloper/proxy
tymon/jwt-auth
# 修改
fzaninotto/faker 为 fakerphp/faker
# 增加
laravel/octane
laravel/sanctum
Octane服务器
升级宝塔面板
- 安装 php8.1
- 安装扩展
- 安装 supervisor
supervisor配置
# 打开面板添加守护进程
# 命令输入
/www/server/php/81/bin/php -d variables_order=EGPCS /www/wwwroot/learn/blog/artisan octane:start --host="0.0.0.0" --port=8003 --workers=8 --max-requests=100000 --task-workers=4
# 启动测试网站
Nginx 修改重写规则
# 废弃
location /e33 {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
try_files $uri $uri/ @octane;
}
location @octane {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_set_header Host $host;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# IF https
proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:8003$suffix;
}
Jenkins修改
- 在配置最后添加
# supervisor 关闭端口
/www/wwwroot/default/kill_port.sh 8003
# supervisor 重启
/www/server/panel/pyenv/bin/supervisorctl -c /etc/supervisor/supervisord.conf restart learn:learn_00
坑
# 6.0 接口是 get 请求, 下面这个post请求居然能接收到数据
$request->post('type', 1)
# 9.0 升级测试为空白, 修改
$request->input('type', 1)
结语
- 测试由原来的 40ms 变为现在的 20ms 上下
- 网址
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: