Laravel Sail with HTTPS Swoole

Kidd Tang
Dev Genius
Published in
5 min readJul 7, 2021

--

Most of the PHP applications are hosted either in LAMP or LEMP stack, Nginx+PHP-FPM combo is fast, easy to configure, and popular nowadays. However, the game-changer for PHP has arisen — Swoole. It brings us Asynchronous Capability, Real Multithreading, and Superb Concurrency Performance for PHP!

Victor Gazotti & Eldad A. Fux tested it with 80% ~ 91% of performance gain! It sounds like PHP on steroids!

📝 If you are a newcomer, feel free to read Kickstart your Laravel Web App using Laravel Sail.

The official Laravel documentation does not provide the method (currently not supported yet) for setting up Swoole in HTTPS mode.

The following steps will guide you in setting up Octane with Swoole Server with HTTPS 🚀🚀🚀!

0. Create Your Laravel Project

Just a quick note for creating a blank project, you may read the tutorial here.

cd ~/codecurl -s https://laravel.build/test-project | bashcd test-projectsail up -d

1. Install Laravel Octane

Install the Laravel Octane with

sail composer require laravel/octane

and then

sail artisan octane:install
Select 1 for swoole

Use code . command open the VS Code.

1.1 Octane Config File

✏️ Append/Edit in config/octane.php file

Setting up the necessary options for HTTPS protocols, the SSL cert and key will be generated by the commands in the Dockerfile. You can turn on/off the HTTPS SSL functionality by changing the value of enable-openssl then restarting the containers.

2. Setting ENV file

✏️ Append in .env file

Use Swoole as the Octane server. The flag OCTANE_HTTPS is not turning on the SSL functionality, it is just about the URL prefix, you can read this.

3. Keep Swoole Up and Running

I am going to use Laravel Octane to replace the PHP built-in HTTP server, we need to always keep Octane up and running.

I need to customize our Sail by publishing the config files:

sail artisan sail:publish

So, ✏️update docker/8.0/supervisord.conf (Line 8) as stated in official documentation.

Octane has to restart in order to apply your file changes, so I also turn on the --watch flag in the command above. However, make sure you install the Chokidar file-watching library too, you can read more about in official docs.

sail npm install --save-dev chokidar

4. Docker Config

A. ✏️ Append in docker-compose.yml file, ports section (~Line 13)

I need to map the port 8000 to public

In order to make every configuration above function properly, I need to modify the docker/8.0/Dockerfile.

B. ✏️Append lines for SSL self-signed Cert with proper folder permission

You have to update the latest sail version (> v1.8.4) which include the package
php8.0-swoole from apt without pecl

After sail user creation (~Line 47) —
RUN useradd -ms /bin/bash — no-user-group -g $WWWGROUP -u 1337 sail

add these lines…

The resulting Dockerfile :

5. Re-build Container

Shutdown your container with sail down then re-build with

sail build --no-cache

6. Testing

Your Swoole should be up and running, you may check with

sail artisan octane:status

and also open the browser and enter https://localhost:8000, you should open the Laravel Welcome page with HTTPS.

I also run the test using ab, to find out how much performance gain…

ab -n 10000 -c 512 -H "Accept-Encoding: gzip, deflate" https://localhost:8000/

Result:

Server Software:        swoole-http-server
Server Hostname: localhost
Server Port: 8000
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
Server Temp Key: X25519 253 bits
TLS Server Name: localhost
Document Path: /
Document Length: 17503 bytes
Concurrency Level: 512
Time taken for tests: 97.184 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 185930000 bytes
HTML transferred: 175030000 bytes
Requests per second: 102.90 [#/sec] (mean)
Time per request: 4975.846 [ms] (mean)
Time per request: 9.718 [ms] (mean, across all concurrent requests)
Transfer rate: 1868.33 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 30 96.9 3 633
Processing: 56 4794 2417.4 4598 19666
Waiting: 17 4791 2418.0 4586 19665
Total: 60 4824 2403.2 4613 19668
Percentage of the requests served within a certain time (ms)
50% 4613
66% 5592
75% 6085
80% 6398
90% 7509
95% 8599
98% 10384
99% 12929
100% 19668 (longest request)

As the baseline: the default PHP built-in server

ab -n 10000 -c 512 -H "Accept-Encoding: gzip, deflate" http://localhost/

Result:

Server Software:
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 17490 bytes
Concurrency Level: 512
Time taken for tests: 198.809 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 186310000 bytes
HTML transferred: 174900000 bytes
Requests per second: 50.30 [#/sec] (mean)
Time per request: 10179.014 [ms] (mean)
Time per request: 19.881 [ms] (mean, across all concurrent requests)
Transfer rate: 915.17 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.4 0 9
Processing: 26 9913 1863.2 10041 13891
Waiting: 15 9912 1863.2 10041 13890
Total: 28 9913 1862.1 10041 13891
Percentage of the requests served within a certain time (ms)
50% 10041
66% 10549
75% 11006
80% 11085
90% 11833
95% 12221
98% 12640
99% 13391
100% 13891 (longest request)

Conclude the results

You can see the request per second is improved more than 100% 🤩! The total time is taken and time/request is also halved! This Swoole is just taking the default settings, you can fine-tune it base on your requirements to achieve a more desirable result!

Video Demo:

Are you ready to put your sites online with unlimited app hosting for only $14 per month?

Cloudways comes with tons of features that make you easily host your Laravel, WordPress, or any PHP sites without worrying about cyber-securities nightmares.

Now you can extra discount during the BFCM 2023 campaign period!

Link: www.cloudways.com/en/?id=66136

Let’s start trying the Cloudways!

If you like my article, please clap, follow and share my article (or you could ☕ buy me a coffee ☕ ). Take care and see you soon!

--

--