sanjay ojha
sanjay ojha

Reputation: 537

How to install swoole on ddev?

I want to try swoole (Asynchronous, coroutine-based concurrency library for PHP) in one of my project. I am using DDEV (Docker-based PHP development environments) as my development platform on WSL2 (ubuntu 22.04). It need PECL to install swoole 5.1. Not sure how I can install it.

Upvotes: 2

Views: 240

Answers (1)

sanjay ojha
sanjay ojha

Reputation: 537

After getting some help from Randy. I was able to figure out how to setup swoole development with DDEV on wsl2.

Luckly swoole is available in deb.sury.org. I added webimage_extra_packages: [php8.2-swoole] to the .ddev/config.yaml file and also exposed swoole ports.

web_extra_exposed_ports:
  - name: swoole
    container_port: 9501
    http_port: 9501
    https_port: 9502

And then restarted the ddev ddev restart.

Also, I have binded the swoole daemon to port 0.0.0.0 instead of 127.0.0.1. Like below code

//file index.php
$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->on('start', function ($server) {
    echo "Swoole http server is started at http://0.0.0.0:9501\n";
});

$http->on('Request', function ($request, $response) {
    if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
        $response->end();
        return;
    }
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});

$http->start();

Thats all you need to do. To run the swoole server. ssh to web container

ddev ssh

And then run this cli command

php index.php

I have posted this answer so it can help other in case they want to install swoole on ddev.

I hope there will be more better way to install swoole with ddev in future.

Upvotes: 3

Related Questions