Nginx学习-1( 支持PHP程序)

安装

Linux:Centos6.8

安装依赖

[root@engine67 ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*

创建Nginx用户

[root@engine67 ~]# useradd -M -s /sbin/nologin nginx

 

安装

[root@engine67 ~]# tar -zxvf nginx-1.12.0.tar.gz -C /usr/local/src/

[root@engine67 ~]# cd /usr/local/src/nginx-1.12.0/

[root@engine67 nginx-1.12.0]# \

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx


访问静态内容

配置文件中添加如下内容:

[root@engine67 nginx]# vim conf/nginx.conf

        location ~\.(gif|jpg|png) {

            root   /var/data/www/images;

        }

 

[root@engine67 nginx]# sbin/nginx -s reload

 

/var/data/www/images目录中上传图片structure.png

访问 http://192.168.1.67/structure.png测试

配置Nginx支持PHP


之前已经安装过PHP, 这里直接创建一个.php文件. echo phpinfo();

http://192.168.1.67/index.php

2017/05/03 00:06:43 [error] 4932#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.101, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.67"

 

[root@engine67 nginx]# chown -R nginx /var/data/www/nginx_test/

[root@engine67 nginx]# chgrp -R nginx /var/data/www/nginx_test/

修改权限后,发现还是这样

 

查看文档

设置FastCGI代理

nginx can be used to route requests to FastCGI servers which run applications built with various frameworks and programming languages such as PHP.

Nginx可以使用路由来发送请求到FastCGI服务, 来运行类似PHP的程序。

意思就是Nginx是通过fastcgi来处理PHP程序,

从配置文件也可以看出:

        location ~ \.php$ {

            root           /var/data/www/nginx_test;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }

This will set up a server that will route all requests except for requests for static images to the proxied server operating on localhost:9000 through the FastCGI protocol.

除了静态图片请求, 其他的请求都会使用FastCGI协议发送到9000端口代理服务来操作。

 

如何配置代理服务?

 

FastCGI的管理器是PHP-FPM, 需要启动PHP-FPM服务。而且PHP-FPM是PHP安装时候的需要指定 --enable-fpm

https://php-fpm.org/

 

启动php-fpm

 

[root@engine67 nginx]# /usr/local/php5.6/sbin/php-fpm

[03-May-2017 01:01:46] ERROR: failed to open configuration file '/usr/local/php5.6/etc/php-fpm.conf': No such file or directory (2)

[03-May-2017 01:01:46] ERROR: failed to load configuration file '/usr/local/php5.6/etc/php-fpm.conf'

[03-May-2017 01:01:46] ERROR: FPM initialization failed

[root@engine67 nginx]#

 

 

[root@engine67 nginx]# ls /usr/local/php5.6/etc/

pear.conf  php-fpm.conf.default

[root@engine67 nginx]# cp  /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf

[root@engine67 nginx]#

[root@engine67 nginx]# vim /usr/local/php5.6/etc/php-fpm.conf

user = nginx

group = nginx

[global]

; Pid file

; Note: the default prefix is /usr/local/php5.6/var

; Default Value: none

pid = run/php-fpm.pid

 

[root@engine67 nginx]# /usr/local/php5.6/sbin/php-fpm

[03-May-2017 01:07:44] NOTICE: PHP message: PHP Deprecated:  Comments starting with '#' are deprecated in Unknown on line 1 in Unknown on line 0

[root@engine67 nginx]#

 

[root@engine67 nginx]# netstat -tlunp|grep fpm

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      5005/php-fpm        

[root@engine67 nginx]#

 

重启Nginx

[root@engine67 nginx]# /usr/local/nginx/sbin/nginx -s reload

 

 

修改配置文件

查看文档:

http://nginx.org/en/docs/http/request_processing.html

A simple PHP site configuration

Now let’s look at how nginx chooses a location to process a request for a typical, simple PHP site:

server {

    ...

    location ~ \.php$ {

        fastcgi_pass  localhost:9000;

        fastcgi_param SCRIPT_FILENAME

                      $document_root$fastcgi_script_name;

        include       fastcgi_params;

    }

}

而默认配置文件为:      

location ~ \.php$ {

            root           /var/data/www/nginx_test;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scriptis$fastcgi_script_name;

            include        fastcgi_params;

}

 

/scriptis替换为 $document_root:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

测试成功

[root@engine67 nginx]# sbin/nginx -s reload

 

完成对PHP的支持。

 

安装一个开源软件进行测试, 看看还有没有其他问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值