• 技术文章 >后端开发 >PHP问题

    php nginx 安装配置的方法

    藏色散人藏色散人2020-11-04 10:17:58原创548

    php nginx安装配置的方法:首先找到Nginx的配置文件;然后在vim中点击“i”进入编辑模式;接着使用FastCGI协议默认配置;最后重启Nginx服务即可。

    推荐:《PHP视频教程

    写在前面

    在学习搭建LNMP环境的过程中初识Nginx(读法:engine x),感觉完全复制粘贴网上的安装配置方法没有什么意义,就打算展开学习一下。

    关于Windows下Nginx的安装和配置:Windows下的Nginx安装与配置(PHP)

    工作环境

    书面信息

    Nginx:俄罗斯工程师Igor Sysoev开发,高性能的HTTP/反向代理/邮件服务器。

    安装

    CentOS下安装:

    #使用yum安装,-y表示对所有的提问都回答“yes”,install为安装指令yum -y install nginx

    安装Nginx

    配置

    Nginx的配置文件默认位置为:/etc/nginx/nginx.conf

    如果说找不到可以搜索一下:

    #locate 搜索文件的位置locate nginx.conf

    搜索nginx.conf

    如上图,在我的环境中nginx.conf在/etc/nginx/nginx.conf

    使用vim打开文件nginx.conf

    vim /etc/nginx/nginx.conf

    配置文件分析

    nginx.conf内容如下(只截取了没被注掉的部分):

    nignx.conf默认

    # nginx运行的用户名user nginx;# nginx启动进程,通常设置成和cpu的数量相等,这里为自动worker_processes auto;# errorlog文件位置error_log /var/log/nginx/error.log;# pid文件地址,记录了nginx的pid,方便进程管理pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.# 用来加载其他动态模块的配置include /usr/share/nginx/modules/*.conf;# 工作模式和连接数上限events {    # 每个worker_processes的最大并发链接数
        # 并发总数:worker_processes*worker_connections
        worker_connections 1024;
    }# 与提供http服务相关的一些配置参数类似的还有mailhttp {    # 设置日志的格式
        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记录访问的用户、页面、浏览器、ip和其他的访问信息
        access_log  /var/log/nginx/access.log  main;    # 这部分下面会单独解释
        # 设置nginx是否使用sendfile函数输出文件
        sendfile            on;    # 数据包最大时发包(使用Nagle算法)
        tcp_nopush          on;    # 立刻发送数据包(禁用Nagle算法)
        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;    # http服务上支持若干虚拟主机。
        # 每个虚拟主机一个对应的server配置项
        # 配置项里面包含该虚拟主机相关的配置。
        server {        # 端口
            listen       80 default_server;        listen       [::]:80 default_server;        # 访问的域名
            server_name  _;        # 默认网站根目录(www目录)
            root         /usr/share/nginx/html;        # Load configuration files for the default server block.
    
            include /etc/nginx/default.d/*.conf;        # 默认请求
            location / {
            }        # 错误页(404)
            error_page 404 /404.html;            location = /40x.html {
            }        # 错误页(50X)
            error_page 500 502 503 504 /50x.html;            location = /50x.html {
            }
        }
    }

    值得说明的几点

    1. 关于error_log 可以设置log的类型(记录什么级别的信息)有:debug、info、notice、warn、error、crit几种

    2. 关于sendfile
      一般的网络传输过程
      硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >>协议栈
      使用sendfile后
      硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈
      可以显著提高传输性能。

    3. tcp_nopush和tcp_nodelay
      tcp_nopush只有在启用了sendfile时才起作用,
      在启用tcp_nopush后,程序接收到了数据包后不会马上发出,而是等待数据包最大时一次性发出,可以缓解网络拥堵。(Nagle化)
      相反tcp_nodelay则是立即发出数据包.

    配置

    分析完了配置文件后开始配置环境。

    因为只是配置PHP的服务器,而且只使用一个端口所以只需要改动server部分

    在vim中点击‘i’进入编辑模式。

    server {        listen       80 default_server;        listen       [::]:80 default_server;        # 这里改动了,也可以写你的域名
            server_name  localhost;        root         /usr/share/nginx/html;        # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;        location / {            # 这里改动了 定义首页索引文件的名称
                index index.php index.html index.htm;
            }        error_page 404 /404.html;            location = /40x.html {
            }        error_page 500 502 503 504 /50x.html;            location = /50x.html {
            }        # 这里新加的
            # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
            # Fastcgi服务器和程序(PHP,Python)沟通的协议.
            location ~ \\.php$ {            # 设置监听端口
                fastcgi_pass   127.0.0.1:9000;            # 设置nginx的默认首页文件(上面已经设置过了,可以删除)
                fastcgi_index  index.php;            # 设置脚本文件请求的路径
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;            # 引入fastcgi的配置文件
                include        fastcgi_params;
            }
        }

    修改完成后将vim编辑器切换到一般一半模式(Esc),然后输入:wq保存退出。

    之后重启Nginx服务

    service nginx restart

    以上就配置成功了,但是上面的配置只是nginx配置部分,更多的内容需要继续学习。

    测试

    我们可以通过下面的方法判断Nginx配置是否成功。

    1. 在Nginx的网站根目录(/usr/share/nginx/html)下创建一个php文件,随便起名我的是phpinfo.php

      内容如下:

      <?php
      
          // 顺便可以看一下php的扩展全不全
          phpinfo();
    2. 进入你的网站看看能不能打开文件
      你的ip/文件名 例如:localhost/phpinfo.php

    这里写图片描述

    我的成功了~~

    以上就是php nginx 安装配置的方法的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php nginx
    上一篇:php删除空格函数是什么 下一篇:php服务器怎么配置
    大前端线上培训班

    相关文章推荐

    • php实现上传图片的简单方法• php utf 8如何转gbk• php数据库中文乱码的解决办法• php删除空格函数是什么

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网