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:
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.
Once this is done, as the root user, please install Swoole from the command line:
|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:
|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.|
Then, you can modify the server section of your NGINX configuration by adding these locations:
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:
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:
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.