linux中自建证书搭建https

前言

         搭建https有两种方式,分为单向认证和双向认证。单向认证就是传输的数据加密过了,但是不会校验客户端的来源,也就只有客户端验证服务端证书。


搭建https

 1.生成单向认证的https证书

建立服务器私钥,生成RSA秘钥。过程中会要求输入密码,记住你输入的密码。(如:123456)
[root@iZ23f31fmtgZ certificate]# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...................................................................+++
....................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
[root@iZ23f31fmtgZ certificate]# ls
server.key

生成一个证书请求,涉及到密码就输入之前输入过的密码即可。
[root@iZ23f31fmtgZ certificate]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:ZHEJIANG
Locality Name (eg, city) [Default City]:HANGZHOU
Organization Name (eg, company) [Default Company Ltd]:BEYONDSOFT
Organizational Unit Name (eg, section) []:ODC
Common Name (eg, your name or your server's hostname) []:ODC
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:
[root@iZ23f31fmtgZ certificate]# ls
server.csr  server.key
输入完这些内容,会生成一个server.csr文件
[root@iZ23f31fmtgZ certificate]# cp server.key server.key.org
[root@iZ23f31fmtgZ certificate]# ls
server.csr  server.key  server.key.org
对于上面的秘钥进行ssl加密
[root@iZ23f31fmtgZ certificate]# openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key

使用上面的秘钥和CSR对正式进行签名
[root@iZ23f31fmtgZ certificate]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=CN/ST=ZHEJIANG/L=HANGZHOU/O=BEYONDSOFT/OU=ODC/CN=ODC
Getting Private key
[root@iZ23f31fmtgZ certificate]# ls
server.crt  server.csr  server.key  server.key.org
这样证书就建好了。。

然后在nginx中修改server就可以实现https了
server {
    	listen  443;
        server_name www.beyondsofthz.com;
        ssl on;
        ssl_certificate /home/default/certificate/ca.cer;
        ssl_certificate_key /home/default/certificate/server.keystore;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5; 
        ssl_prefer_server_ciphers   on;
    }

2. 生成双向证书

      略。


参考文章:
使用keytool生成证书    

http://www.cnblogs.com/beiyan/p/6248187.html





你可能感兴趣的:(linux,https)