Swoole HTTP Server
High-performance async HTTP server for PHP applications.
Overview
Swoole is a revolutionary PHP extension that transforms traditional synchronous PHP into a high-performance asynchronous runtime, eliminating the need for external web servers like Apache or Nginx. Originally developed by Chinese engineers and now maintained globally, Swoole introduces coroutines, event loops, and built-in server capabilities directly into PHP, enabling developers to build concurrent applications without the complexity of callback-based programming. This async approach allows a single PHP process to handle thousands of concurrent connections efficiently.
The Swoole HTTP server leverages coroutine-based architecture to achieve performance gains of 10-100x over traditional PHP-FPM setups for I/O-intensive workloads. Unlike traditional PHP where each request spawns a separate process, Swoole maintains persistent worker processes that handle multiple requests concurrently using lightweight coroutines. The built-in HTTP server eliminates the overhead of CGI/FastCGI communication layers, while connection pooling for databases and caching systems dramatically reduces connection establishment costs.
This stack is ideal for PHP developers building high-throughput APIs, real-time applications, or microservices who need better performance without abandoning PHP's familiar syntax and ecosystem. Companies migrating from Node.js or Go for performance reasons can leverage Swoole to achieve similar concurrency levels while maintaining their existing PHP codebase and developer expertise.
Key Features
- Coroutine-based concurrency allowing thousands of simultaneous connections without callback complexity
- Built-in HTTP/HTTPS server eliminating need for Apache, Nginx, or PHP-FPM
- Persistent connection pooling for MySQL, Redis, and PostgreSQL reducing connection overhead
- WebSocket and TCP/UDP server capabilities within the same PHP process
- Memory-resident application state between requests enabling advanced caching strategies
- Laravel Octane and Hyperf framework integration for enterprise applications
- Built-in process management with worker process recycling and memory limit controls
- Hook-based PHP function replacement making file I/O and network calls non-blocking
Common Use Cases
- 1High-frequency trading platforms requiring microsecond response times for financial data APIs
- 2Gaming backends handling real-time multiplayer interactions and leaderboard updates
- 3IoT data collection services processing thousands of sensor readings per second
- 4Chat applications combining HTTP APIs with WebSocket connections in single deployment
- 5E-commerce platforms during flash sales requiring rapid inventory updates across concurrent users
- 6Microservices architectures where PHP teams need Node.js-level performance without language migration
- 7Development environments testing async PHP code before production deployment
Prerequisites
- Minimum 512MB RAM allocation as Swoole maintains persistent worker processes
- PHP 7.2+ knowledge with understanding of namespace and class-based architecture
- Port 9501 available for HTTP server or ability to modify port mapping
- Basic understanding of asynchronous programming concepts and coroutines
- Familiarity with PHP composer for managing Swoole-compatible packages
- Application code designed for stateful operation as variables persist between requests
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 swoole: 3 image: phpswoole/swoole:latest4 container_name: swoole5 restart: unless-stopped6 volumes: 7 - ./app:/var/www/html8 ports: 9 - "9501:9501"10 networks: 11 - swoole-network1213networks: 14 swoole-network: 15 driver: bridge.env Template
.env
1# Swoole HTTP serverUsage Notes
- 1Docs: https://wiki.swoole.com/
- 2Coroutine-based PHP - async without callback hell
- 310-100x faster than php-fpm for many workloads
- 4Built-in HTTP server, WebSocket, TCP, UDP support
- 5Connection pooling for MySQL, Redis, PostgreSQL
- 6Compatible with Laravel (Octane), Hyperf, and other frameworks
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 swoole:5 image: phpswoole/swoole:latest6 container_name: swoole7 restart: unless-stopped8 volumes:9 - ./app:/var/www/html10 ports:11 - "9501:9501"12 networks:13 - swoole-network1415networks:16 swoole-network:17 driver: bridge18EOF1920# 2. Create the .env file21cat > .env << 'EOF'22# Swoole HTTP server23EOF2425# 3. Start the services26docker compose up -d2728# 4. View logs29docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/swoole/run | bashTroubleshooting
- Fatal error: Class 'Swoole\Http\Server' not found: Ensure phpswoole/swoole image is used instead of standard PHP images
- Connection refused on port 9501: Verify Swoole server is binding to 0.0.0.0 instead of localhost in server configuration
- Memory leaks causing worker restarts: Implement proper variable cleanup and avoid circular references in request handlers
- Database connection errors after idle time: Configure connection pool with proper heartbeat settings to maintain persistent connections
- Session data not persisting between requests: Replace PHP's default session handling with Swoole Table or Redis-based storage
- Composer autoloader conflicts: Use Swoole-compatible autoloaders or manually require files before server startup
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Components
swoole
Tags
#swoole#php#async#high-performance#coroutines
Category
Web Servers & Reverse ProxiesAd Space
Shortcuts: C CopyF FavoriteD Download