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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay