linux下centos php-fpm设置为开启自启动和服务

centos 7 编译安装 php-7.2.11的步骤

在官网下载php-7.2.11的php压缩包,centos 7 安装 php-7.2.11的步骤,http://cn2.php.net/distributions/php-7.2.11.tar.gz

 
  1. cd /usr/local/src

  2. wget -c http://cn2.php.net/distributions/php-7.2.11.tar.gz

下载好后解压 php-7.2.11.tar.gz 压缩包,并进入解压后的目录

 
  1. tar -xzvf php-7.2.11.tar.gz

  2. cd php-7.2.11

安装php7需要的一些依赖库包 libxml2和一些其他依赖的扩展库

 
  1. yum -y install libxml2 

  2. yum -y install libxml2-devel 

  3. yum -y install openssl 

  4. yum -y install openssl-devel 

  5. yum -y install curl-devel 

  6. yum -y install libjpeg-devel 

  7. yum -y install libpng-devel 

  8. yum -y install freetype-devel

  9. yum -y install bzip2-devel

  10. yum -y install libmcrypt libmcrypt-devel

  11. yum -y install postgresql-devel

  12. yum -y install aspell-devel

  13. yum -y install readline-devel

  14. yum -y install libxslt-devel

  15. yum -y install net-snmp-devel

  16. yum -y install unixODBC-devel

  17. yum -y install libicu-devel

  18. yum -y install libc-client-devel

  19. yum -y install libXpm-devel

  20. yum -y install libvpx-devel

  21. yum -y install enchant-devel

  22. yum -y install openldap

  23. yum -y install openldap-devel

  24. yum -y install db4-devel

  25. yum -y install gmp-devel

  26. yum -y install sqlite-devel

  27. yum -y install mysql-devel

安装前的环境配置检查,php7的一些依赖包的检查和php扩展的启动,这个过程如果缺少php依赖的库包会有报错提示。

添加用户和组: (此处用户和组为 nginx, 我 nginx 用的用户名 ) groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin

php 安装配置

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-xmlrpc --with-openssl --with-mcrypt --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache

 

对php7进行编译和安装的操作

make && make install

( 如果出现 make: *** [sapi/cli/php] Error 1 的错误,通过继续执行 make ZEND_EXTRA_LIBS='-liconv' 命令解决

编译成功,查看php版本

/usr/local/php/bin/php -v

运行php-fpm

/usr/local/php/sbin/php-fpm
 

启动php-fpm可能会遇到的错误,基本都是配置文件配置和重命名的问题:

错误一:缺少www.conf配置文件(WARNING: Nothing  matches the include pattern /usr/local/php/etc/php-fpm.d/*.conf),需要把/usr/local/php/etc/php-fpm.d/*.conf目录中的www.conf.default重命名为www.conf。

错误二:缺少php-fpm.conf配置文件 需要在/usr/local/php/etc/php-fpm.conf 把默认的配置文件php-fpm.conf.default改成php-fpm.conf

错误三:缺少php.ini配置文件 php7编译安装好后会发现没有php.ini配置文件,需要在解压包的根目录中复制php.ini-production或php.ini-development并重命名为php.ini到php编译好的安装目录中的/usr/local/php/etc文件夹即可。


在centos7中编译安装好php后,系统重启后并不会对php进行开机启动,所以需要对centos7添加开机启动脚本对php进行开机启动服务。

php7编译安装方法:http://zixuephp.net/article-207.html    

手动启动:

  1. /usr/local/php/sbin/php-fpm

开机自动启动:

开启配置php-fpm pid

  1. #找到php-fpm.conf配置文件
  2. /usr/local/php/etc/php-fpm.conf
  3. #开启pid ,去掉分号注释
  4. pid = run/php-fpm.pid

创建开机启动脚本

在Linux中将php-fpm配置成服务的方法


PHP 源文件路径:   /usr/local/src/php-7.2.11/
PHP 安装后路径:   /usr/local/php/

1. 配置 php-fpm.conf

vim /usr/local/php/etc/php-fpm.conf

php-fpm.pid 目录必须指向:/usr/local/php/var/run/php-fpm.pid
 

2. 拷贝php-fpm脚本至/etc/init.d目录

cp /usr/local/src/php-7.2.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

 
  1. #! /bin/sh

  2.  
  3. ### BEGIN INIT INFO

  4. # Provides: php-fpm

  5. # Required-Start: $remote_fs $network

  6. # Required-Stop: $remote_fs $network

  7. # Default-Start: 2 3 4 5

  8. # Default-Stop: 0 1 6

  9. # Short-Description: starts php-fpm

  10. # Description: starts the PHP FastCGI Process Manager daemon

  11. ### END INIT INFO

  12.  
  13. prefix=/usr/local/php

  14. exec_prefix=${prefix}

  15.  
  16. php_fpm_BIN=${exec_prefix}/sbin/php-fpm

  17. php_fpm_CONF=${prefix}/etc/php-fpm.conf

  18. php_fpm_PID=${prefix}/var/run/php-fpm.pid

  19.  
  20.  
  21. php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"

  22.  
  23.  
  24. wait_for_pid () {

  25. try=0

  26.  
  27. while test $try -lt 35 ; do

  28.  
  29. case "$1" in

  30. 'created')

  31. if [ -f "$2" ] ; then

  32. try=''

  33. break

  34. fi

  35. ;;

  36.  
  37. 'removed')

  38. if [ ! -f "$2" ] ; then

  39. try=''

  40. break

  41. fi

  42. ;;

  43. esac

  44.  
  45. echo -n .

  46. try=`expr $try + 1`

  47. sleep 1

  48.  
  49. done

  50.  
  51. }

  52.  
  53. case "$1" in

  54. start)

  55. echo -n "Starting php-fpm "

  56.  
  57. $php_fpm_BIN --daemonize $php_opts

  58.  
  59. if [ "$?" != 0 ] ; then

  60. echo " failed"

  61. exit 1

  62. fi

  63.  
  64. wait_for_pid created $php_fpm_PID

  65.  
  66. if [ -n "$try" ] ; then

  67. echo " failed"

  68. exit 1

  69. else

  70. echo " done"

  71. fi

  72. ;;

  73.  
  74. stop)

  75. echo -n "Gracefully shutting down php-fpm "

  76.  
  77. if [ ! -r $php_fpm_PID ] ; then

  78. echo "warning, no pid file found - php-fpm is not running ?"

  79. exit 1

  80. fi

  81.  
  82. kill -QUIT `cat $php_fpm_PID`

  83.  
  84. wait_for_pid removed $php_fpm_PID

  85.  
  86. if [ -n "$try" ] ; then

  87. echo " failed. Use force-quit"

  88. exit 1

  89. else

  90. echo " done"

  91. fi

  92. ;;

  93.  
  94. status)

  95. if [ ! -r $php_fpm_PID ] ; then

  96. echo "php-fpm is stopped"

  97. exit 0

  98. fi

  99.  
  100. PID=`cat $php_fpm_PID`

  101. if ps -p $PID | grep -q $PID; then

  102. echo "php-fpm (pid $PID) is running..."

  103. else

  104. echo "php-fpm dead but pid file exists"

  105. fi

  106. ;;

  107.  
  108. force-quit)

  109. echo -n "Terminating php-fpm "

  110.  
  111. if [ ! -r $php_fpm_PID ] ; then

  112. echo "warning, no pid file found - php-fpm is not running ?"

  113. exit 1

  114. fi

  115.  
  116. kill -TERM `cat $php_fpm_PID`

  117.  
  118. wait_for_pid removed $php_fpm_PID

  119.  
  120. if [ -n "$try" ] ; then

  121. echo " failed"

  122. exit 1

  123. else

  124. echo " done"

  125. fi

  126. ;;

  127.  
  128. restart)

  129. $0 stop

  130. $0 start

  131. ;;

  132.  
  133. reload)

  134.  
  135. echo -n "Reload service php-fpm "

  136.  
  137. if [ ! -r $php_fpm_PID ] ; then

  138. echo "warning, no pid file found - php-fpm is not running ?"

  139. exit 1

  140. fi

  141.  
  142. kill -USR2 `cat $php_fpm_PID`

  143.  
  144. echo " done"

  145. ;;

  146.  
  147. configtest)

  148. $php_fpm_BIN -t

  149. ;;

  150.  
  151. *)

  152. echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"

  153. exit 1

  154. ;;

  155.  
  156. esac

3.设置权限并启动php-fpm:

chmod 755 /etc/init.d/php-fpm

/etc/init.d/php-fpm start

chkconfig –add php-fpm

最后,给出php-fpm以服务的方式启动、停止和重启:

service php-fpm start

service php-fpm stop

service php-fpm reload


PHP-FPM 自启动配置服务:  
vim /etc/rc.d/rc.local

 


查看开机启动的所有服务

chkconfig --list

你可能感兴趣的:(php)