How to Install and Get Started with Swoole for High‑Performance PHP Backend

This article introduces Swoole, a production‑grade asynchronous network engine for PHP, outlines the required prerequisites, shows a quick PECL installation, explains how to enable the extension in php.ini, and provides a complete example of building and testing a simple HTTP server.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
How to Install and Get Started with Swoole for High‑Performance PHP Backend

Overview

Swoole is a production‑grade asynchronous network engine for PHP, written in C (partially C++ since version 4). It provides asynchronous multi‑threaded servers, TCP/UDP clients, MySQL/Redis clients, connection pools, async tasks, message queues, millisecond timers, async file I/O, and async DNS queries.

Key Features

Beyond async I/O, Swoole adds concurrent data structures and IPC mechanisms for PHP multi‑process programming, including atomic counters, concurrent hash tables, channels, locks, and other IPC tools.

Why Use Swoole

Traditional PHP is synchronous and blocking, which limits its ability to handle I/O‑intensive workloads. Swoole enables high‑performance asynchronous TCP, UDP, Unix socket, HTTP, and WebSocket services, extending PHP into high‑concurrency networking.

Documentation

Official documentation: https://wiki.swoole.com

Prerequisites

Linux knowledge of processes and threads

Understanding of process scheduling

Basic IPC concepts (pipes, Unix sockets, message queues, shared memory)

Socket basics (accept/connect, send/recv, listen, bind, blocking/non‑blocking, timeouts)

IO multiplexing (select/poll/epoll, Reactor model, readable/writable events)

TCP/IP fundamentals (TCP, UDP)

Debugging tools (gdb, strace, tcpdump, ps, lsof, top, vmstat, netstat, sar, ss)

Installation Requirements

PHP 7.1 or higher

gcc 4.8 or higher

make

autoconf

Installation via PECL

pecl install swoole

After installation, enable the extension in php.ini (locate the file with php -i | grep php.ini) and add: extension=swoole.so Verify the extension is loaded with php -m.

Test Example

Create swoole_http.php with the following code:

<?php
$server = new swoole_http_server('0.0.0.0', 9505);
$server->on('start', function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9505
";
});
$server->on('request', function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World
");
});
$server->start();
?>

Run the script: php swoole_http.php The server starts on port 9505 and responds with “Hello World” when accessed at http://127.0.0.1:9505.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentAsynchronousPHPHTTP serverTutorialInstallationSwoole
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.