How to use Swoole to accelerate Laravel framework

created at 07-12-2021 views: 19

Introduction to swoole

Swoole is a parallel network communication engine based on asynchronous event-driven and coroutine written in C++ language, which provides coroutine and high-performance network programming support for PHP. Provides a variety of communication protocols of web server and client modules, which can easily and quickly implement TCP/UDP services, high-performance Web, WebSocket services, Internet of Things, real-time communications, games, microservices, etc., making PHP no longer limited to traditional Web domain.

install swoole

The installation is very simple, just look at the official operation document of swoole:


the principle of swoole accelerating laravel

First of all, it is clear that swoole can not only accelerate the laravel framework, as long as it is an IOC type framework, swoole can be used to accelerate, such as the current mainstream PHP frameworks: thinkphp, yii, etc.

To understand the principle of swoole acceleration framework, we must first understand the working mode and shortcomings of traditional php-fpm. php-fpm is a process manager that manages php-cgi. Every time a request comes, it needs to load all the running files of PHP once, and then process the business. After the request is over, the memory will be released and the connection pool cannot be used. In this way, once the amount of visits is large, the system resources required are very expensive, and the system may not be able to bear it.

The swoole is a long-resident memory mode, one of the biggest advantages of long-resident memory is that it can speed up performance. To put it simply, it used to be that every request comes in, you need to load the PHP running files, but now you only need to load these files when the first request comes in, and then reside in memory, and then requests that come in can directly reuse the memory. Content in.

Of course, there are advantages and disadvantages. The biggest disadvantage is that it may cause memory overflow problems.

PS: swoole is optimized to accelerate, only the framework itself is accelerated, and it will not have any effect on the operation of business logic, such as: algorithm logic processing, mysql operation, etc.

LaravelS components accelerate laravel

Swoole components can be used in the project to accelerate the corresponding framework, there are many components, here we use LaravelS to accelerate laravel

# composer install component package
composer require hhxsv5/laravel-s
# Automatically publish the corresponding configuration file (available in Laravel 5.5 and above)
php artisan laravels publish
# Modify the configuration in .env (you can also modify it in the configuration file generated under the config folder)
# More configuration items:
# Start LaravelS
php bin/laravels start

Use Nginx reverse proxy to swoole

Because the default listening port of the service opened with LaravelS is 5200, we definitely don't want to add this port after the domain name or IP to access when accessing the project, so we need to use nginx to realize the reverse proxy. The relevant configuration is as follows (you can also do not use this configuration method):


upstream swoole {
   server weight=5 max_fails=3 fail_timeout=30s;
   keepalive 16;

server {

    location / {
        try_files $uri @laravels;

    location @laravels {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header Server-Protocol $server_protocol;
        proxy_set_header Server-Name $server_name;
        proxy_set_header Server-Addr $server_addr;
        proxy_set_header Server-Port $server_port;
        proxy_pass http://swoole;


Use Supervisor to manage LaravelS services

Supervisor is a set of general process management programs developed in Python, which can turn a common command line process into a background daemon, monitor the process status, and automatically restart when it exits abnormally. For use in a production environment, it is recommended to use Supervisor to manage LaravelS services to provide service stability.

To learn more about Supervisor related content, please go to the official website:


# Method 1: Yum source installation
yum install supervisor
# Method 2: Debian/Ubuntu can be installed via apt
apt-get install supervisor
# Add boot-up
systemctl enable supervisord

After installation, its configuration file supervisord.conf will be generated in the /etc directory, and the following configuration will be added to this configuration file:

command=php /www/wwwroot/blog/bin/laravels start -i

Full configuration file:





command=php /www/wwwroot/blog/bin/laravels start -i   
stdout_logfile_backups = 20   


files = supervisord.d/*.ini     

Start supervisord after configuration:

# method one
systemctl start supervisord.service
# Method two
supervisord -c /etc/supervisord.conf

After the startup is successful, test whether the supervisor takes effect:

Stop LaravelS or kill the process directly, and then check whether there is output content in the log file configured before

[root@localhost blog]# php bin/laravels stop
[2021-07-12 09:28:48] [INFO] The max time of waiting to forcibly stop is 60s.
[2021-07-12 09:28:48] [INFO] Waiting Swoole[PID=727] to stop. [1]
[2021-07-12 09:28:49] [INFO] Swoole [PID=727] is stopped.
[root@localhost blog]# cat storage/logs/supervisord-stdout.log 
 _                               _  _____ 
| |                             | |/ ____|
| |     __ _ _ __ __ ___   _____| | (___  
| |    / _` | '__/ _` \ \ / / _ \ |\___ \ 
| |___| (_| | | | (_| |\ V /  __/ |____) |
|______\__,_|_|  \__,_| \_/ \___|_|_____/ 

Speed up your Laravel/Lumen
>>> Components
| Component                 | Version |
| PHP                       | 7.4.21  |
| Swoole                    | 4.6.7   |
| LaravelS                  | 3.7.19  |
| Laravel Framework [local] | 7.30.4  |
>>> Protocols
| Protocol  | Status | Handler           | Listen At           |
| Main HTTP | On     | Laravel Framework | |
>>> Feedback:
[2021-07-12 09:28:49] [TRACE] Swoole is running, press Ctrl+C to quit.
[root@localhost blog]#

If there is an output similar to this, and then visit it in the browser and find that the laravel project can still be opened normally, it proves that the supervisor installation and configuration are successful.

created at:07-12-2021
edited at: 07-12-2021: