在CentOS上安装PHP

一、环境描述

  1. 虚拟机配置
    CPU:单核
    内存:2 GB
    硬盘:120 GB
    IP:10.24.17.108

  2. 操作系统
    版本:CentOS 6.6 x86_64
    安装方式:Minimal

  3. 虚拟化环境
    VMware Workstation 12.1.0

  4. MySQL
    版本:mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
    安装方式:TAR压缩包

  5. nginx
    版本:nginx-1.10.1.tar.gz
    安装方式:编译源码安装

  6. PHP
    版本:php-5.6.22.tar.gz
    安装方式:编译源码安装

二、安装MySQL

按照《在CentOS上安装MySQL数据库》的步骤进行安装。

三、安装nginx

按照《在CentOS上安装nginx服务器》的步骤进行安装。

四、安装PHP的依赖包

在bash中运行以下命令,安装PHP的依赖包:

  1. yum --disablerepo=\* --enablerepo=c6-media install -y bzip2-devel freetype-devel glib2-devel libcurl-devel libjpeg-turbo-devel libpng-devel libxml2-devel openldap-clients openldap-devel openldap-servers gmp-devel readline-devel libxslt-devel

五、下载源码包

在bash中运行以下命令,下载PHP及其依赖库的源码包:

  1. cd /root/Downloads/
  2. # 下载PHP源码包
  3. wget http://cn2.php.net/distributions/php-5.6.22.tar.gz
  4. # 下载libmcrypt加密扩展源码包
  5. wget http://ncu.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz

六、解压源码包

在bash中运行以下命令,解压缩源码包:

  1. tar xvzf php-5.6.22.tar.gz
  2. tar xvzf libmcrypt-2.5.8.tar.gz

七、编译安装libmcrypt

在bash中运行以下命令,编译安装加密扩展包:

  1. cd libmcrypt-2.5.8
  2. ./configure
  3. make && make install
  4. cd ..

八、编译安装PHP

在bash中运行以下命令,编译安装PHP:

  1. cd php-5.6.22
  2. ./configure \
  3. --prefix=/usr/local/PHP \
  4. --with-config-file-path=/usr/local/PHP/etc \
  5. --with-mysql=/usr/local/MySQL \
  6. --with-mysql-sock=/usr/local/MySQL/sock/mysql.sock \
  7. --with-mysqli=/usr/local/MySQL/bin/mysql_config \
  8. --enable-fpm \
  9. --enable-soap \
  10. --with-libxml-dir \
  11. --with-openssl \
  12. --with-mcrypt \
  13. --with-mhash \
  14. --with-pcre-regex \
  15. --with-sqlite3 \
  16. --with-zlib \
  17. --enable-bcmath \
  18. --with-iconv \
  19. --with-bz2 \
  20. --enable-calendar \
  21. --with-curl \
  22. --with-cdb \
  23. --enable-dom \
  24. --enable-exif \
  25. --enable-fileinfo \
  26. --enable-filter \
  27. --with-pcre-dir \
  28. --enable-ftp \
  29. --with-gd \
  30. --with-openssl-dir \
  31. --with-jpeg-dir \
  32. --with-png-dir \
  33. --with-zlib-dir \
  34. --with-freetype-dir \
  35. --enable-gd-native-ttf \
  36. --enable-gd-jis-conv \
  37. --with-gettext \
  38. --with-gmp \
  39. --with-mhash \
  40. --enable-json \
  41. --enable-mbstring \
  42. --disable-mbregex \
  43. --disable-mbregex-backtrack \
  44. --with-libmbfl \
  45. --with-onig \
  46. --enable-pdo \
  47. --with-pdo-mysql \
  48. --with-zlib-dir \
  49. --with-pdo-sqlite \
  50. --with-readline \
  51. --enable-session \
  52. --enable-shmop \
  53. --enable-simplexml \
  54. --enable-sockets \
  55. --enable-sysvmsg \
  56. --enable-sysvsem \
  57. --enable-sysvshm \
  58. --enable-wddx \
  59. --with-libxml-dir \
  60. --with-xsl \
  61. --enable-zip \
  62. --enable-mysqlnd-compression-support \
  63. --with-pear
  64. make && make install

九、配置环境变量

在bash中运行以下命令,配置profile环境变量文件:

  1. sed -i '80s/$/&:\/usr\/local\/PHP\/bin/' /etc/profile
  2. sed -i '80s/$/&:\/usr\/local\/PHP\/sbin/' /etc/profile
  3. source /etc/profile

十、配置PHP

在bash中运行以下命令,配置php.ini配置文件:

  1. cp /root/Downloads/php-5.6.22/php.ini-production /usr/local/PHP/etc/php.ini
  2. sed -i '772s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /usr/local/PHP/etc/php.ini

十一、配置PHP-FPM

在bash中运行以下命令,配置php.fpm.ini配置文件:

  1. cd /usr/local/PHP/etc
  2. cp php-fpm.conf.default php-fpm.conf
  3. sed -i '164s/listen = 127.0.0.1:9000/listen = \/usr\/local\/PHP\/var\/run\/php5-fpm.sock/' /usr/local/PHP/etc/php-fpm.conf
  4. sed -i '149s/user = nobody/user = nginx/' /usr/local/PHP/etc/php-fpm.conf
  5. sed -i '150s/group = nobody/group = nginx/' /usr/local/PHP/etc/php-fpm.conf
  6. sed -i '175s/;listen.owner = nobody/listen.owner = nginx/' /usr/local/PHP/etc/php-fpm.conf
  7. sed -i '176s/;listen.group = nobody/listen.group = nginx/' /usr/local/PHP/etc/php-fpm.conf
  8. sed -i '177s/;listen.mode = 0660/listen.mode = 0666/' /usr/local/PHP/etc/php-fpm.conf

十二、将PHP-FPM设置为自启动服务

在bash中运行以下命令,将PHP-FPM配置为自启动服务:

  1. cd /root/Downloads/php-5.6.22
  2. cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  3. chmod 755 /etc/init.d/php-fpm
  4. chkconfig php-fpm on

十三、配置nginx

在bash中运行以下命令,使得nginx能够支持PHP页面:

  1. sed -i '43,85d' /usr/local/nginx/conf/nginx.conf
  2. sed -i '42a\ listen 80;' /usr/local/nginx/conf/nginx.conf
  3. sed -i '43a\ root \/home\/www;' /usr/local/nginx/conf/nginx.conf
  4. sed -i '44a\ index index.php index.html index.htm;' /usr/local/nginx/conf/nginx.conf
  5. sed -i '45a\ server_name 127.0.0.1;' /usr/local/nginx/conf/nginx.conf
  6. sed -i '46a\ location \/ {' /usr/local/nginx/conf/nginx.conf
  7. sed -i '47a\ try_files $uri $uri\/ \/index.html;' /usr/local/nginx/conf/nginx.conf
  8. sed -i '48a\ }' /usr/local/nginx/conf/nginx.conf
  9. sed -i '49a\ error_page 404 \/404.html;' /usr/local/nginx/conf/nginx.conf
  10. sed -i '50a\ error_page 500 502 503 504 \/50x.html;' /usr/local/nginx/conf/nginx.conf
  11. sed -i '51a\ location = \/50x.html {' /usr/local/nginx/conf/nginx.conf
  12. sed -i '52a\ root \/home\/www;' /usr/local/nginx/conf/nginx.conf
  13. sed -i '53a\ }' /usr/local/nginx/conf/nginx.conf
  14. sed -i '54a\ # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000' /usr/local/nginx/conf/nginx.conf
  15. sed -i '55a\ location ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf
  16. sed -i '56a\ try_files $uri =404;' /usr/local/nginx/conf/nginx.conf
  17. sed -i '57a\ fastcgi_pass unix:\/usr\/local\/PHP\/var\/run\/php5-fpm.sock;' /usr/local/nginx/conf/nginx.conf
  18. sed -i '58a\ fastcgi_index index.php;' /usr/local/nginx/conf/nginx.conf
  19. sed -i '59a\ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' /usr/local/nginx/conf/nginx.conf
  20. sed -i '60a\ include fastcgi_params;' /usr/local/nginx/conf/nginx.conf
  21. sed -i '61a\ }' /usr/local/nginx/conf/nginx.conf

十四、创建测试页面

在bash中运行以下命令,创建测试页面info.php:

  1. mkdir -p /home/www
  2. touch /home/www/info.php
  3. echo "<?php" > /home/www/info.php
  4. echo " phpinfo();" >> /home/www/info.php
  5. echo "?>" >> /home/www/info.php

十五、重启服务器

在bash中运行以下命令,重新启动服务器:

  1. reboot