How to Enable the Swoole Module for PHP in ServBay
ServBay, as a powerful local web development environment, comes integrated with a variety of essential tools and extensions. Among them is the high-performance Swoole PHP extension. Swoole is an asynchronous, parallel, high-performance network engine designed specifically for PHP, and it can significantly enhance the performance and scalability of PHP applications. This guide will walk you through enabling the Swoole extension in ServBay, empowering you to build more efficient PHP apps and services with its rich features.
About the Swoole Framework
Swoole is an open-source project that brings advanced features like Node.js-style asynchronous I/O, coroutines, and multi-process support to PHP. With Swoole, PHP developers can break free from the traditional synchronous, blocking model of typical web servers (such as Apache/Nginx + PHP-FPM) and build high-performance, resident-memory services such as:
- High-performance API gateways
- Real-time communication apps (WebSocket)
- Game servers
- Internet of Things (IoT) applications
- Background services and task processing
Key Features of Swoole
- High Performance: Utilizes system calls like epoll/kqueue for asynchronous, non-blocking I/O, combined with multi-threading, multi-processing, and coroutines for high throughput and low latency.
- Asynchronous & Coroutines: Provides coroutine capabilities, allowing asynchronous code to be written in a synchronous style, greatly improving development efficiency.
- Rich Components: Built-in HTTP, WebSocket, TCP, and UDP servers and clients, as well as features like connection pools, timers, and process management.
- Easy Integration: Swoole can be used alongside existing PHP frameworks (like Laravel, Symfony, ThinkPHP, etc.) or serve as the foundation for new, Swoole-native applications.
Swoole Module Support in ServBay
ServBay ships with the Swoole extension pre-installed for various PHP versions. There’s no need to separately download or compile it. ServBay manages compatibility between PHP versions and the appropriate Swoole extensions.
Currently, ServBay supports enabling the Swoole extension for the following PHP versions:
- PHP 5.6
- PHP 7.x series
- PHP 8.x series (including PHP 8.5)
ServBay will automatically match your chosen PHP version with the appropriate Swoole extension files.
Prerequisites
Before enabling the Swoole module, ensure:
- You have successfully installed and are running ServBay.
- You have installed and selected the desired PHP version in ServBay where you wish to use Swoole.
Steps to Enable the Swoole Module
ServBay offers two ways to enable the Swoole module: via the graphical interface (GUI) or by manually editing configuration files. It’s recommended to use the GUI for a more straightforward experience.
Enabling via ServBay GUI (Recommended)
This is the simplest and fastest method to enable the Swoole module:
- Open the ServBay graphical user interface.
- In the left sidebar, select Languages.
- Click PHP.
- In the PHP versions list on the right, locate the PHP version for which you want to enable Swoole (for example:
PHP 8.3
). Ensure this version is currently selected (the dot is green). - Click the Extensions button on the right of that PHP version.
- In the pop-up extension list, find the swoole extension.
- Toggle the switch on the left of the swoole extension to the ON position (the switch becomes green).
- Click Save or Apply at the bottom of the window.
- ServBay will prompt you to restart the relevant PHP service to load the new extension. Click Restart.
Once the service restarts, the Swoole module will be successfully enabled.
Manually Editing Configuration Files (Advanced)
If you prefer advanced configuration or want to manually manage your config files, you can enable Swoole by editing PHP’s ini
file.
Navigate to the
conf.d
directory for the relevant PHP version. Theconf.d
directory contains additional extension configuration files, and ServBay automatically loads any.ini
files placed here. For PHP 8.3, for example, the path is usually:/Applications/ServBay/etc/php/8.3/conf.d/
1If you chose a different install path during ServBay setup, adjust accordingly.
In this directory, locate and open the
swoole.ini
file.By default, the Swoole line may be commented out (starting with
;
). Find the following:ini[Swoole] ; Uncomment the following line to enable Swoole ;extension = swoole.so
1
2
3Remove the
;
at the start of theextension = swoole.so
line to uncomment it:ini[Swoole] ; Uncomment the following line to enable Swoole extension = swoole.so
1
2
3Save the
swoole.ini
file.In the ServBay GUI main interface or service management panel, find the corresponding PHP service and click the restart button.
After restart, the Swoole module will load successfully according to your configuration.
Verify That the Swoole Module Is Enabled
The most common way is to use the phpinfo()
function to check which modules are loaded in your PHP environment.
- In the root directory of a website managed by ServBay (for example:
/Applications/ServBay/www/servbay.demo/public/
), create a file namedphpinfo.php
. - Edit
phpinfo.php
, adding the following contents:php<?php phpinfo(); ?>
1
2
3 - Ensure your ServBay site
servbay.demo
is set up and pointing to the directory containingphpinfo.php
. - Access the corresponding URL in your web browser, such as
https://servbay.demo/phpinfo.php
. - On the
phpinfo
page that opens, scroll down or use your browser’s search (Ctrl+F or Cmd+F) to look for "swoole".
If Swoole-related information appears—including version number, config options, etc.—then Swoole has been successfully loaded and enabled.
Screenshot: Locating Swoole information on the phpinfo page
Example Usage of Swoole
Once Swoole is enabled, you can start building Swoole applications. Below is a simple example that creates a basic HTTP server using Swoole.
Sample Code: Creating a Simple Swoole HTTP Server
Create a project directory In your development directory (such as creating a new folder under
/Applications/ServBay/www/
), make a new directory calledmy-swoole-app
. Inside it, create apublic
folder for entry files.bashcd /Applications/ServBay/www/ mkdir my-swoole-app cd my-swoole-app mkdir public
1
2
3
4Create the server entry file
public/index.php
php<?php require __DIR__ . '/../vendor/autoload.php'; // Only needed if you installed other dependencies with Composer use Swoole\Http\Server; use Swoole\Http\Request; use Swoole\Http\Response; // Create an HTTP server instance listening on all local IPs (0.0.0.0) at port 9501 $server = new Server("0.0.0.0", 9501); // Set server parameters (optional) // $server->set([ // 'worker_num' => 4, // Number of worker processes // 'daemonize' => false, // Run as a daemon // ]); // Listen for request events $server->on("request", function (Request $request, Response $response) { // Set response header $response->header("Content-Type", "text/plain"); // Set response body $response->end("Hello ServBay with Swoole!"); }); // Listen for server start event (optional) $server->on("start", function (Server $server) { echo "Swoole http server is started at http://0.0.0.0:9501\n"; }); // Start the server $server->start(); echo "Swoole server stopped.\n"; // This line only runs after the server stops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35Note: The line
require __DIR__ . '/../vendor/autoload.php';
is only needed if you've installed other Swoole-related libraries via Composer (likeswoole/ide-helper
or a Swoole framework). If you’re using plain Swoole native code, this can usually be removed.Run the server from the command line Open the ServBay terminal feature, or use your system terminal, and navigate to your project directory (
/Applications/ServBay/www/my-swoole-app/
). Ensure you are using the PHP version managed by ServBay (check withwhich php
or use the ServBay terminal).Run the following command to start the Swoole server:
shphp public/index.php
1If everything works, your terminal will display "Swoole http server is started at http://0.0.0.0:9501" or similar.
Screenshot: Running the Swoole server in the terminal
Test access Once the server is running, open your browser and visit
http://localhost:9501
. You should see the page display: "Hello ServBay with Swoole!"To stop the Swoole server in the terminal, press
Ctrl + C
.
Notes & Considerations
- How it runs: The above example runs the Swoole server directly from the command line, which is suitable for building resident-memory services. Traditional web applications (like Laravel/Symfony/WordPress) typically run in PHP-FPM mode, served through a proxy (like Caddy or Nginx). To run them on Swoole (e.g. Hyperf, Swoole-Laravel), you usually use the framework's own scripts and processes, similar to the native Swoole example above.
- Port usage: The Swoole server listens on a specific port (9501 in the example). Make sure that port isn't already being used by another process.
- Error logs: Swoole errors and output will appear in the terminal window where it runs, or be written to the configured log files.
Frequently Asked Questions (FAQ)
Q: I've enabled Swoole in the ServBay GUI, but I can't see Swoole info on the phpinfo()
page. What should I do?
A: Please check the following:
- Make sure you're viewing the
phpinfo()
page for the correct PHP version. ServBay can run multiple PHP versions at once, so confirm the site or CLI session is using the version where Swoole is enabled. - Ensure you've saved changes and successfully restarted the relevant PHP service in the ServBay GUI.
- If you edited
swoole.ini
manually, check that the file path and name are correct, and it's not still commented out (theextension = swoole.so
line should NOT start with;
).
Q: Why do I get Class 'Swoole\Http\Server' not found
when running the Swoole example?
A: This usually means the Swoole extension wasn't loaded in the current PHP environment. Please review the enablement steps, and make sure the terminal’s PHP (php public/index.php
) is the ServBay-managed PHP version with Swoole enabled. Run php -m | grep swoole
in the terminal to quickly check if Swoole is loaded.
Summary
Enabling the Swoole extension for PHP in ServBay is straightforward and efficient. Whether you use the intuitive GUI or manually edit configuration files, you can quickly add Swoole to your chosen PHP version and kickstart the development of high-performance, asynchronous web applications. With Swoole’s powerful capabilities at your disposal, you can easily build faster, more concurrent PHP services and fully unleash PHP’s potential for modern web development. ServBay’s streamlined environment management lets you focus on writing code, not juggling environment configuration.