使用OpenSSL生成自签名证书

326 阅读3分钟

简介

现在如Chrome浏览器等都要求使用https协议来访问网站,一些对硬件设备的访问也需要使用SSL加密访问。除了外网域名使用https需要购买ssl服务外,一般本地建立的域名需要用到SSL访问的时候,我们可以采用OpenSSL在服务器端生成安全证书和私钥,再在web服务器中进行简单配置即可。

环境

本次测试的服务器使用Suse Linux 15 SP3,已经安装了OpenSSL程序,如果没有安装请自行搜索安装。

步骤1:创建SSL证书

https的安全证书其实是一对公钥和私钥,生成的公钥和私钥分别放在服务器不同的目录下。 在服务器上执行下面命令:

openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/ssl/private/coc_hh_loc.key -out /etc/ssl/certs/coc_hh_loc.crt

  • openssl: 是创建、管理证书(公钥)和私钥及其他文件的命令。
  • req: 请求创建一个x509协议的签名证书 certificate signing request (CSR)。
  • -x509 创建一个x509协议的签名证书。
  • -node 产生安全证书时不需要创建密码,以便在使用时不需要介入。
  • -days 3650 安全证书的有效期,3650是10年,如果是外网使用,一般是365天。
  • -newkey rsa:2018 使用rsa 2048位来创建密钥。
  • -keyout: 指定密钥保存的目录,此处是保存在/etc/ssl/private/下,私钥名称为coc_hh_loc.key。
  • -out 是证书即公钥的保存目录,此处存放于/etc/ssl/certs,证书名称为:coc_hh_loc.crt 在创建过程中,openssl会要求回答这些问题,例如:

Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Guangdong
Locality Name (eg, city) []:Guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Hh Group Limited Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:coc.hh.loc
Email Address []:w@163.com

步骤2:配置Ningx使用SSL

Ningx的配置比较简单,打开nginx.conf文件,按如下方法修改:

  • 修改http server,将http 80端口强制转发到http 443商品

rewrite ^(.*)https:// https://host1permanent;或者return302https://1 permanent; 或者 return 302 https://server_name$request_uri;

  • 修改 https server,将私钥及公钥证书配置

ssl_certificate /etc/ssl/certs/coc_huihai_loc.crt; ssl_certificate_key /etc/ssl/private/coc_huihai_loc.key;

  • 完整的nginx.conf配置文件如下:
# http 服务配置
server {
        listen       80;
        server_name  coc.hh.loc;
        # redirect to ssl server
        # return 302 https://$server_name$request_uri;
        
        # 80端口强制转发到443端口
        rewrite ^(.*)$ https://$host$1 permanent;

        charset utf-8;

        access_log  /var/log/nginx/host.access.log  main;

        location / {
            root    /srv/www/hh;
            try_files $uri $uri/ /index.html;
            index  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   /srv/www/hh/;
        }
    }
    
    # HTTPS 服务
    server {
        listen       443 ssl;
        server_name  coc.hh.loc;
        
        # 证书的存放位置
        # coc_hh_loc.crt Openssl生成的证书/公钥
        # coc_hh_loc.key Openssl生成的私钥
        ssl_certificate      /etc/ssl/certs/coc_hh_loc.crt;
        ssl_certificate_key  /etc/ssl/private/coc_hh_loc.key;

    # Allow TLS version 1.2 only, which is a recommended default these days
    # by international information security standards.
        ssl_protocols        TLSv1.2;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /srv/www/hh/;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
    }

步骤3:防火墙设置

如果服务器上启动了防火墙,需要将80和443端口的访问权限打开,例如我的服务器上使用firewalld防火墙服务,永久开启80和443端口的访问权限:

firewall-cmd --permanent --zone=public --add-service=https firewall-cmd --permanent --zone=public --add-service=http

步骤4:Nginx设置生效

首先检查一下nginx.conf配置文件是否有错误

nginx -t 如果一切没有问题,会显示如下: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 然后重启Nginx systemctl restart nginx

访问测试

浏览器访问coc.hh.loc,或者https://coc.hh.loc…:

ssl server visit0.png

点击“高级”,再点击“继续访问”,浏览器会使用https来访问您本地的网站。 ssl server visit.png

结论

现在你已经使用加密ssl协议来访问你的网站了,可以避免使网站的安全受到威胁。