DEV Community

Cover image for Setting Php Swoole on Laravel and nginx as reversed proxy
hardyweb
hardyweb

Posted on • Edited on

Setting Php Swoole on Laravel and nginx as reversed proxy

dua situasi, guna php 7.4 atau php 8.1

Kalau guna php 7.4
kena pakai pakej Laravel Swoole

*INSTALL PAKEJ *

https://github.com/swooletw/laravel-swoole
Enter fullscreen mode Exit fullscreen mode

Kalau guna php 8.1 ke atas
boleh guna pakej Laravel Octane

composer require laravel/octane
php artisan octane:install
Enter fullscreen mode Exit fullscreen mode

Dalam nota ini, aku anggap dalam linux dah ada nginx , kalu tak dok , install guna arahan untuk debian.

apt install nginx 
Enter fullscreen mode Exit fullscreen mode

Kalau guna php 7.4, install laravel-swoole dan kalu nok adjust host dan port boleh publish vendor

php artisan vendor:publish --tag=laravel-swoole
Enter fullscreen mode Exit fullscreen mode

edit file swoole_http.php dalam folder /config

Kalau guna php 8.1 ke atas, boleh guna laravel octane, dia boleh set host dan port direct.

php artisan octane:start  --host=0.0.0.0 --port=80
Enter fullscreen mode Exit fullscreen mode

CONFIGURE NGINX
Buatkan satu vhost untuk reversed proxy

 server {
    listen 80;
    root /path/to/laravel;
    index index.php;

   error_page 404 /index.php

    location / {
         proxy_pass http://127.0.0.1:8000;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection 'upgrade';
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
    }


}
Enter fullscreen mode Exit fullscreen mode

CONFIGURE SUPERVISOR
Supervisor ni digunakan untuk run apa-apa program secara background, jika server restart, ia akan start program tersebut secara automatik.

sudo apt install supervisor
Enter fullscreen mode Exit fullscreen mode
sudo nvim /etc/supervisor/conf.d/php-swoole.conf
Enter fullscreen mode Exit fullscreen mode
[program:laravel-swoole-worker]
process_name=%(program_name)s_%(process_num)02d
command=php path_of_laravel_project/artisan  swoole:http start
autostart=true
autorestart=true
user=namauser 
redirect_stderr=true
stdout_logfile=any_existing_path/laravel-octane-worker.log​
stopwaitsecs=3600

Enter fullscreen mode Exit fullscreen mode
sudo supervisorctl reread
sudo supervisorctl reload
sudo supervisorctl restart-all
Enter fullscreen mode Exit fullscreen mode

Google translate

Top comments (0)