Swoole Getting Started Guide: php7 installation swoole detailed tutorial (1)


I haven’t updated it for a long time. It’s not lazy. Er, it’s too busy! I finally got a few days of leisure.
During this period of time, I’m going to bring you the introduction tutorial of swoole and feel the strength of nodejs in PHP.

All the sample code is placed on GitHub:learn-swoole


Apache is not used as a web server here. With nginx + PHP FPM, the system has more powerful performance and more convenient configuration. And in order to keep up with the pace of PHP, a relatively new version of PHP is also used

  • [x] centos7

  • [x] php7.0.12

  • [x] nginx/1.10.2

  • [x] php-fpm

Download swoole

First, download the source package of swoole. This operation is very simple without much to say.

wget -c https://github.com/swoole/swoole-src/archive/v2.0.6.tar.gz

Decompression: tar -zxvf v2.0.6.tar.gz

cd swoole-src-2.0.6/

Compile & install

Use phpize to generate PHP compilation configuration

./configureTo do compilation configuration detection

Compile with make and install with make install

Command execution:

[[email protected] swoole-src-2.0.6]# phpize
[[email protected] swoole-src-2.0.6]# ./configure
[[email protected] swoole-src-2.0.6]# make && make install

After make install, if it is correct, the following contents will appear

[[email protected] swoole-src-2.0.6]# make install
Installing shared extensions:     /usr/lib64/php/modules/

This means that in/usr/lib64/php/modules/Directory, successfully generatedswoole.sofile

Modify profile

To be able to use this module, you also need tophp.iniAdd the module to the file.

It should be noted here that the module configuration files of php7 are separated separately.
stayphp.iniThe following can be found in


; Note: packaged extension modules are now loaded via the .ini files
; found in the directory /etc/php.d; these are loaded by default.

Therefore, if your PHP is installed without special settings, you can/etc/php.dDirectory, locate the configuration file to add the module.

Enter intocd /etc/php.dDirectory, related configuration completed

[[email protected] swoole-src-2.0.6]# vim swoole.ini

; Enable swoole extension module

Restart service

After adding a PHP module, you need to restart the service before it takes effect. If it is still invalid after restart, please close it first and then restart it.

[[email protected] swoole-src-2.0.6]# systemctl restart nginx
[[email protected] swoole-src-2.0.6]# systemctl restart php-fpm

Use PHP – m or phpinfo() to see if the swoole was successfully loaded

Swoole Getting Started Guide: php7 installation swoole detailed tutorial (1)

Automatic prompt of swoole code

After installing the extension, it is a problem to use it. Because there is no code prompt, it is sometimes very inconvenient. Someone on GitHub has provided an IDE helper for the code prompt of swoole. The specific setting method is as follows:

Here, take phpstorm as an example, and download the code to the local

git clone [email protected]:eaglewu/swoole-ide-helper.git ide-helper

Set according to the picture content
Swoole Getting Started Guide: php7 installation swoole detailed tutorial (1)

After the above steps, you can use swoole in phpstorm. And provide code tips.

Simple test

All test codes are placed inhere

Please enter this codecloneTo the local, convenient to observe the effect.

git clone [email protected]:helei112g/learn-swoole.git

Code examples provided in this sectiondemo1-serv

Enter the test code directory,

Then use it on the servertelnetTest

  • First, run the listener on the server

php demo1-serv.php

After executing this command, the server will display:

Client: Connect.

  • Open another terminal and use ittelnetConnect the port monitored by the current server, enter some text and observe the changes of the terminal.

[[email protected] ~]# telnet 9999
Server: Hi!

Here is just a simple simulation reply. The server responds to any message sent by the client.
Exit telnet and observe what the server will display
Close the window directly, or:

[[email protected] ~]# Ctrl+]
[[email protected] ~]# telnet> quit

At this time, you will see on the service side:

Client: Close.

PS: if the test environment is not installedtelnet, please install Google by yourself.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]