iT邦幫忙

0

筆記 - Laravel-Octane 環境架設

  • 分享至 

  • xImage
  •  

官方文件

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/octane

php artisan octane:install
# 安裝過程選項 => 選擇 swoole

開發環境啟動

php artisan octane:start --watch

或是不掛載 octane 直接使用原本的

php artisan serve

部署

⚠️ 如果部署到HTTPS環境,記得修改 .envOCTANE_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;
    }
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言