官方文件
Laravel Octane supercharges your application's performance by serving your application using high-powered application servers, including Open Swoole, Swoole, and RoadRunner. Octane boots your application once, keeps it in memory, and then feeds it requests at supersonic speeds.
套件功能
让 PHP 能以多执行绪的方式运行 。安装好后一样能使用 php artisan serve
作为开发时的测试环境,部署时则不用依赖 php-fpm
,透过指令能够运行在指定的 port 上,之后在 nginx 做 proxy_pass 就能上线了。
安装
1. 安装 Swoole
pecl install swoole# 安装选项全选择[no]
2. 添加 Swoole 至 php.ini
透过 php --ini 找到 php.ini 位置,并在 php.ini 最后加上
extension=swoole.so
3. 安装 laravel/octane
composer require laravel/octanephp artisan octane:install# 安装过程选项 => 选择 swoole
开发环境启动
php artisan octane:start --watch
或是不挂载 octane 直接使用原本的
php artisan serve
部署
⚠️ 如果部署到HTTPS环境,记得修改 .env
的 OCTANE_HTTPS
设定,让静态档案也能以 HTTPS 挂载
OCTANE_HTTPS=true
启动
php artisan octane:start --port=<port>
PM2
我会用 PM2 去执行启动指令
# pm2.json example{ "name": "laravel-octane-server", "script": "current/artisan octane:start --port=8090", "max_memory_restart": "4G", "autorestart": true, "log": "./pm2-octane.log"}
执行
pm2 start ./pm2.json# 跑起来后记得下 save,防止机器重开机后消失挂载pm2 save
Nginx Example
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; }}