nginx的安装

  • 安装nginx版本1.16.1,原因是安装方便。
  • 当然也会写nginx1.17.1的安装。缺点是比较慢,且麻烦。

先写安装比较简单的1.16.1版本

检查一下是否安装了nginx

yum list installed |grep nginx

安装nginx

yum install nginx

查看安装的nginx是什么版本

nginx -v

返回结果就是安装的版本

eg: nginx version: nginx/1.16.1

然后就是修改nginx配置文件

nginx的配置文件夹在/etc/nginx/
该目录下的nginx.conf就是配置文件
备份是个好习惯,丢失找回更方便,因此第一步是:

cd /etc/nginx
cp nginx.conf nginx.confbak
vi nginx.conf

按i进入编辑模式。

需要修改的部分,找到server的内容,替换为以下内容即可
配置完成后按ESC 输入:wq保存退出

server {
 listen       80;
 root   /usr/share/nginx/html;
 server_name  localhost;
 #charset koi8-r;
 #access_log  /var/log/nginx/log/host.access.log  main;
 location / {
       index index.php index.html index.htm;
 }
 #error_page  404              /404.html;
 #redirect server error pages to the static page /50x.html
 error_page   500 502 503 504  /50x.html;
 location = /50x.html {
   root   /usr/share/nginx/html;
 }
 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 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;
 }
}

附修改后的conf

nginx_1.16.1.conf

启动nginx

systemctl start nginx

开机自启nginx

systemctl enable nginx

到浏览器输入IP地址即可打开nginx的欢迎界面


nginx 1.17.1安装

创建ngin.repo文件

vi /etc/yum.repos.d/nginx.repo

并写入以下信息,因为这是官方的源,所以下载过程就有些令人捉急。

[nginx] 
name = nginx repo 
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/ 
gpgcheck = 0 
enabled = 1

安装nginx

yum install nginx

经过漫长的等待过后,终于成功的下载完成了
之后依然是修改配置文件不过文件夹的位置有所变化

cd /etc/nginx/conf.d

修改default.conf配置文件

vi default.conf

写入以下配置文件

server {
    listen       80;
    root   /usr/share/nginx/html;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        index  indxe.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    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;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

附修改后conf

nginx_1.17.1.conf

最后只需要启动nginx

systemctl start nginx

并设置开机自启

systemctl enable nginx

随后到浏览器输入IP地址即可打开nginx的欢迎界面


最后留下还有其他需要的可以浏览官方文档

nginx官方文档

nginx中文文档