创建用户组和用户

groupadd www
useradd -g www -s /sbin/nologin -M www

下载源码包, 然后解压, 进入解压后的目录

安装依赖

具体需要安装哪些依赖, 可以先执行下一步configure, 根据报错安装.

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel

configure

./configure \
    --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-config-file-scan-dir=/usr/local/php/etc/php.d \
    --with-fpm-user=www \        #默认就是www
    --with-fpm-group=www \        #默认就是www
    --enable-fpm \                #如果只使用swoole, 可以不使用php-fpm
    --enable-mysqlnd \            #php7连接mysql的默认方式
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-curl \
    --with-gd \
    --with-openssl \
    --enable-xml \
    --enable-pcntl \
    --enable-shmop \    #操作内存
    --enable-exif \        #获取完整EXif类
    --enable-inline-optimization \        #优化程序, 具体不太清楚
    --enable-mbstring \
    --disable-fileinfo \
    --disable-rpath \
    --disable-debug

configure成功

//这个提示是没有安装php-fpm的版本, 如果安装php-fpm, 生成sapi/fpm/init.d.php-fpm
Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

make && make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/src/php-7.2.11/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

将 /usr/local/php/bin 下的命令添加到 环境变量

echo "export PATH=/usr/local/php/bin:\$PATH" >> /etc/profile

初始化php.ini的一些设置

cp ./php.ini-production /usr/local/php/etc/php.ini
#!/bin/sh
sed -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' /usr/local/php/etc/php.ini
sed -i 's@^;cgi.fix_pathinfo.*@cgi.fix_pathinfo=0@' /usr/local/php/etc/php.ini
sed -i 's@^short_open_tag = Off@short_open_tag = On@' /usr/local/php/etc/php.ini
sed -i 's@^expose_php = On@expose_php = Off@' /usr/local/php/etc/php.ini
sed -i 's@^request_order.*@request_order = "CGP"@' /usr/local/php/etc/php.ini
sed -i 's@^;date.timezone.*@date.timezone = Asia/Shanghai@' /usr/local/php/etc/php.ini
sed -i 's@^post_max_size.*@post_max_size = 100M@' /usr/local/php/etc/php.ini
sed -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' /usr/local/php/etc/php.ini
sed -i 's@^max_execution_time.*@max_execution_time = 600@' /usr/local/php/etc/php.ini
sed -i 's@^;realpath_cache_size.*@realpath_cache_size = 2M@' /usr/local/php/etc/php.ini
sed -i 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' /usr/local/php/etc/php.ini
[ -e /usr/sbin/sendmail ] && sed -i 's@^;sendmail_path.*@sendmail_path = /usr/sbin/sendmail -t -i@' /usr/local/php/etc/php.ini

设置opcache

[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128    #1G内存是128, 2G内存是192
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=5
opcache.use_cwd=1
opcache.validate_timestamps=1
opcache.revalidate_freq=60
;opcache.save_comments=0
opcache.consistency_checks=0
;opcache.optimization_level=0

添加php-fpm到系统服务和开机

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm;    #添加到系统服务
chkconfig php-fpm on;        #添加到开机启动

初始化php-fpm的一些设置

./etc/php-fpm.conf中的是全局的设置, ./etc/php-fpm.d目录下的是自定义的设置.

//修改php-fpm.conf中的设置
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning

emergency_restart_threshold = 30
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = yes

修改/etc/php-fpm.d目录下的conf文件中的设置, 默认是www.conf

listen = /dev/shm/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = php-fpm
listen.group = php-fpm
listen.mode = 0666
user = php-fpm
group = php-fpm

pm = dynamic
pm.max_children = 16
pm.start_servers = 11
pm.min_spare_servers = 8
pm.max_spare_servers = 16
pm.max_requests = 2048
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
request_slowlog_timeout = 0

pm.status_path = /php-fpm_status
slowlog = log/slow.log
rlimit_files = 51200
rlimit_core = 0

catch_workers_output = yes
;env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

注意: listen = /dev/shm/php-cgi.sock 和 listen.mode = 0666 , 这个关系到 nginx.conf 中的 fastcgi_pass 参数的设置, 如果按照默认 listen = 127.0.0.1:9000 和 listen.mode = 0660 , 那么 fastcgi_pass 127.0.0.1:9000; 现在这么设置, 则 fastcgi_pass unix:/dev/shm/php-cgi.sock;

ginx和PHP-FPM的进程间通信有两种方式,一种是TCP,一种是UNIX Domain Socket.
其中TCP是IP加端口,可以跨服务器.而UNIX Domain Socket不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景.用哪种取决于你的PHP-FPM配置:
方式1:
php-fpm.conf: listen = 127.0.0.1:9000
nginx.conf: fastcgi_pass 127.0.0.1:9000;
方式2:
php-fpm.conf: listen = /tmp/php-fpm.sock
nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock;
其中php-fpm.sock是一个文件,由php-fpm生成,类型是srw-rw----.

UNIX Domain Socket可用于两个没有亲缘关系的进程,是目前广泛使用的IPC机制,比如X Window服务器和GUI程序之间就是通过UNIX Domain Socket通讯的.这种通信方式是发生在系统内核里而不会在网络里传播.UNIX Domain Socket和长连接都能避免频繁创建TCP短连接而导致TIME_WAIT连接过多的问题.对于进程间通讯的两个程序,UNIX Domain Socket的流程不会走到TCP那层,直接以文件形式,以stream socket通讯.如果是TCP Socket,则需要走到IP层,对于非同一台服务器上,TCP Socket走的就更多了.

UNIX Domain Socket:
Nginx <=> socket <=> PHP-FPM
TCP Socket(本地回环):
Nginx <=> socket <=> TCP/IP <=> socket <=> PHP-FPM
TCP Socket(Nginx和PHP-FPM位于不同服务器):
Nginx <=> socket <=> TCP/IP <=> 物理层 <=> 路由器 <=> 物理层 <=> TCP/IP <=> socket <=> PHP-FPM

像mysql命令行客户端连接mysqld服务也类似有这两种方式:
使用Unix Socket连接(默认):
mysql -uroot -p --protocol=socket --socket=/tmp/mysql.sock
使用TCP连接:
mysql -uroot -p --protocol=tcp --host=127.0.0.1 --port=3306

针对不同的服务器的内存, 需要对一些参数做一点调整

#!/bin/sh
Mem=`free -m | awk '/Mem:/{print $2}'`
if [ $Mem -le 3000 ]; then
  sed -i "s@^pm.max_children.*@pm.max_children = $(($Mem/3/20))@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.start_servers.*@pm.start_servers = $(($Mem/3/30))@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = $(($Mem/3/40))@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = $(($Mem/3/20))@" /usr/local/php/etc/php-fpm.conf
elif [ $Mem -gt 3000 -a $Mem -le 4500 ]; then
  sed -i "s@^pm.max_children.*@pm.max_children = 50@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.start_servers.*@pm.start_servers = 30@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 20@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 50@" /usr/local/php/etc/php-fpm.conf
elif [ $Mem -gt 4500 -a $Mem -le 6500 ]; then
  sed -i "s@^pm.max_children.*@pm.max_children = 60@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.start_servers.*@pm.start_servers = 40@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 30@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 60@" /usr/local/php/etc/php-fpm.conf
elif [ $Mem -gt 6500 -a $Mem -le 8500 ]; then
  sed -i "s@^pm.max_children.*@pm.max_children = 70@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.start_servers.*@pm.start_servers = 50@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 40@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 70@" /usr/local/php/etc/php-fpm.conf
elif [ $Mem -gt 8500 ]; then
  sed -i "s@^pm.max_children.*@pm.max_children = 80@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.start_servers.*@pm.start_servers = 60@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 50@" /usr/local/php/etc/php-fpm.conf
  sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 80@" /usr/local/php/etc/php-fpm.conf
fi

安装成功

安装成功, 启动php-fpm

systemctl start php-fpm.service

标签: none

添加新评论