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.
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
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
[[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 generated
To be able to use this module, you also need to
php.iniAdd the module to the file.
It should be noted here that the module configuration files of php7 are separated separately.
php.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 extension=swoole.so
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
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
After the above steps, you can use swoole in phpstorm. And provide code tips.
All test codes are placed inhere
Please enter this code
cloneTo 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 server
First, run the listener on the server
After executing this command, the server will display:
Open another terminal and use it
telnetConnect the port monitored by the current server, enter some text and observe the changes of the terminal.
[[email protected] ~]# telnet 127.0.0.1 9999 Hi! 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:
PS: if the test environment is not installed
telnet, please install Google by yourself.