微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

centos安装nginx、配置https

centosan安装Nginx,配置https

1. Nginx 安装、 ssl 模块安装

wget http://Nginx.org/download/Nginx-1.20.1.tar.gz
  • 解压安装包。
tar -vzxf Nginx-1.20.1.tar.gz
  • 配置 ssl 模块
cd Nginx-1.20.1.tar.gz
./configure --prefix=/usr/local/Nginx --with-http_ssl_module
  • 编译
    使用 make 命令编译,使用make install安装Nginx
make
make install

在这里插入图片描述

  • 查看安装后版本信息【注意使用大写V】,如果出现 configure arguments: --with-http_ssl_module, 则已安装http_ssl_module 模块,可以使用https
/usr/local/Nginx/sbin/Nginx -V

在这里插入图片描述

2. ssl 证书部署

2.1 使用申请的域名证书

下载申请好的 ssl 证书文件压缩包到本地并解压【pem 与 key 文件
Nginx 目录新建 cert 文件夹存放证书文件

2.2 使用openssl生成证书

# 1.创建服务器证书密钥文件 server.key:
$ openssl genrsa -des3 -out server.key 2048
# 输入密码,确认密码,自己随便定义,但是要记住,后面会用到。

# 2.创建服务器证书的申请文件 server.csr
openssl req -new -key server.key -out server.csr
# 输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 输入域名,如:iot.conet.com
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入

# 4.备份一份服务器密钥文件
cp server.key server.key.org

# 5.去除文件口令
openssl rsa -in server.key.org -out server.key

# 6.生成证书文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

3. Nginx.conf 配置

编辑 /usr/local/Nginx/conf/Nginx.conf 配置文件

配置 https server。
删除之前的 http server 配置,新增 https server:

    server {
        # 服务器端口使用443,开启ssl, 这里ssl就是上面安装的ssl模块
        listen       443 ssl;
        # 域名,多个以空格分开
        server_name  xyz.com;

        # ssl证书地址
        # ssl_certificate     /usr/local/Nginx/cert/ssl.pem;  # pem文件的路径
        # ssl_certificate_key  /usr/local/Nginx/cert/ssl.key; # key文件的路径
        
        ssl_certificate        /usr/local/Nginx/cert/server.crt;
        ssl_certificate_key    /usr/local/Nginx/cert/server.key;

        # ssl验证相关配置
        ssl_session_timeout  5m;    #缓存有效期
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;    #加密算法
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全链接可选的加密协议
        ssl_prefer_server_ciphers on;   #使用服务器端的首选算法

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

将 http 重定向 https

    server {
        listen       80;
        server_name  xyz.com;
        return 301 https://$server_name$request_uri;
    }

4. 重启 Nginx

/usr/local/Nginx/sbin/Nginx -c /usr/local/Nginx/conf/Nginx.conf

如果 80 端口被占用,用kill [id]来结束进程:

# 查看端口使用
$ netstat -lntp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21307/Nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3072/sshd           
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      21307/Nginx: master 

# 结束 80 端口进程
$ kill 21307

# 再次重启 Nginx:
$ /usr/local/Nginx/sbin/Nginx -c /usr/local/Nginx/conf/Nginx.conf

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐