Open Swoole TLS and HTTPS

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Generate SSL certificate for your development environment with mkcert

You can use mkcert to generate SSL certificate and setup CA at your local development environment.

mkcert -install

# Created a new local CA 💥
# The local CA is now installed in the system trust store! ⚡️
# The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊

mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1

#Created a new certificate valid for the following names 📜
# - "example.com"
# - "*.example.com"
# - "example.test"
# - "localhost"
# - "127.0.0.1"
# - "::1"

# The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅

Enable HTTPS at Open Swoole Server?

You can enable SSL for HTTP server or HTTP2 server, WebSocket server with the following settings:

<?php
$server->set([
    'ssl_cert_file' => __DIR__ . '/config/example.com+5.pem',
    'ssl_key_file' => __DIR__ . '/config/example.com+5-key.pem'
]);

You an find other SSL configrations at Open Swoole Server Configuration.

You have to pass SWOOLE_SSL when defining a Open Swoole server to enable SSL:

$server = new Swoole\Server("0.0.0.0", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
Last updated on August 31, 2022