Laravel9使用Octane加速系统
首先,使用php8.1 centeros7 宝塔最新版的环境,在宝塔安装好swoole。
在终端处,切换到laravel9根目录(app同级),安装Octane,此处可在宝塔面板上的网站处,选择composer最新版,自定义执行命令,使用腾讯源,输入命令
Octane 可以通过 Composer 包管理器安装:
composer require laravel/octane
安装 Octane 后,您可以执行 octane:install
命令,该命令会将 Octane 的配置文件安装到您的应用程序中:
php artisan octane:install
选择1,使用swoole
启用服务
Octane 服务器可以通过 octane:start
Artisan 命令启动。此命令将使用由应用程序的 octane
配置文件的 server
配置选项指定的服务器:
php artisan octane:start
Octane 将在 8000 端口上启动服务器(可配置),因此你可以在 Web 浏览器中通过 http://localhost:8000
访问你的应用程序。
通过 Nginx 为应用提供服务
这是我的配置:先关闭伪静态,然后配置文件输入
map $http_upgrade $connection_upgrade {
default upgrade;
” close;
}
server {
listen 81 ssl http2;
server_name linshi.ggai.top;
server_tokens off;
index index.html index.php index.htm default.php default.htm default.html;
root /www/wwwroot/laravel/public;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 81){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /www/server/panel/vhost/cert/linshi.ggai.top/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/linshi.ggai.top/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security “max-age=31536000”;
error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-81.conf;
#PHP-INFO-END
#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/linshi.ggai.top.conf;
#REWRITE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
#禁止在证书验证目录放入敏感文件
if ( $uri ~ “^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$” ) {
return 403;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log /dev/null;
access_log /dev/null;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log /dev/null;
access_log /dev/null;
}
access_log /www/wwwlogs/linshi.ggai.top.log;
error_log /www/wwwlogs/linshi.ggai.top.error.log;
charset utf-8;
location /index.php {
try_files /not_exists @octane;
}
location / {
try_files $uri $uri/ @octane;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location @octane {
set $suffix “”;
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:8000$suffix;
}
}
提示:如果你还没有准备好管理自己的服务器配置,或者不习惯配置运行健壮的 Laravel Octane 应用所需的所有各种服务,请查看 Laravel Forge。
在生产环境中,你应该在传统 Web 服务器(例如 Nginx 或 Apache)之后为 Octane 应用提供服务。 这样做将允许 Web 服务器为你的静态资源(例如图片和样式表)提供服务,并管理 SSL 证书。
在下面的 Nginx 配置示例文件中,Nginx 将向在端口 8000 上运行的 Octane 服务器提供站点的静态资源和代理请求:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name domain.com;
server_tokens off;
root /home/forge/domain.com/public;
index index.php;
charset utf-8;
location /index.php {
try_files /not_exists @octane;
}
location / {
try_files $uri $uri/ @octane;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location @octane {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:8000$suffix;
}
}
监视文件修改
由于应用在 Octane 服务启动时已经加载到内存中了,因此代码修改后不会起作用。例如,routes/web.php
文件增加路由,在服务重启之前,都不会生效。为了方便,可以使用 --watch
标识来让 Octane 在应用中任何文件修改时都能够自动重启:
php artisan octane:start --watch
使用该功能之前,必须保证本地开发环境安装了 Node 。并且在项目库中安装 Chokidar 文件监视库:
npm install --save-dev chokidar
你可以使用应用程序的 config/octane.php
配置文件中的 watch
配置选项配置应监视的目录和文件。
指定线程数
默认,Octane 会对机器的每个 CPU 核心启动一个应用线程。这些线程将处理进入应用的 HTTP 请求。你可以通过 octane:start
命令的 --workers
参数手动设置线程数:
php artisan octane:start --workers=4
如果是使用 Swoole 服务,可以设置 「任务线程」 数量:
php artisan octane:start --workers=4 --task-workers=6
指定最大请求计数
为了防止内存泄漏,Octane 可在处理了给定数量的请求后优雅地重新启动 worker。要指示 Octane 执行此操作,你可以使用 --max-requests
选项:
php artisan octane:start --max-requests=250
重载 Workers
你可以使用 octane:reload
命令优雅地重新启动 Octane 服务器的应用 workers。通常,这应该在部署后完成,以便将新部署的代码加载到内存中并用于为后续请求提供服务:
php artisan octane:reload
停止服务器
你可以使用 octane:stop
Artisan 命令停止 Octane 服务器:
php artisan octane:stop
检查服务器状态
你可以使用 octane:status
Artisan 命令检查 Octane 服务器的当前状态:
php artisan octane:status