Nginx快速部署SSL证书_2023年10月29日可用

一、目的

复制代码
Nginx快速部署SSL证书
                                       --2023年10月29日可用

二、环境

  • Centos/Ubuntu服务器

  • 前端代码文件夹:

    • 假设为:/root/demo文件夹
  • 域名

  • SSL证书(通过上一篇博客获取,获取其他方式获取到的SSL证书)

    • demo.com.cer
    • demo.com.key

三、步骤

3.1 安装Nginx

官方文档:https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/
CentOS

复制代码
sudo yum install epel-release
sudo yum update
sudo yum install nginx
sudo nginx -v
sudo systemctl restart nginx
sudo systemctl enable nginx
sudo systemctl status nginx

Ubuntu

复制代码
sudo apt-get update
sudo apt install nginx
sudo nginx -v
sudo systemctl restart nginx
sudo systemctl enable nginx
sudo systemctl status nginx

3.2 前端代码

前端代码原先位置:/root/demo -> 移动到nginx默认文件夹/usr/share/nginx/html/

复制代码
cp -r /root/demo/* /usr/share/nginx/html/

3.3 HTTP配置

3.3.1 修改Nginx配置

默认配置为/etc/nginx/nginx.conf

复制代码
nano /etc/nginx/nginx.conf

内容修改如下:

复制代码
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    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;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

3.3.2 重载配置

复制代码
sudo systemctl restart nginx
sudo systemctl status nginx

3.3.3 网站访问

浏览器访问http://demo.com即可看到对应页面。
注:请确保域名解析到服务器IP已完成。(该步骤一般在域名购买供应商那里操作,不同供应商操作步骤不同,这里不再描述,网上直接搜索即可)

3.4 HTTPS配置

需要SSL证书(参考 上一篇文章申请)

3.4.1 获取SSL证书

上一篇获取到文件包含如下,该密钥文件可以部署到nginx,通过https访问

复制代码
demo.com.cer (公钥)
demo.com.key (私钥)

3.4.2 移动SSL证书

移动证书文件到指定路径/etc/pki/nginx/

复制代码
cp demo.com.cer /etc/pki/nginx/
cp demo.com.key /etc/pki/nginx/

3.4.3 修改Nginx配置

复制代码
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    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;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  _;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/pki/nginx/demo.com.cer";
        ssl_certificate_key "/etc/pki/nginx/demo.com.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on; 
 

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

3.4.5 重载配置

复制代码
sudo systemctl restart nginx
sudo systemctl status nginx

sudo systemctl restart nginx
sudo systemctl status nginx

3.4.6 网站访问

浏览器访问https://demo.com即可看到对应页面。
注:请确保域名解析到服务器IP已完成。(该步骤一般在域名购买供应商那里操作,不同供应商操作步骤不同,这里不再描述,网上直接搜索即可)


  • 注:接单前端代码的服务器部署,私信或者邮件[email protected]

    • 内容包括完整代码的服务器部署流程

    • 服务器域名的购买(自己提供也可以)

    • 域名映射 + 服务器的前端代码部署 150RMB

    • 也支持:静态页面的书写(如落地页、官方网站、个人简历) 200RMB起步,根据页面和内容多少确定

  • 最后效果: demo.com可访问对应网页

其他

如有问题 欢迎提出
如有疑问 欢迎留言
如有作用 欢迎点赞
如有失效 欢迎留言
个人邮箱:[email protected]
2023年10月 可用

相关推荐
搬码临时工17 分钟前
远程连接电脑的方法?异地远程桌面连接和三方软件实现
运维·服务器·网络·物联网·电脑·远程工作
窦再兴33 分钟前
来一个复古的技术FTP
linux·运维·服务器
梦在深巷、38 分钟前
nginx配置之负载均衡
运维·nginx·负载均衡
iRayCheung1 小时前
Kind方式部署k8s单节点集群并创建nginx服务对外访问
nginx·kubernetes·kind
小黑_深呼吸1 小时前
Prometheus实战教程:k8s平台-Mysql监控案例
运维·学习·kubernetes·prometheus
疯狂的挖掘机2 小时前
记一次从windows连接远程Linux系统来控制设备采集数据方法
linux·运维·windows
忧郁的狐狸2 小时前
Jenkins 安装与配置指南
运维·jenkins
sy_cora2 小时前
IEEE 列表会议第五届机器人、自动化与智能控制国际会议
运维·人工智能·机器人·自动化
数巨小码人3 小时前
Linux常见命令
大数据·linux·运维·服务器·elasticsearch·搜索引擎
magic 2454 小时前
第五章:Linux用户管理
linux·运维·服务器