nginx下对服务器脚本php的支持

Posted Active_Code

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx下对服务器脚本php的支持相关的知识,希望对你有一定的参考价值。

    1. 安装php7     
      下载地址:https://secure.php.net/downloads.php
      这里下载的是:wget http://ar2.php.net/distributions/php-7.0.6.tar.gz
      下载之后解压并进入在解压文件中
      安装:./configure  –enable-fpm (enable-fpm参数即可开启PHP-FPM)  ->  make && make install
      (PHP在 5.3.3 之后已经讲php-fpm写入php源码核心了)
      默认安装目录:
      复制代码
      root@iZ25fm7iewtZ:/php-7.0.6# make install
      Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20151012/
      Installing PHP CLI binary:        /usr/local/bin/
      Installing PHP CLI man page:      /usr/local/php/man/man1/
      Installing PHP FPM binary:        /usr/local/sbin/
      Installing PHP FPM config:        /usr/local/etc/
      Installing PHP FPM man page:      /usr/local/php/man/man8/
      Installing PHP FPM status page:   /usr/local/php/php/fpm/
      Installing phpdbg binary:         /usr/local/bin/
      Installing phpdbg man page:       /usr/local/php/man/man1/
      Installing PHP CGI binary:        /usr/local/bin/
      Installing PHP CGI man page:      /usr/local/php/man/man1/
      Installing build environment:     /usr/local/lib/php/build/
      Installing header files:           /usr/local/include/php/
      Installing helper programs:       /usr/local/bin/
        program: phpize
        program: php-config
      Installing man pages:             /usr/local/php/man/man1/
        page: phpize.1
        page: php-config.1
      Installing PEAR environment:      /usr/local/lib/php/
      [PEAR] Archive_Tar    - already installed: 1.4.0
      [PEAR] Console_Getopt - already installed: 1.4.1
      [PEAR] Structures_Graph- already installed: 1.1.1
      [PEAR] XML_Util       - already installed: 1.3.0
      [PEAR] PEAR           - already installed: 1.10.1
      Wrote PEAR system config file at: /usr/local/etc/pear.conf
      You may want to add: /usr/local/lib/php to your php.ini include_path
      /php-7.0.6/build/shtool install -c ext/phar/phar.phar /usr/local/bin
      ln -s -f phar.phar /usr/local/bin/phar
      Installing PDO headers:           /usr/local/include/php/ext/pdo/
      复制代码
    2. 安装nginx,见http://www.cnblogs.com/jecyhw/p/5505474.html
    3. nginx整合php-fpm
      1. 启动php-fpm: /usr/local/sbin/php-fpm
      报错
      [18-May-2016 18:07:58] ERROR: failed to open configuration file \'/usr/local/etc/php-fpm.conf\': No such file or directory (2)
      [18-May-2016 18:07:58] ERROR: failed to load configuration file \'/usr/local/etc/php-fpm.conf\'
      [18-May-2016 18:07:58] ERROR: FPM initialization failed

      到/usr/local/etc/目录下,将php-fpm.conf.default拷贝一份成php-fpm.conf

      root@iZ25fm7iewtZ:/# cd /usr/local/php/etc/
      root@iZ25fm7iewtZ:/usr/local/etc# cp php-fpm.conf.default php-fpm.conf

      然后在编辑php-fpm.conf配置文件

      ;最后一行改成如下
      include=/usr/local/etc/php-fpm.d/*.conf

      进入到/usr/local/etc/php-fpm.d/目录下,将www.conf.default拷贝一份成www.conf

      root@iZ25fm7iewtZ:/usr/local/etc# cd php-fpm.d/
      root@iZ25fm7iewtZ:/usr/local/etc/php-fpm.d# cp www.conf.default www.conf

      编辑www.conf文件,将user和group改成和nginx.conf中的user和group一致

      user = www
      group = www

      再次启动

      /usr/local/sbin/php-fpm
      查看是否启动成功
      复制代码
      root@iZ25fm7iewtZ:/usr/local/etc# ps -ef | grep php-fpm
      root      3691     1  0 18:49 ?        00:00:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
      www-data  3692  3691  0 18:49 ?        00:00:00 php-fpm: pool www      
      www-data  3693  3691  0 18:49 ?        00:00:00 php-fpm: pool www      
      root      4982 29553  0 18:59 pts/1    00:00:00 grep --color=auto php-fpm
      
      
      root@iZ25fm7iewtZ:/usr/local/etc# netstat -tnl | grep 9000
      tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN  
      复制代码

      开机启动php-fpm,开机启动的配置文件是:/etc/rc.local ,加入 /usr/local/sbin/php-fpm 即可

      vi /etc/rc.local
      添加 /usr/local/sbin/php-fpm


      修改nginx的配置文件,支持php文件的解析,找到location的添加位置,在后面添加下面这个location

      复制代码
       location ~ \\.php$ {
                              root /var/www; #指定php的根目录
                              fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
                              fastcgi_index index.php;
                              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                              include fastcgi_params;
                      }
      复制代码
    4. //解析目录可以直接指定nginx的发布目录的绝对路径

    5. 测试nginx.conf是否修改成功

      /usr/sbin/nginx -t

      重起nginx

      /usr/sbin/nginx -s reload

      进入到/var/www目录(如果该目录不存在的话,就使用mkdir命令创建)

      vi index.php
      
      添加
      
      <?php phpinfo(); ?>

      最后在浏览器中输入:localhost/index.php即可

以上是关于nginx下对服务器脚本php的支持的主要内容,如果未能解决你的问题,请参考以下文章

Nginx和PHP-FPM的启动重启停止脚本分享

Nginx Suse 11开机自动启动脚本

Nginx + PHP:当我尝试在包含的文件中调用函数时,脚本死了

实战Nginx与PHP(FastCGI)的安装配置与优化

fastCGI与PHP-fpm

nginx文件类型错误解析漏洞