Swoole is a PHP extension that is programmed in C/C++ and that makes it possible to accelerate PHP code execution by running different parts of your PHP code in multiple processes, through the use of workers. Most PHP applications that can run on Swoole, can do so anywhere from 3 to 10 times faster! The most impressive benchmarks have come from combining the use of Swoole with the NGINX server as its proxy server. Let's have a closer look at these technologies and how to set them up by installing an event-driven and Swoole-enabled framework like the LightMVC Framework.

Installing the LightMVC Framework Skeleton Application

First, let's install the LightMVC Framework Skeleton Application. To do so, please clone the project using Git, and install the dependencies using Composer. From the command line:

git clone https://github.com/lightmvc/lightmvcskel
cd lightmvcskel
composer install

 

Once the previous step is done:

  • Add your database configuration to config/config.local.php,
  • Load the included test database data/db_schema.sql and data/db_data.sql.

Installing Swoole

Once this is done, as the root user, please install Swoole from the command line:

pecl install swoole

 

Please note: Swoole will ask you if wish to enable sockets support, openssl support, http2 support, mysqlnd support and postgresql coroutine client support. Please enable these options according to what is available on the system where you are installing Swoole and your needs.

 

 

Then, you need to add the Swoole extension to your php.ini file. On Unix/Linux/Mac systems, as the root user, please enter the following command:

echo "extension=swoole.so" >> /etc/php.ini

 

Please note: The php.ini file might be located elsewhere on your system. For example, on Ubuntu 18.04, when running PHP 7.2, you will find this file in '/etc/php/7.2/apache2'. You can discover the location of this file by entering the command 'php --ini' on the command line. It must also be mentioned that some systems have multiple INI files (CLI vs Web). Please modify all those that apply.

 

 

Configuring NGINX

Then, you can modify the server section of your NGINX configuration by adding these locations:

location ~* \.(jpe?g|png|gif|ico|css)$ {
    try_files $uri $uri/;
}

location / {
    set $suffix ?$query_string;

    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;

    # IF https
    # proxy_set_header HTTPS "on";

    proxy_pass http://127.0.0.1:9501$request_uri;
    proxy_cookie_path / /;
}

 

The important line here is line #17. It instructs NGINX to proxy all requests to Swoole when the actual file does not exist in the server's web document root.

Please note: Swoole, by default, listens on the localhost loopback, on port 9501 when using the LightMVC Framework Skeleton Application. This can be modified by changing the 'run-swoole' Composer command within the given composer.json file.

 

 

Here is the entire nginx.conf file:

user apache;
worker_processes 2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid /var/log/nginx/nginx.pid;


events {
    worker_connections 1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen 80 default_server;
        server_name localhost;

        root /srv/lightmvcskel/public;

        location ~* \.(jpe?g|png|gif|ico|css)$ {
            try_files $uri $uri/;
        }

        location / {
            set $suffix ?$query_string;

            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;

            # IF https
            # proxy_set_header HTTPS "on";

            proxy_pass http://127.0.0.1:9501$request_uri;
            proxy_cookie_path / /;
        }
    }
}

 

Running Swoole and NGINX

Once done with configuring the NGINX server, you can now start the Swoole server, and restart (or start) the NGINX server:

/etc/init.d/nginx restart #OR: sudo service nginx restart
COMPOSER_PROCESS_TIMEOUT=0 composer run-swoole

 

The application will be running at http://localhost.

If you check for application performance, you should see that the application's main page now loads within 7ms, instead of 40ms, and that pages that require backend I/O calls now load within 50ms, instead of 150ms.

Enjoy!


Loading Conversation