首页 > 代码库 > Linux下php+mysql+nginx编译搭建(三)

Linux下php+mysql+nginx编译搭建(三)

之前一直都是一键搭建的web服务器,但是一键搭建的环境相对来说都是比较老的。如果要用比较新的环境,特别是正式服务器,就必须自己手动编译搭建了(以下搭建基于linux centos6.5 32位服务器)。

三、安装php
进入安装目录:

  1. cd /opt

下载并解压:

  1. Wget http://am1.php.net/get/php-5.4.34.tar.gz/from/this/mirror
  2. tar -zxf php-5.4.34.tar.gz

进入目录编译:

  1. cd php-5.4.34
  2. ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli=/usr/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/usr/local/mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-pear --enable-pdo --with-pdo-mysql --with-gettext --enable-exif --enable-wddx --enable-calendar --enable-ftp --enable-dba --enable-sysvmsg --enable-sysvshm --enable-debug --enable-maintainer-zts --enable-embed --with-pcre-regex --enable-gd-jis-conv --with-fpm-user=www --with-fpm-group=www --enable-sockets

报错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
在http://sourceforge.net/projects/mcrypt/files/下载libmcrypt和mcrypt编译(其中编译mcrypt需要mhash的支持,所以编译完libmcrypt后需要下载mhash,编译完mhash后才编译mcrypt)

  1. wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
  2. tar -zxvf libmcrypt-2.5.8.tar.gz
  3. cd libmcrypt-2.5.8
  4. ./configure
  5. Make && Make install
  6.  
  7. Wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
  8. tar -zxvf mhash-0.9.9.9.tar.gz
  9. cd mhash-0.9.9.9.tar.gz
  10. ./configure
  11. Make && Make install
  12.  
  13. wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
  14. tar -zxvf mcrypt-2.6.8.tar.gz
  15. cd mcrypt-2.6.8
  16. ./configure
  17. Make && Make install

此时再编译(命令不copy了),报错:mysql_config not found
执行:find / -name mysql_config
发现/opt/mysql-5.5.30/scripts/mysql_config
于是:cp /opt/mysql-5.5.30/scripts/mysql_config /usr/bin/mysql_config
再次编译,通过。

  1. make && make install

由于php5.4已经集成了php-fpm,所以我们不需要再额外下载php-fpm。
直接运行:

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

报错:ERROR: failed to open configuration file ‘/usr/local/php/etc/php-fpm.conf‘: No such file or directory (2)

  1. Cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf

解决。ps aux | grep php 查看,已经启动。

此时新建一个php文件访问,是不是还不支持?哈[坏笑]。
修改nginx.conf

  1. location ~ \.php$ {
  2. root html;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. include fastcgi_params;
  7. }

重启nginx,至此,所有完结。

然后是不是又有点淡淡的忧伤?觉得/usr/local/php/sbin/php-fpm太长?习惯了/etc/init.d/php-fpm start?
Vim /etc/init.d/php-fpm

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: php-fpm
  4. # Required-Start: $remote_fs $network
  5. # Required-Stop: $remote_fs $network
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: starts php-fpm
  9. # Description: starts the PHP FastCGI Process Manager daemon
  10. ### END INIT INFO
  11. prefix=/usr/local/php
  12. exec_prefix=${prefix}
  13.  
  14. php_fpm_BIN=${exec_prefix}/sbin/php-fpm
  15. php_fpm_CONF=${prefix}/etc/php-fpm.conf
  16. php_fpm_PID=${prefix}/var/run/php-fpm.pid
  17. php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
  18. wait_for_pid () {
  19. try=0
  20. while test $try -lt 35 ; do
  21. case "$1" in
  22. ‘created‘)
  23. if [ -f "$2" ] ; then
  24. try=‘‘
  25. break
  26. fi
  27. ;;
  28. ‘removed‘)
  29. if [ ! -f "$2" ] ; then
  30. try=‘‘
  31. break
  32. fi
  33. ;;
  34. esac
  35. echo -n .
  36. try=`expr $try + 1`
  37. sleep 1
  38. done
  39. }
  40. case "$1" in
  41. start)
  42. echo -n "Starting php-fpm "
  43. $php_fpm_BIN --daemonize $php_opts
  44. if [ "$?" != 0 ] ; then
  45. echo " failed"
  46. exit 1
  47. fi
  48. wait_for_pid created $php_fpm_PID
  49. if [ -n "$try" ] ; then
  50. echo " failed"
  51. exit 1
  52. else
  53. echo " done"
  54. fi
  55. ;;
  56. stop)
  57. echo -n "Gracefully shutting down php-fpm "
  58. if [ ! -r $php_fpm_PID ] ; then
  59. echo "warning, no pid file found - php-fpm is not running ?"
  60. exit 1
  61. fi
  62. kill -QUIT `cat $php_fpm_PID`
  63. wait_for_pid removed $php_fpm_PID
  64. if [ -n "$try" ] ; then
  65. echo " failed. Use force-quit"
  66. exit 1
  67. else
  68. echo " done"
  69. fi
  70. ;;
  71. force-quit)
  72. echo -n "Terminating php-fpm "
  73. if [ ! -r $php_fpm_PID ] ; then
  74. echo "warning, no pid file found - php-fpm is not running ?"
  75. exit 1
  76. fi
  77. kill -TERM `cat $php_fpm_PID`
  78. wait_for_pid removed $php_fpm_PID
  79. if [ -n "$try" ] ; then
  80. echo " failed"
  81. exit 1
  82. else
  83. echo " done"
  84. fi
  85. ;;
  86. restart)
  87. $0 stop
  88. $0 start
  89. ;;
  90. reload)
  91. echo -n "Reload service php-fpm "
  92. if [ ! -r $php_fpm_PID ] ; then
  93. echo "warning, no pid file found - php-fpm is not running ?"
  94. exit 1
  95. fi
  96. kill -USR2 `cat $php_fpm_PID`
  97. echo " done"
  98. ;;
  99. *)
  100. echo "Usage: $0 {start|stop|force-quit|restart|reload}"
  101. exit 1
  102. ;;
  103. esac

保存,添加x权限。

如需开机启动:chkconfig php-fpm on


更多请支持:http://www.webyang.net/Html/web/article_129.html

Linux下php+mysql+nginx编译搭建(三)