nginx.conf添加php支持和其他

ENV:

[root@lnmp ~]# nginx -v
nginx version: nginx/1.16.0
[root@lnmp ~]# php -v
PHP 7.4.7 (cli) (built: Jul 20 2020 15:12:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

nginx.conf 文件,主要三部分组成
 
http {
    server{}
    server{}
}
 
一个nginx.conf对应一个http,代表http请求
一个http可以包含多个server区块,每个区块就是一个项目(网站)=>虚拟主机

不同的server区块指定一台电脑不同目录为不同的站点,实现一台电脑运行不同的多个网站

一:默认内容

对操作文件进行备份,防止出错

[root@lnmp Downloads]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@lnmp Downloads]# cd /usr/local/nginx/conf/

删减无用内容

[root@lnmp conf]# grep -Ev '#|^$' nginx.conf.bak > nginx.conf

最终内容及注释

[root@lnmp conf]# cat nginx.conf
worker_processes  1;
events {
    #连接数
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
    	#监听的端口
        listen       80;
        #真实域名,localhost代表本机
        server_name  localhost;
        #任意请求
        location / {
        	#项目目录
            root   html;
            #默认首页(如有默认首页,直接加载)
            index  index.html index.htm;
        }
        #当即访问遇到错误时,返回错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

二:添加php支持

在我们的备份文件nginx.conf.bak中已经带有php和其他的功能支持,只是没有启用,我们来添加一下,启用支持

注:修改内容  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

[root@lnmp conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
    	#监听的端口
        listen       80;
        #真实域名,localhost代表本机
        server_name  localhost;
       	#项目目录
        root   html;
        #任意请求
        location / {


            #默认首页(如有默认首页,直接加载)
            index  index.html index.htm;
        }
        #添加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   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

添加完后重新加载nginx,reload平滑重启,  systemctl reload nginx

并确保php-fpm启动, systemctl start php-fpm

[root@lnmp ~]# ss -naltp |grep 9000
LISTEN     0      128    127.0.0.1:9000                     *:*                   users:(("php-fpm",pid=30977,fd=8),("php-fpm",pid=30976,fd=8),("php-fpm",pid=30973,fd=6))

三:添加其他,如https(现在的网站基本都是这种方式,更安装)

只需如添加php一样,在备份文件中找到# HTTPS server 及以下server{}内容放到指定位置即可。

这里的内容和之前使用的server位置一样,为同级
    # HTTPS server
    #
    #server {
        #这里就是不同,采用443端口
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
        #这里可以看到通信是采用的MD5加密,这也是https更加安全的原因
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

 

你可能感兴趣的:(Linux)