Skip to content

Instantly share code, notes, and snippets.

@viezel
Created April 9, 2021 13:57
Show Gist options
  • Save viezel/49f7a80ade2ce7ff209e377bed978c8d to your computer and use it in GitHub Desktop.
Save viezel/49f7a80ade2ce7ff209e377bed978c8d to your computer and use it in GitHub Desktop.
Swoole http server on Laravel Forge
<?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
$server = new Swoole\HTTP\Server("0.0.0.0", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$server->set([
'ssl_cert_file' => '/etc/nginx/ssl/direct.example.com/3456789/server.crt',
'ssl_key_file' => '/etc/nginx/ssl/direct.example.com/3456789/server.key',
'ssl_ciphers' => 'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS_AES_256_GCM_SHA384:TLS-AES-256-GCM-SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS-CHACHA20-POLY1305-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA',
]);
$server->on("start", function (Server $server) {
echo "Swoole http server is started at http://0.0.0.0:9501\n";
});
$server->on("request", function (Request $request, Response $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello Laravel Forge\n");
});
$server->start();

Quickly get Octane running on Laravel Forge

  • Spin up a new App server, choose PHP 8
  • SSH into it and run sudo -i to become root
  • Run pecl install swoole and enable what you need. (I disabled curl as it did not work for me)
  • Add a new site to your server and use git to pull in your Laravel project that has Octane installed.
  • Go to "Network" within the overall server settings
  • Add new Firewall rule
    • name: Swoole
    • port: 9501
    • IP: leave empty
    • checkmark in Allow
  • Point your DNS to your new site
  • Enable SSL using Lets Encrypt
  • Copy SSL path
  • SSH into server and navigate to the public folder of the new site
  • run touch server.php
  • insert the content of server.php into it
  • update the SSL path and save the file

Now run the server

php server.php

You have Swoole HTTP Server running with SSL support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment