TrinityTuts

Configure Swoole PHP on your Cloud Server

Configure Swoole PHP on your Cloud Server

Last updated on August 28th, 2020 at 04:17 pm

Swoole is Async programming Framework for PHP which helps you to scale your application and improve its performance. Swoole Enable PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP Framework.

Official Website: https://www.swoole.co.uk/#get-started

Swoole allows a developer to write Async or Sync API to create application.

The feature of Swoole mention in their website

Before start configures or installing Swoole you can also check some benchmark of Swoole which are really interesting, you can check on their official website or in the image given below.

Swoole benchmark

Install Swoole PHP on your Cloud Server

For this tutorial, I am using Digital Ocean Cloud you can create one for you using this link and get free 10$. Now before I start I think you already install your PHP and your server is working. Now follow below simple step to install Swoole on your server

Step 1. We need to install some library before we install Swoole, login to your terminal and run below command

sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
sudo apt install libssl-dev

Step 2. Now we need to install PHP Pear library from where we can install Swoole

sudo apt install php-pear
sudo apt install php7.0-dev

Step 3. Finally, install Swoole and add Swoole extension in PHP.INI file and reload PHP

pecl install swoole

When you run above command you ask for some permission as shown bellow image

Once the installation is complete you need to add Swoole extension.

Add the extension in PHP.INI file

sudo systemctl restart php7.0-fpm

Step 4. Create Folder in /var/www/html or in your server location and add below code and run online if everything work file you can start creating your application with Swoole

<?php
$http = new swoole_http_server("127.0.0.1", 3001);

$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

Note you also need to allow port 3001 to run Swoole server.

That’s it.

If this post helpful then please share this post and like our Facebook page 🙂 .