Installing Swoole On Laravel Homestead

Nick Poulos
3 min readAug 16, 2019

--

Async Laravel Using Swoole

My last post talked about PHP performance related to Node.js, so I have decided to put my money where my mouth is, and get a little async PHP project up and running. Laravel Vapor isn’t out yet, so we are gonna try Swoole, a project started by Tencent in China and powers WeChat and others.

So the first step in that, is getting Swoole installed in our local Laravel Homestead environment. I will be going by the standard Swoole install docs. Or if you are the lazy type and want to skip to the end. Try this quick-n-dirty install script.

Install Swoole

  1. First, let’s update PECL.
sudo pecl channel-update pecl.php.net
Update PECL and install Swoole

2. Next, install Swoole via PECL

sudo pecl install swoole
Swoole completed install, now it tells us to update some configs

3. Swoole should complete installation and give you some config updates to make. Mainly adding the swoole extension to php.ini, and updating the Swoole config to point to that php.ini. Find your php.ini with:

php -i | grep php.ini

4. Ok lets run VIM and add the extension to php.ini

sudo vim /etc/php/7.3/cli/php.ini

and add extension=swoole.so at the end.

5. Restart php-fpm.

sudo service php7.3-fpm reload

6. Now let’s check PHP and see if our extension is loaded.

php -m | grep swoole

If everything worked correctly, you should see the word swoole echo’d back out to you in the terminal.

Forward Ports

Now that Swoole is successfully installed, we want to forward port 1215 from our host to Homestead.

1. Open up Homestead.yaml. And look for the last section “ports”. We want to add an entry send/to 1215, like so:

ports:
- send: 1215
to: 1215

2. Reload vagrant

vagrant reload --provision

Trouble Shooting

Sometimes having multiple versions of PHP under your Homestead can give you some errors running these lines exactly as-is. That is why I ran php -v earlier, to know which PHP version file paths to look for. If you run into trouble, take note of the file-paths to your exact PHP versions and make sure they match your extension directory.

Check your extension directory by running:

php -i | grep extension

--

--

Nick Poulos
Nick Poulos

No responses yet