Nginx+PHP基础配置

Posted bazingafraser

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+PHP基础配置相关的知识,希望对你有一定的参考价值。

返回顶部

 

目录:

            一、在一台服务器的Ngnx+php配置

            二、分离式的nginx+php配置

            三、php监听端口为0.0.0.0:9000配置

            四、upstream方式的配置

           五、 nginx+php配置文件及功能参数介绍

 

 

      1. 简要说明:

          nginx安装路径:/usr/local/nginx

         nginx_config路径:/usr/local/nginx/conf/nginx.conf

         nginx_virtualhost路径:/usr/local/nginx/conf/vhost/*

         php安装路径   :/usr/local/php

         php_php-fpm路径:/usr/local/php/etc/php-fpm.conf

        

          网站根目录:/data/www/*

          nginx_ip地址:192.168.110.40

          php_ip地址:192.168.110.46,192.168.110.46

 

          标红的字体为每次配置更改的参数

 

一、在一台服务器的Ngnx+php配置

        1、Nginx配置文件

                        

user  www www;

worker_processes 4;

 

events

  {

        use epoll;

        worker_connections 1204;

        }

 

http

       {

                include       mime.types;

                default_type  application/octet-stream;

 

               

                     sendfile on;

                tcp_nopush     on;

 

                keepalive_timeout 60;

 

                tcp_nodelay on;

 

 

                server_tokens off;

               

                log_format  access  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

             ‘$status $body_bytes_sent "$http_referer" ‘

             ‘"$http_user_agent" $http_x_forwarded_for‘;

 

server

        {

                listen       80;

                server_name www.test.com;

                index index.php  index.html index.htm;

                root  /data/www/www.test.com;

                error_log  /data/weblogs/nginx_error.log  crit;

                access_log  /data/wwwlogs/access.log  access;

 

                         ###fastcgi配置

           location ~ \.php$ {

            fastcgi_pass   127.0.0.1:9000;    php  fast-cgi监听地址和端口

            fastcgi_index  index.php;               支持的首页文档类型

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi.conf;

                  }

        }

 

}

 

  在Nginx+php的配置中,Nginx只需要配置上支持首页文档格式为index.php的格式,同时配置正确的fastcgi配置即可,红色字体标注

 

2、php配置

[global]

pid = /usr/local/php/php-fpm.pid

error_log = /data/weblogs/php-fpm-error.log

log_level = notice

emergency_restart_threshold = 10

emergency_restart_interval = 1m

process_control_timeout = 5s

daemonize = yes

[www]

listen = 127.0.0.1:9000

listen.backlog = -1

listen.allowed_clients = 127.0.0.1

user = www

group = www

listen.mode=0666

pm = dynamic

pm.max_children = 64

pm.start_servers = 5

pm.min_spare_servers = 5

pm.max_spare_servers = 15

pm.max_requests = 1024

request_terminate_timeout = 0s

request_slowlog_timeout = 0s

slowlog = /data/weblogs/php-fpm-slow.log

rlimit_files = 65535

rlimit_core = 0

chroot =

chdir =

catch_workers_output = yes

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

php_flag[display_errors] = off

 

在Nginx+php的配置中,php配置文件只需要修改“listen = ”参数和"listen.allowed_clients ="两个参数即可,

listen =  :php服务器的监听地址和监听端口

listen.allowed_clients = :允许那些ip,可以访问php-fastcgi

 

 

 

 

二、分离式的nginx+php配置

         分离式的Nginx+php配置,也就是Nginx和php分别搭建在两台服务器上

         Nginx服务器:192.168.110.40

         php服务器:    192.168.110.46

         准备工作:

                  首先需要确认网站根目录是什么路径

                   实验环境是在/data/www/www.test.com下

         分清两个条件:

                       1、如果你的程序里既包括静态网页又存在动态网页,那么你的Nginx和php项目的根目录下必须都要存在相同的代码文件

                       2、如果你的程序里只包含动态的网页,那么只需要将代码部署到php的根目录下即可,需要主义这个根目录是写在Nginx配置文件中的

        

    Nginx配置

user  www www;

worker_processes 4;

 

events

        {

                use epoll;

                worker_connections 1204;

        }

 

http

        {

                include       mime.types;

                default_type  application/octet-stream;

 

                server_names_hash_bucket_size 128;

                client_header_buffer_size 32k;

                large_client_header_buffers 4 32k;

                client_max_body_size 50m;

 

                sendfile on;

                tcp_nopush     on;

 

                keepalive_timeout 60;

 

                tcp_nodelay on;

 

 

                server_tokens off;

                #log format

                log_format  access  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

             ‘$status $body_bytes_sent "$http_referer" ‘

             ‘"$http_user_agent" $http_x_forwarded_for‘;

 

server

        {

                listen       80;

                server_name www.test.com;

                index index.php  index.html index.htm;

                root  /data/www/www.test.com;

                error_log  /data/weblogs/nginx_error.log  crit;

                access_log  /data/wwwlogs/access.log  access;

 

           location ~ \.php$ {

            fastcgi_pass   192.168.110.46:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi.conf;

        }

        }

 

}

 

在分离式Nginx+php配置中,Nginx配置需要注意两点,一个是指定网站根目录root  /data/www/www.test.com  一个是fastcgi_pass 

 

php配置

 

[global]

pid = /usr/local/php/php-fpm.pid

error_log = /data/weblogs/php-fpm-error.log

log_level = notice

emergency_restart_threshold = 10

emergency_restart_interval = 1m

process_control_timeout = 5s

daemonize = yes

[www]

listen = 192.168.110.46:9000

listen.backlog = -1

listen.allowed_clients = 127.0.0.1,192.168.110.40

user = www

group = www

listen.mode=0666

pm = dynamic

pm.max_children = 64

pm.start_servers = 5

pm.min_spare_servers = 5

pm.max_spare_servers = 15

pm.max_requests = 1024

request_terminate_timeout = 0s

request_slowlog_timeout = 0s

slowlog = /data/weblogs/php-fpm-slow.log

rlimit_files = 65535

rlimit_core = 0

chroot =

chdir =

catch_workers_output = yes

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

php_flag[display_errors] = off

在分离式Nginx+php配置中,php配置需要注意的还是“listen = ”  “listen.allowed_clients = ” 这两个配置,在这个配置中,listen = 修改为了,可以让其他主机访问的监听ip,可以看为公网ip地址。如果写127.0.0.1是无法提供服务的。

listen.allowed_clients = 这个位置新添加了192.168.110.40 是需要使用fastcgi进程的用户ip,如果不添加,fastcgi是不允许本机之外的其他主机访问的

 

 

 

三、php监听端口为0.0.0.0:9000配置

         分离式环境,但是fastcgi的监听地址和端口为 0.0.0.0:9000

         php服务器ip地址:

               公网:123.123.123.23

               内网或者私网:192.168.110.40

              本地:127.0.0.1

         例如php服务器同时有这样的使用需求,需要上述三个ip地址同时可以成为监听端口,那么久需要配置为0.0.0.0:9000任意地址的监听

    

php配置

[global]

pid = /usr/local/php/php-fpm.pid

error_log = /data/weblogs/php-fpm-error.log

log_level = notice

emergency_restart_threshold = 10

emergency_restart_interval = 1m

process_control_timeout = 5s

daemonize = yes

[www]

listen = 0.0.0.0:9000

listen.backlog = -1

listen.allowed_clients = 127.0.0.1,192.168.110.40

user = www

group = www

listen.mode=0666

pm = dynamic

pm.max_children = 64

pm.start_servers = 5

pm.min_spare_servers = 5

pm.max_spare_servers = 15

pm.max_requests = 1024

request_terminate_timeout = 0s

request_slowlog_timeout = 0s

slowlog = /data/weblogs/php-fpm-slow.log

rlimit_files = 65535

rlimit_core = 0

chroot =

chdir =

catch_workers_output = yes

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

php_flag[display_errors] = off

                

 

Nginx配置

    

user  www www;

worker_processes 4;

events

        {

                use epoll;

                worker_connections 1204;

        }

 

http

        {

                include       mime.types;

                default_type  application/octet-stream;

 

                server_names_hash_bucket_size 128;

                client_header_buffer_size 32k;

                large_client_header_buffers 4 32k;

                client_max_body_size 50m;

 

                sendfile on;

                tcp_nopush     on;

 

                keepalive_timeout 60;

 

                tcp_nodelay on;

 

 

                server_tokens off;

                #log format

                log_format  access  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

             ‘$status $body_bytes_sent "$http_referer" ‘

             ‘"$http_user_agent" $http_x_forwarded_for‘;

 

server

        {

                listen       80;

                server_name www.test.com;

                index index.php  index.html index.htm;

                root  /data/www/www.test.com;

                error_log  /data/weblogs/nginx_error.log  crit;

                access_log  /data/wwwlogs/access.log  access;

 

           location ~ \.php$ {

            fastcgi_pass   192.168.110.46:9000;

                           fastcgi_pass   192.168.110.40:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi.conf;

        }

        }

 

}

 

php采用了监听0.0.0.0:9000的方式,那么Nginx就可以为fastcgi_pass 配置不同的监听地址,都可以成功的请求到fastcgi程序

 

 

 

 

四、upstream方式的配置

         可以简单的实现后端服务器负载均衡,之前的文档有介绍

  

 

  创建一个文件,在文件中添加,upstream配置参数,以下面的配置为例,upstream指定了一个名字为myfastcgi的负载均衡池  

 如果是虚拟主机的配置,根据当前环境文件是创建在/usr/local/nginc/conf/vhost/目录下的

 如果是只有一个站点,且直接使用nginx.conf进行配置,只需要将upstream的配置写到nginx.conf的http区块就可以了

 

[[email protected] vhost]# vim /usr/local/nginx/conf/vhost/myfastcgi            

  

   upstream myfastcgi  {

   server  192.168.110.40:9000 weight=1 max_fails=2 fail_timeout=30;

     server  192.168.110.46:9000 weight=5 max_fails=2 fail_timeout=30;

      }

 

 

虚拟主机配置文件:

server

        {

                listen       80;

                server_name www.test.com;

                index index.php  index.html index.htm;

                root  /data/www/www.test.com;

                error_log  /data/weblogs/nginx_error.log  crit;

                access_log  /data/wwwlogs/access.log  access;

 

           location ~ \.php$ {

            fastcgi_pass    myfastcgi;

                        fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi.conf;

        }

        }

 

nginx.conf配置文件

user  www www;

 

worker_processes 4;

 

 

events

        {

                use epoll;

                worker_connections 1204;

        }

 

http

        {

                include       mime.types;

                default_type  application/octet-stream;

 

                server_names_hash_bucket_size 128;

                client_header_buffer_size 32k;

                large_client_header_buffers 4 32k;

                client_max_body_size 50m;

 

                sendfile on;

                tcp_nopush     on;

 

                keepalive_timeout 60;

 

                tcp_nodelay on;

 

 

                server_tokens off;

                #log format

                log_format  access  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

              ‘$status $body_bytes_sent "$http_referer" ‘

             ‘"$http_user_agent" "$http_x_forwarded_for" ‘

              ‘"$request_time" "$upstream_response_time" "$upstream_addr" ‘;

             upstream myfastcgi  {

                       server  192.168.110.40:9000 weight=1 max_fails=2 fail_timeout=30;

                       server  192.168.110.46:9000 weight=1 max_fails=2 fail_timeout=30;

              }

 

 

server

        {

                listen       80;

                server_name www.test.com;

                index index.php  index.html index.htm;

                root  /data/www/www.test.com;

                error_log  /data/weblogs/nginx_error.log  crit;

                access_log  /data/wwwlogs/access.log  access;

 

           location ~ \.php$ {

            fastcgi_pass   myfastcgi;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi.conf;

        }

        }

 

}

 

上述配置可以实现,192.168.110.40,192.168.110.46两台主机同时处理请求,轮询算法   

 

 

 

 

 

 

以上是关于Nginx+PHP基础配置的主要内容,如果未能解决你的问题,请参考以下文章

Nginx基础入门之fastcgi常用配置项说明

CentOS7 配置 nginx php php-fpm

nginx和php的基本配置说明

nginx配置多域名(二级域名)

Web 基础——Nginx

Web 基础——Nginx