上一次修改时间:2019-02-26 23:44:04

nginx整合PHP

  1. 安装图形库libxml2,jpeg8,libpng,freetype(字体库),gd库;

  2. [root@iZ28mhfkaunZ tar]# tar -zxvf libxml2-2.7.2.tar.gz 
    [root@iZ28mhfkaunZ tar]# cd libxml2-2.7.2/
    [root@iZ28mhfkaunZ libxml2-2.7.2]# ./configure --prefix=/usr/local/web/libxml2
    [root@iZ28mhfkaunZ libxml2-2.7.2]# make && make install
    + ----------------------------------------------------------------------------- +
    [root@iZ28mhfkaunZ tar]# tar -zxvf jpegsrc.v8b.tar.gz 
    [root@iZ28mhfkaunZ tar]# cd jpeg-8b/
    [root@iZ28mhfkaunZ jpeg-8b]# ./configure --prefix=/usr/local/web/jpeg \
    > --enable-shared --enable-static
    [root@iZ28mhfkaunZ jpeg-8b]# make && make install
    + ----------------------------------------------------------------------------- +
    [root@iZ28mhfkaunZ tar]# tar -zxvf libpng-1.4.3.tar.gz 
    [root@iZ28mhfkaunZ tar]# cd libpng-1.4.3/
    [root@iZ28mhfkaunZ libpng-1.4.3]# ./configure    #和zlib一样不要带参数,让它默认安装到相应
    [root@iZ28mhfkaunZ libpng-1.4.3]# make && make install
    + ----------------------------------------------------------------------------- +
    [root@iZ28mhfkaunZ tar]# tar -zxvf freetype-2.4.1.tar.gz 
    [root@iZ28mhfkaunZ freetype-2.4.1]# ./configure --prefix=/usr/local/web/freetype
    [root@iZ28mhfkaunZ freetype-2.4.1]# make && make install
    + ----------------------------------------------------------------------------- +
    [root@iZ28mhfkaunZ tar]# yum install libXpm-devel.x86_64    #缺少该lib,PHP make时会报错
    [root@iZ28mhfkaunZ tar]# tar -zxvf libgd-2.1.0.tar.gz
    [root@iZ28mhfkaunZ tar]# cd libgd-2.1.0/    #php5.6下gd库的版本必须大于2.1
    [root@iZ28mhfkaunZ libgd-2.1.0]# ./configure --prefix=/usr/local/web/gd2 \
    > --with-jpeg=/usr/local/web/jpeg \
    > --with-png \
    > --with-freetype=/usr/local/web/freetype
    [root@iZ28mhfkaunZ libgd-2.1.0]# make && make install
    gd安装时如果现在如下错误,需要安装libpng-devel
    configure: error: png support requested
    [root@iZ28mhfkaunZ libgd-2.1.0]yum install libpng libpng-devel
  3. 编译安装PHP

  4. #编译参数示例:
    ./configure --prefix=/usr/local/web/fastphp \
                --with-mysql=mysqlnd \
                --with-pdo-mysql=mysqlnd \
                --with-mysqli=mysqlnd \
                --with-freetype-dir=/usr/local/web/freetype \
                --with-gd=/usr/local/web/gd2 \
                --with-zlib --with-libxml-dir=/usr/local/web/libxml2 \
                --with-jpeg-dir=/usr/local/web/jpeg \
                --with-png-dir \
                --with-xpm-dir=/usr/lib64/  \  #dir位置需用find找
                --enable-mbstring=all \
                --enable-mbregex \
                --enable-shared \
                --disable-fileinfo \    #make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1 
                --enable-fpm
    make && make install
    服务器内存小于1G时,会产生如下错误:make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1 ,在configure时添加参数--disable-fileinfo
  5. 将源文件目录下的配置文件拷贝到安装文件夹中的lib目录下,并将安装文件中etc目录下的php-fpm.conf.default拷贝一份并重命名为php-fpm.conf

  6. [root@localhost php-5.4.13]# cp ./php.ini-development /usr/local/fastphp/lib/php.ini
    [root@localhost php-5.4.13]# cp /usr/local/fastphp/etc/php-fpm.conf.default /usr/local/fastphp/etc/php-fpm.conf
  7. 启动PHP

  8. [root@localhost php-5.4.13]# /usr/local/fastphp/sbin/php-fpm 
    [root@localhost php-5.4.13]# ps aux|grep php
    root     19607  0.0  0.1 184828  4216 ?        Ss   13:48   0:00 php-fpm: master process (/usr/local/fastphp/etc/php-fpm.conf)
    nobody   19608  0.0  0.1 186912  4112 ?        S    13:48   0:00 php-fpm: pool www
    nobody   19609  0.0  0.1 186912  4112 ?        S    13:48   0:00 php-fpm: pool www
    root     19612  0.0  0.0 112656   968 pts/0    S+   13:48   0:00 grep --color=auto php
  9. 将PHP加入开机启动,centos7系的系统中,该文件默认是没有执行权限的,需要将其加入执行权限才会生效;

  10. [root@localhost models]# vim /etc/rc.d/rc.local 
    #!/bin/bash                    
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    #                              
    # It is highly advisable to create own systemd services or udev rules
    # to run scripts during boot instead of using this file.
    #                              
    # In contrast to previous versions due to parallel execution during boot
    # this script will NOT be run after all other services.
    #                              
    # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    # that this script will be executed during boot.
                                   
    touch /var/lock/subsys/local   
    #/usr/local/apache/bin/apachectl start
    /usr/local/nginx/sbin/nginx    
    #/usr/local/mysql/bin/mysqld_safe --user=mysql &
    #rpm方式安装的mysql启动
    service mysql start
    /usr/local/fastphp/sbin/php-fpm
  11. 修改nginx的配置文件,添加PHP支持

  12. [root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf
  13. server {
        listen 80;
            server_name admin.zero.com;
              
            location / {
            root /usr/local/apache/htdocs/zero/backend/web;
            index index.php index.html index.htm;
            }
             
            location ~ \.php$ {                                                              
            root /usr/local/apache/htdocs/zero/backend/web;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
              
            }
            # access_log logs/admin.zero.com.access.log main;
    }
  14. rewrite示例

  15. server {
            listen 80;
            server_name www.zero.com;
        location / {
            rewrite  ([\w]+)\/([\w]+)\/([\w]+)\/([\w]+)\/([\w]+)\/([\w]+)\.html$  /index.php?r=$1/$2&$3=$4&$5=$6;
            rewrite  ([\w]+)\/([\w]+)\/([\w]+)\/([\w]+)\.html$  /index.php?r=$1/$2&$3=$4;
            rewrite  ([\w]+)\/([\w]+)\.html$  /index.php?r=$1/$2;
       
            root /usr/local/apache/htdocs/zero/frontend/web;
            index index.php index.html index.htm;
        }
        location ~ \.php$ {
            root /usr/local/apache/htdocs/zero/frontend/web;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
            # access_log logs/www.zero.com.access.log main;
    }
  16. 当需要使用pdo连接mysql时(如yii2),需要手动指定mysql.sock的位置;否则会产生一个mysql.sock的错误;

  17. [Pdo_mysql]
    ; If mysqlnd is used: Number of cache slots for the internal result set cache
    ; http://php.net/pdo_mysql.cache_size
    pdo_mysql.cache_size = 2000
    ; Default socket name for local MySQL connects.  If empty, uses the built-in
    ; MySQL defaults.
    ; http://php.net/pdo_mysql.default-socket
    pdo_mysql.default_socket=/var/lib/mysql/mysql.sock
  18. PHP下imagick扩展安装

  19. #ImageMagick安装
    [root@iZ28mhfkaunZ imagick]# tar -zxvf ImageMagick-6.7.5-10.tar.gz
    [root@iZ28mhfkaunZ imagick]# cd ImageMagick-6.7.5-10/
    [root@iZ28mhfkaunZ ImageMagick-6.7.5-10]# ./configure --prefix=/usr/local/web/imagemagick \
    CPPFLAGS="-I/usr/local/web/jpeg -I/usr/local/web/jpeg/include " LDFLAGS="-L/usr/local/lib -L/usr/local/web/jpeg/lib"
    [root@iZ28mhfkaunZ ImageMagick-6.7.5-10]# make && make install
    #注:imagemagick安装时,如果此前安装的jpeg不是默认安装时,需要手动指定jpeg的安装路径,否则安装完成后,不能处理识别jpg,jpeg类型的图像文件;
    
    #设置环境变量
    [root@iZ28mhfkaunZ pkgconfig]# export PKG_CONFIG_PATH=/usr/local/web/imagemagick/lib/pkgconfig
    [root@iZ28mhfkaunZ pkgconfig]# echo $PKG_CONFIG_PATH                           
    /usr/local/web/imagemagick/lib/pkgconfig
    [root@iZ28mhfkaunZ pkgconfig]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@iZ28mhfkaunZ pkgconfig]# export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/web/imagemagick/lib/pkgconfig
    [root@iZ28mhfkaunZ pkgconfig]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/web/imagemagick/lib/pkgconfig
    
    #编译php的imagick扩展
    [root@iZ28mhfkaunZ imagick-3.1.0RC3]# tar -zxvf imagick-3.1.0RC3.tgz 
    [root@iZ28mhfkaunZ imagick-3.1.0RC3]# cd imagick-3.1.0RC3/
    [root@iZ28mhfkaunZ imagick-3.1.0RC3]# /usr/local/web/fastphp/bin/phpize 
    [root@iZ28mhfkaunZ imagick-3.1.0RC3]# ./configure --with-php-config=/usr/local/web/fastphp/bin/php-config --with-imagick=/usr/local/web/imagemagick
    [root@iZ28mhfkaunZ imagick-3.1.0RC3]# make && make install
    #php.ini修改
    extension_dir="/usr/local/web/fastphp/lib/php/extensions/no-debug-non-zts-20131226/"extension="imagick.so
  20. 查看PHP在LINUX下的运行用户

  21. [root@iZ28mhfkaunZ ~]# ps -aux|grep php
    root     16545  0.0  0.4 178192  4620 ?        Ss   May24   0:49 php-fpm: master process (/usr/local/web/fastphp/etc/php-fpm.conf)
    nobody   16546  0.0  1.1 182732 11964 ?        S    May24   0:12 php-fpm: pool www
    nobody   16547  0.0  1.1 182968 11944 ?        S    May24   0:11 php-fpm: pool www
    nobody   16548  0.0  1.3 186116 13668 ?        S    May24   0:10 php-fpm: pool www
    root     31169  0.0  0.0 112644   960 pts/1    S+   23:20   0:00 grep --color=auto php
    #注:nobody为PHP运行时的用户,另外,$_SERVER['USER']也可以查看PHP运行的用户;
  22. 创建一个普通的用户

  23. [root@iZ28mhfkaunZ usr]# useradd -d /home/pikaqiu -m pikaqiu
    [root@iZ28mhfkaunZ usr]# passwd pikaqiu
  24. 修改php-fpm.conf里的用户及组(php的安装目录下的etc目录下)

    user = pikaqiu

    group = pikaqiu

    注:

  25. php下安装socket扩展

    先进入php安装源文件下的ext目录下的socket扩展目录,然后直接运行已安装的php下的bin目录下的phpize生成configure文件,指定php的安装目录及安装目录下bin下的php-config文件后make && make install,最后php.ini下添加生成的扩展;

  26. [root@iZ28mhfkaunZ sockets]# /usr/local/web/fastphp/bin/phpize
    [root@iZ28mhfkaunZ sockets]# ./configure -prefix=/usr/local/web/fastphp/ -with-php-config=/usr/local/web/fastphp/bin/php-config -enable-socket
  27. php重启nginx重启(扩展安装完后只需重php就行)

  28. [root@iZ28mhfkaunZ ~]# ps aux|grep php
    [root@iZ28mhfkaunZ ~]# kill -9 31443 
    [root@iZ28mhfkaunZ web]# /usr/local/web/fastphp/sbin/php-fpm
    [root@iZ28mhfkaunZ web]# ps aux|grep nginx
    [root@iZ28mhfkaunZ web]# kill -9 13485  
    [root@iZ28mhfkaunZ web]# /usr/local/web/nginx/sbin/nginx