Open Swoole TLS and HTTPS

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

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, HTTP2 or 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 OpenSwoole\Constant::SSL when defining a Open Swoole server to enable SSL:

use OpenSwoole\Server;
use OpenSwoole\Constant;

$server = new Server("0.0.0.0", 9501, Server::PROCESS, Constant::SOCK_TCP | Constant::SSL);
Last updated on October 14, 2022