Swoole is an extension of PHP, so installing Swoole is essentially installing a PHP extension. Swoole only supports three operating systems: Linux, FreeBSD, and MacOS.
Recommended tutorials: "thinkphp" "swoole tutorial"
Installation preparation
Before installation You must ensure that the following software has been installed on the system
php-7.1 或更高版本 gcc-4.8 或更高版本 make autoconf
Generally, the first three have been installed, and the autoconf tool needs to be installed.
Linux:yum install autoconf Mac:brew install autoconf
Install the Swoole extension for PHP
1. Download Swoole
Download address: https://github.com/swoole/swoole-src /releases
Under normal circumstances, download the latest version.
2. Unzip it into the PHP extension directory. For example, my decompression path is:
/Applications/MAMP/bin/php/php7.2.10/include/php/ext/
The decompressed directory can be named swoole.
3. Enter the decompressed swoole directory and execute the phpize command. If the command is not added to the environment variable, you can execute the absolute path
sudo /Applications/MAMP/bin/php/php7.2.10/bin/phpize
Since there are multiple PHPs on my computer version, so I specified the version I'm using now to execute the command.
Command path: /php installation directory/bin/phpize
4. Execute command:
./configure --with-php-config=/Applications/MAMP/bin/php/php7.2.10/bin/php-config --enable-openss --enable-http2
The specific path is modified according to the actual situation.
5. After executing the command:
make && make install
After success, the installation address will appear. For example, my address is:
/Applications/MAMP/bin/php/php7.2.10/lib/php/extensions/no-debug-non-zts-20170718/
Enter this directory and see if there is swoole in it. so.
6. Add the swoole extension to php.ini.
Be sure to select the currently used PHP version configuration file, which can be viewed using the php --ini command. Mine is at:
/Applications/MAMP/bin/php/php7.2.10/conf/php.ini
Open the php.ini file and add the extension:
extension=”/Applications/MAMP/bin/php/php7.2.10/lib/php/extensions/no-debug-non-zts-20170718/swoole.so”
If you use the MAMP integrated environment, in addition to modifying the above php.ini file, you also need to modify the integrated environment dynamic configuration php.ini file, you can click the arrow after the version in the php option of the panel, or File -> Edit Template -> PHP -> PHP 7.xx -> php.ini to modify it.
7. After the installation is complete, execute the command:
php -m
to see if there is a swoole module in the list. If so, the installation is successful.
Execute the php --ri swoole command to view swoole related information.
8. Test
Enter the following path, and modify it according to the actual situation:
/Applications/MAMP/bin/php/php7.2.10/include/php/ext/swoole/examples/server
There is an echo.php file in it. After opening it, you will see a line of code:
$serv = new swoole_server("0.0.0.0", 9501);
means to listen to all IPs, the port number is 9501.
In the command line, enter the command:
php echo.php
If the command is being executed, there is no cursor, and no error is reported, then create a new command window and enter the following command:
Linux: netstat -anp | grep 9501 Mac: ps -ef | grep 9501
You can see that this process is already executing and has a pid process number. The test is successful!
Install the Think-Swoole extension in the ThinkPHP framework
After the PHP extension is installed, you need to install the extension in the framework.
First, we need to download the ThinkPHP framework (Think-Swoole extension currently supports ThinkPHP 5.1 and ThinkPHP 6. In order to demonstrate some new functions, the latest ThinkPHP 6 framework will be used in subsequent articles), and then go to the framework Execute the Think-Swoole installation command in the root directory:
ThinkPHP 5.1 installation command
ThinkPHP 6 installation command
After the installation is completed, execute the command:
php think swoole
If you see the prompt in the picture below, the plug-in can be used normally:
Error 1:
I reported an error when I installed and executed the above command, check An error message shows that the Xdebug plug-in cannot be used, so just close it. In the MAMP environment, it can be turned off directly in the php options.
Error 2:
[Swoole\Exception] failed to listen server port[127.0.0.1:80], Error: Permission denied[13]
Open app/config/swoole.php, you can see that the port corresponds to port 80, because in Linux and Mac, only super administrators have ports 1024 and below. Use permissions, so you can change the port to 9501 (the default host is 127.0.0.1, which means monitoring the local address, here it is changed to 0.0.0.0, which means monitoring all addresses).
After the swoole service is turned on, let’s test it. Enter 127.0.0.1:9501 in the browser, and you can access it normally:
At the same time, this also shows that, Swoole comes with HTTP Server, which helps us open an http service, which is equivalent to Apache and Nginx.
At this point, the Think-Swoole plug-in installation is completed.
The above is the detailed content of Think-Swoole tutorial installation. For more information, please follow other related articles on the PHP Chinese website!