Centos安装nginx

66 阅读2分钟

1:更新系统软件包:

[root@wyj ~]# sudo yum update

2:安装EPEL存储库

[root@wyj ~]# sudo yum install epel-release

3:安装Nginx

[root@wyj ~]# sudo yum install nginx

4:启动/停止Nginx服务

[root@wyj ~]# sudo systemctl start nginx // 启动服务
[root@wyj ~]# sudo systemctl stop nginx // 停止服务,如果服务已经启动了,可以使用nginx -s stop停止
[root@wyj ~]# sudo systemctl reload nginx // 重启服务,服务需要先启动,才能重启, 也可以使用 nginx -s reload命令重启

5:设置开机启动

[root@wyj ~]# sudo systemctl enable nginx

6:检查是否运行

[root@wyj ~]# sudo systemctl status nginx

7:查看nginx配置文件路径

[root@wyj ~]# find / -name nginx.conf
/etc/nginx/nginx.conf // nginx配置文件路径

8:检测配置文件是否错误

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

9: 开启https服务

[root@wyj ~]# vi /etc/nginx/nginx.conf

#添加配置如下
server {
        listen       443 ssl;  # HTTPS的默认访问端口443。如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
        server_name  <yourdomain>; # 填写证书绑定的域名

        ssl_certificate      "/etc/nginx/conf.d/cert/cert.pem"; # 填写证书文件绝对路径
        ssl_certificate_key  "/etc/nginx/conf.d/cert/cert.key"; # 填写证书私钥文件绝对路径
        ssl_session_cache     shared:SSL:1m;
        ssl_session_timeout      10m;
        
        # 自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
        # TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        
        # 表示优先使用服务端加密套件。默认开启
        ssl_prefer_server_ciphers    on;

        location / {
           root         /webserver/home; # 网站文件所属目录
           index        index.html; # 网站首页文件
        }
   }

10:配置http重定向到https

[root@wyj ~]# vi /etc/nginx/nginx.conf
#添加配置如下
server {
    listen 80;
    #填写证书绑定的域名
    server_name <yourdomain>;
    #将所有HTTP请求通过rewrite指令重定向到HTTPS。
    rewrite ^(.*)$ https://$host$1;
    location / {
        index index.html index.htm;
    }
}

检查端口是否在运行

[root@wyj ~]# netstat -ntlp  // 查看80/443端口占用情况

image.png