LEMP环境搭建及配置(七)配置Nginx来处理PHP页面

现在,我们安装了所有必需的组件。唯一的配置更改我们仍然需要做的是告诉Nginx使用我们的PHP处理器的动态内容。我们在服务器块级别上执行此操作(服务器块类似于Apache的虚拟主机)。

一、修改nginx.conf

whereis nginx搜索nginx

[root@localhost~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz
[root@localhost ~]# cd /etc/nginx
[root@localhost nginx]# ls
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@localhost nginx]# vim nginx.conf

vi nginx.conf打开配置

以下配置文件中,被>>><<<标注的地方,为必须要更改的地方,如下5点

[root@localhost nginx]# vi nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;                                                          1. >>>改成有权限的自定义账户,也可以是root<<< 【可暂时不改】
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  xxx;                                        2. >>>自定义服务名称<<<    【可暂时不改】
        root         /***/***/***;                               3. >>>项目根目录地址<<<      【可暂时不改】
        index        index.php index.html;                       4. >>>必须新增这一行,不然报403<<< 【可暂时不改】

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {

        }

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 5. 直接复制如下代码块,在server { } 模块里,表示动态访问php 【必须添加】

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        error_page 404 /404.html;
            location = /40x.html {

        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

ps aux | grep "nginx:worker process" | awk '{print $1}'查看nginx账户,本文直接使用root

[root@localhost ~]# ps aux | grep "nginx:worker process" | awk '{print $1}'
root

vim保存退出 ,建议查看是否修改成功,然后重启nginx

二、重启Nginx

注意~~~修改了配置文件后,最好检查下修改过的配置文件是否正确,以免重启后Nginx出现错误,影响服务器稳定运行。

nginx -t -c /xxx/xxx/nginx.conf检查Nginx配置是否正确

[root@localhost ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx -s reload重启,有错误会报错,没错误表明重启成功

[root@localhost ~]# nginx -s reload
[root@localhost ~]# 

当然也可以kill进程,然后重新启动

[root@localhost ~]# ps -ef | grep nginx
root     14136     1  0 09:24 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
root     14242 14192  0 10:29 pts/1    00:00:00 tail -f /var/log/nginx/error.log
root     14661 14136  0 14:35 ?        00:00:00 nginx: worker process
root     14698 14021  0 14:38 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# kill 14136
[root@localhost ~]# ps -ef | grep nginx
root     14242 14192  0 10:29 pts/1    00:00:00 tail -f /var/log/nginx/error.log
root     14700 14021  0 14:38 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# nginx -c /etc/nginx/nginx.conf
[root@localhost ~]# ps -ef | grep nginx
root     14242 14192  0 10:29 pts/1    00:00:00 tail -f /var/log/nginx/error.log
root     14702     1  0 14:39 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
root     14703 14702  0 14:39 ?        00:00:00 nginx: worker process
root     14709 14021  0 14:39 pts/0    00:00:00 grep --color=auto nginx

三、Web服务器上测试PHP处理

在/usr/share/nginx/html下面,新建test.php文件

[root@localhost ~]# vi /usr/share/nginx/html/test.php

输入如下php代码。


查看

[root@localhost ~]# cd /usr/share/nginx
[root@localhost nginx]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct  8 15:25 html
drwxr-xr-x 2 root root 4096 Sep 26 14:56 modules
[root@localhost nginx]# cd html
[root@localhost html]# ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png  test.php

测试,配置正确,如下图

Web访问php处理页面.png

你可能感兴趣的:(LEMP环境搭建及配置(七)配置Nginx来处理PHP页面)