[Worker Man]安裝流程-AWS


安裝說明

WorkerMan實際上就是一個PHP代碼包,如果你的PHP環境已經裝好,只需要把WorkerMan源代碼或者demo下載下來即可運行。

安裝php 7.2

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@workerman ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
warning: /var/tmp/rpm-tmp.O2pe84: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:epel-release-7-11                ################################# [100%]

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[root@workerman ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
warning: /var/tmp/rpm-tmp.O2pe84: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:epel-release-7-11                ################################# [100%]

yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-posix php72w-pear

[root@workerman ~]# yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-posix php72w-pear
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-webtatic-el7
Importing GPG key 0x62E74CA5:
 Userid     : "Webtatic EL7 <rpms@webtatic.com>"
 Fingerprint: 830d b159 6d9b 9b01 99dc 24a3 e87f d236 62e7 4ca5
 Package    : webtatic-release-7-3.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-webtatic-el7
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : php72w-common-7.2.14-1.w7.x86_64    1/27 
  Installing : libargon2-20161029-2.el7.x86_64     2/27 
  Installing : php72w-cli-7.2.14-1.w7.x86_64       3/27 
  Installing : php72w-pdo-7.2.14-1.w7.x86_64       4/27 
  Installing : libXau-1.0.8-2.1.el7.x86_64         5/27 
  Installing : libxcb-1.13-1.el7.x86_64            6/27 
  //------------------------(略)---------------------//
  Verifying  : autoconf-2.69-11.el7.noarch        22/27 
  Verifying  : libXau-1.0.8-2.1.el7.x86_64        23/27 
  Verifying  : php72w-opcache-7.2.14-1.w7.x86_64  24/27 
  Verifying  : php72w-embedded-7.2.14-1.w7.x86_64 25/27 
  Verifying  : php72w-gd-7.2.14-1.w7.x86_64       26/27 
  Verifying  : mod_php72w-7.2.14-1.w7.x86_64      27/27 

Installed:
  mod_php72w.x86_64 0:7.2.14-1.w7        php72w-cli.x86_64 0:7.2.14-1.w7         php72w-common.x86_64 0:7.2.14-1.w7    
  php72w-devel.x86_64 0:7.2.14-1.w7      php72w-embedded.x86_64 0:7.2.14-1.w7    php72w-fpm.x86_64 0:7.2.14-1.w7       
  php72w-gd.x86_64 0:7.2.14-1.w7         php72w-mbstring.x86_64 0:7.2.14-1.w7    php72w-mysqlnd.x86_64 0:7.2.14-1.w7   
  php72w-opcache.x86_64 0:7.2.14-1.w7    php72w-pdo.x86_64 0:7.2.14-1.w7         php72w-process.x86_64 0:7.2.14-1.w7   
  php72w-xml.x86_64 0:7.2.14-1.w7       

Dependency Installed:
  autoconf.noarch 0:2.69-11.el7           automake.noarch 0:1.13.4-3.el7          libX11.x86_64 0:1.6.5-2.el7            
  libX11-common.noarch 0:1.6.5-2.el7      libXau.x86_64 0:1.0.8-2.1.el7           libXpm.x86_64 0:3.5.12-1.el7           
  libargon2.x86_64 0:20161029-2.el7       libjpeg-turbo.x86_64 0:1.2.90-6.el7     libxcb.x86_64 0:1.13-1.el7             
  m4.x86_64 0:1.4.16-10.el7               pcre-devel.x86_64 0:8.32-17.el7         perl-Data-Dumper.x86_64 0:2.145-3.el7  
  perl-Test-Harness.noarch 0:3.28-3.el7   perl-Thread-Queue.noarch 0:3.02-2.el7  

Complete!

安裝gcc pecl wget

yum -y install wget

[root@workerman ~]# yum -y install wget
Failed to set locale, defaulting to C
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================
 Package    Arch         Version            Repository                          Size
=====================================================================================
Installing:
 wget       x86_64       1.14-18.el7        rhui-REGION-rhel-server-releases   547 k

Transaction Summary
=====================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
wget-1.14-18.el7.x86_64.rpm                                       | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7.x86_64                                           1/1 
  Verifying  : wget-1.14-18.el7.x86_64                                           1/1 

Installed:
  wget.x86_64 0:1.14-18.el7                                                              

Complete!

wget http://pear.php.net/go-pear.phar

[root@workerman ~]# wget http://pear.php.net/go-pear.phar
--2019-03-24 01:12:04--  http://pear.php.net/go-pear.phar
Resolving pear.php.net (pear.php.net)... 109.203.101.62
Connecting to pear.php.net (pear.php.net)|109.203.101.62|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://pear.php.net/go-pear.phar [following]
--2019-03-24 01:12:05--  https://pear.php.net/go-pear.phar
Connecting to pear.php.net (pear.php.net)|109.203.101.62|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5389331 (5.1M)
Saving to: 'go-pear.phar'

100%[==========================================>] 5,389,331   2.72MB/s   in 1.9s   

2019-03-24 01:12:08 (2.72 MB/s) - 'go-pear.phar' saved [5389331/5389331]

php go-pear.phar

[root@workerman ~]# php go-pear.phar 

Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.

 1. Installation base ($prefix)                   : /usr
 2. Temporary directory for processing            : /tmp/pear/install
 3. Temporary directory for downloads             : /tmp/pear/install
 4. Binaries directory                            : /usr/bin
 5. PHP code directory ($php_dir)                 : /usr/share/pear
 6. Documentation directory                       : /usr/docs
 7. Data directory                                : /usr/data
 8. User-modifiable configuration files directory : /usr/cfg
 9. Public Web Files directory                    : /usr/www
10. System manual pages directory                 : /usr/man
11. Tests directory                               : /usr/tests
12. Name of configuration file                    : /etc/pear.conf

1-12, 'all' or Enter to continue:  
Beginning install...
Configuration written to /etc/pear.conf...
Initialized registry...
Preparing to install...
installing phar:///tmp/go-pear.phar/PEAR/go-pear-tarballs/Archive_Tar-1.4.5.tar...
installing phar:///tmp/go-pear.phar/PEAR/go-pear-tarballs/Console_Getopt-1.4.2.tar...
installing phar:///tmp/go-pear.phar/PEAR/go-pear-tarballs/PEAR-1.10.8.tar...
installing phar:///tmp/go-pear.phar/PEAR/go-pear-tarballs/Structures_Graph-1.1.1.tar...
installing phar:///tmp/go-pear.phar/PEAR/go-pear-tarballs/XML_Util-1.4.3.tar...
installing PEAR-1.10.9.tar...
PHP Warning:  require_once(phar://go-pear.phar/PEAR/REST/13.php): failed to open stream: phar error: "PEAR/REST/13.php" is not a file in phar "go-pear.phar" in phar:///tmp/go-pear.phar/PEAR/Config.php on line 2067

yum -y install gcc gcc-c++ kernel-devel

[root@workerman ~]# yum -y install gcc gcc-c++ kernel-devel
Failed to set locale, defaulting to C
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-36.el7_6.1 will be installed
--> Processing Dependency: cpp = 4.8.5-36.el7_6.1 for package: gcc-4.8.5-36.el7_6.1.x86_64
//-----------------------------(略)--------------------------//
--> Processing Dependency: kernel-headers >= 2.2.1 for package: glibc-headers-2.17-260.el7_6.3.x86_64
--> Processing Dependency: kernel-headers for package: glibc-headers-2.17-260.el7_6.3.x86_64
--> Running transaction check
---> Package kernel-headers.x86_64 0:3.10.0-957.10.1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================
 Package         Arch   Version              Repository                        Size
====================================================================================
Installing:
 gcc             x86_64 4.8.5-36.el7_6.1     rhui-REGION-rhel-server-releases  16 M
 gcc-c++         x86_64 4.8.5-36.el7_6.1     rhui-REGION-rhel-server-releases 7.2 M
 kernel-devel    x86_64 3.10.0-957.10.1.el7  rhui-REGION-rhel-server-releases  17 M
Installing for dependencies:
 cpp             x86_64 4.8.5-36.el7_6.1     rhui-REGION-rhel-server-releases 5.9 M
 glibc-devel     x86_64 2.17-260.el7_6.3     rhui-REGION-rhel-server-releases 1.1 M
 glibc-headers   x86_64 2.17-260.el7_6.3     rhui-REGION-rhel-server-releases 683 k
 kernel-headers  x86_64 3.10.0-957.10.1.el7  rhui-REGION-rhel-server-releases 8.0 M
 libmpc          x86_64 1.0.1-3.el7          rhui-REGION-rhel-server-releases  51 k
 libstdc++-devel x86_64 4.8.5-36.el7_6.1     rhui-REGION-rhel-server-releases 1.5 M
 mpfr            x86_64 3.1.1-4.el7          rhui-REGION-rhel-server-releases 203 k

Transaction Summary
====================================================================================
Install  3 Packages (+7 Dependent packages)

Total download size: 57 M
Installed size: 122 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/10): glibc-devel-2.17-260.el7_6.3.x86_64.rpm              | 1.1 MB  00:00:00     
(2/10): gcc-c++-4.8.5-36.el7_6.1.x86_64.rpm                  | 7.2 MB  00:00:01     
(3/10): gcc-4.8.5-36.el7_6.1.x86_64.rpm                      |  16 MB  00:00:01     
(4/10): glibc-headers-2.17-260.el7_6.3.x86_64.rpm            | 683 kB  00:00:00     
(5/10): libmpc-1.0.1-3.el7.x86_64.rpm                        |  51 kB  00:00:00     
(6/10): cpp-4.8.5-36.el7_6.1.x86_64.rpm                      | 5.9 MB  00:00:01     
(7/10): mpfr-3.1.1-4.el7.x86_64.rpm                          | 203 kB  00:00:00     
(8/10): kernel-headers-3.10.0-957.10.1.el7.x86_64.rpm        | 8.0 MB  00:00:00     
(9/10): libstdc++-devel-4.8.5-36.el7_6.1.x86_64.rpm          | 1.5 MB  00:00:00     
(10/10): kernel-devel-3.10.0-957.10.1.el7.x86_64.rpm         |  17 MB  00:00:00     
------------------------------------------------------------------------------------
Total                                                   28 MB/s |  57 MB  00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mpfr-3.1.1-4.el7.x86_64                                         1/10 
  //-----------------------------(略)--------------------------//
  Installing : gcc-c++-4.8.5-36.el7_6.1.x86_64                                 9/10 
  Installing : kernel-devel-3.10.0-957.10.1.el7.x86_64                        10/10 
  Verifying  : glibc-devel-2.17-260.el7_6.3.x86_64                             1/10 
  Verifying  : gcc-4.8.5-36.el7_6.1.x86_64                                     2/10 
  //-----------------------------(略)--------------------------//
  Verifying  : kernel-devel-3.10.0-957.10.1.el7.x86_64                         8/10 
  Verifying  : cpp-4.8.5-36.el7_6.1.x86_64                                     9/10 
  Verifying  : glibc-headers-2.17-260.el7_6.3.x86_64                          10/10 

Installed:
  gcc.x86_64 0:4.8.5-36.el7_6.1                gcc-c++.x86_64 0:4.8.5-36.el7_6.1   
  kernel-devel.x86_64 0:3.10.0-957.10.1.el7   

Dependency Installed:
  cpp.x86_64 0:4.8.5-36.el7_6.1                                                     
  glibc-devel.x86_64 0:2.17-260.el7_6.3                                             
  glibc-headers.x86_64 0:2.17-260.el7_6.3                                           
  kernel-headers.x86_64 0:3.10.0-957.10.1.el7                                       
  libmpc.x86_64 0:1.0.1-3.el7                                                       
  libstdc++-devel.x86_64 0:4.8.5-36.el7_6.1                                         
  mpfr.x86_64 0:3.1.1-4.el7                                                         

Complete!

安裝event或者libevent擴展

為了能支持更大的並發連接數,必須安裝event擴展或者libevent擴展(二者作用相同,二選一即可),並且優化Linux內核。以Event為例,安裝方法如下:

wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libevent-2.0.21-4.el7.x86_64.rpm

[root@workerman ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libevent-2.0.21-4.el7.x86_64.rpm
[root@workerman ~]# rpm -ivh libevent-2.0.21-4.el7.x86_64.rpm 
warning: libevent-2.0.21-4.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:libevent-2.0.21-4.el7            ################################# [100%]

wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libevent-devel-2.0.21-4.el7.x86_64.rpm

[root@workerman ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libevent-devel-2.0.21-4.el7.x86_64.rpm
[root@workerman ~]# rpm -ivh libevent-devel-2.0.21-4.el7.x86_64.rpm 
warning: libevent-devel-2.0.21-4.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:libevent-devel-2.0.21-4.el7      ################################# [100%]

pecl install event

注意提示:Include libevent OpenSSL support [yes] :時輸入no回車,
注意提示:PHP Namespace for all Event classes :時輸入yes,其它直接敲回車就行

[root@workerman ~]# pecl install event
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading event-2.4.3.tgz ...
Starting to download event-2.4.3.tgz (139,788 bytes)
..............................done: 139,788 bytes
50 source files, building
running: phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
perl: warning: Falling back to the standard locale ("C").
Enable internal debugging in Event [no] : 
Enable sockets support in Event [yes] : 
libevent installation prefix [/usr] : 
Include libevent's pthreads library and enable thread safety support in Event [no] : 
Include libevent protocol-specific functionality support including HTTP, DNS, and RPC [yes] : 
Include libevent OpenSSL support [yes] : no    //輸入 no
PHP Namespace for all Event classes [no] : yes //輸入 yes
openssl installation prefix [no] : 
building in /tmp/pear/install/pear-build-rootYC0caN/event-2.4.3
running: /tmp/pear/install/event/configure --with-php-config=/usr/bin/php-config --enable-event-debug=no --enable-event-sockets=yes --with-event-libevent-dir=/usr --with-event-pthreads=no --with-event-extra --with-event-openssl=no --with-event-ns --with-openssl-dir=no

vim /etc/php.d/sockets.ini

[root@workerman ~]# vim /etc/php.d/sockets.ini
; Enable sockets extension module
extension=sockets.so
extension=event.so   //加入這行

php -m

[root@workerman ~]# php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
event   //會多了這個event
exif
fileinfo
filter
ftp

Linux內核調優

為了讓系統能夠支持更大的並發,除了必須安裝event擴展(或libevent擴展)之外,優化linux內核也是重中之重,以下優化每一項都非常非常重要,請務必按逐一完成。

vim /etc/sysctl.conf

[root@workerman ~]# vim /etc/sysctl.conf
net.ipv4.tcp_max_tw_buckets = 20000
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 262144
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_tw_recycle = 0
fs.file-max = 6815744
net.netfilter.nf_conntrack_max = 2621440

設定說明

#该参数设置系统的TIME_WAIT的数量,如果超过默认值则会被立即清除
net.ipv4.tcp_max_tw_buckets = 20000
#定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数
net.core.somaxconn = 65535
#对于还未获得对方确认的连接请求,可保存在队列中的最大数目
net.ipv4.tcp_max_syn_backlog = 262144
#在每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
net.core.netdev_max_backlog = 30000
#能够更快地回收TIME-WAIT套接字。此选项会导致处于NAT网络的客户端超时,建议为0
net.ipv4.tcp_tw_recycle = 0
#系统所有进程一共可以打开的文件数量
fs.file-max = 6815744
#防火墙跟踪表的大小。注意:如果防火墙没开则会提示error: "net.netfilter.nf_conntrack_max" is an unknown key,忽略即可
net.netfilter.nf_conntrack_max = 2621440

執行 sysctl -p

[root@workerman ~]# sysctl -p
net.ipv4.tcp_max_tw_buckets = 20000
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 262144
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_tw_recycle = 0
fs.file-max = 6815744
sysctl: cannot stat /proc/sys/net/netfilter/nf_conntrack_max: No such file or directory //忽略即可

打開文件數

設置系統打開文件數設置,解決高並發下too many open files問題。此選項直接影響單個進程容納的客戶端連接數。

Soft open files是Linux系統參數,影響系統單個進程能夠打開最大的文件句柄數量,這個值會影響到長連接應用如聊天中單個進程能夠維持的用戶連接數,運行ulimit -n能看到這個參數值,如果是1024,就是代表單個進程只能同時最多只能維持1024甚至更少(因為有其它文件的句柄被打開)。如果開啟4個進程維持用戶連接,那麼整個應用能夠同時維持的連接數不會超過4*1024個,也就是說最多只能支持4×1024個用戶在線可以增大這個設置以便服務能夠維持更多的TCP連接。

Soft open files 修改方法:

(1)ulimit -HSn 102400

這只是在當前終端有效,退出之後,open files 又變為默認值。

(2)在/etc/profile文件末尾添加一行ulimit -HSn 102400,這樣每次登錄終端時,都會自動執行/etc/profile。

(3)令修改open files的數值永久生效,則必須修改配置文件:/etc/security/limits.conf. 在這個文件後加上:

vim /etc/security/limits.conf

[root@workerman ~]# vim /etc/security/limits.conf
* soft nofile 1024000
* hard nofile 1024000
root soft nofile 1024000
root hard nofile 1024000