且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

centos下配置https服务器

更新时间:2022-09-20 12:12:18

以前一直没有了解,今天花了点时间搞了搞,没什么难度。

web服务器用的是nginx。


首先需要确认一下nginx是否安装了SSL模块,如下的命令:

1
nginx -V

显示的结果中如果包含

1
--with-http_ssl_module

那么安装就很简单了,否则还需要编译nginx将ssl模块编进去,这里就不详述了,google一下就能找到


接下来,需要将nginx的ssl模块启用。我的nginx版本是1.2.6,在安装完之后,它给出了一个示例文件。

1
2
cd/etc/nginx/conf.d/
ls

发现其下有一个example_ssl.conf文件。打开一看,全都注释掉了,我们把注释都打开,改成如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# HTTPS server
#
server {
listen       443;
server_name  demo;
ssl                  on;
#ssl_certificate      /etc/nginx/cert.pem;
ssl_certificate     /etc/nginx/cert.crt;
ssl_certificate_key /etc/nginx/cert.key;
ssl_session_timeout  5m;
ssl_protocols  SSLv2 SSLv3 TLSv1;
ssl_ciphers  HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers   on;
location / {
root  /data/www;
index  index.html index.htm index.php;
}
location ~ \.php$ {
root          /data/www;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
include        fastcgi_params;
}
}

这里有两个文件需要说明一下,它们一个是cert.crt,另一个是cert.key

这两个都是跟认证有关的文件,我们暂时还没有。由于是内网的测试环境,我们可以自己去生成这两个文件。


生成的办法如下:


1.进入到要生成文件的目录,然后使用openssl创建服务器私钥


1
2
cd /etc/nginx/
openssl genrsa -des3 -out cert.key1024


这时,会提示:

1
2
3
4
5
6
Generating RSA private key, 1024 bit long modulus
......++++++
............................................................++++++
e is 65537 (0x10001)
Enter pass phraseforcert.key:
Verifying - Enter pass phraseforcert.key:

写两次密码,然后我们再ls一下看看,就会发现在/etc/nginx下已经生成了cert.key文件了。


2.接下来要生成cert.csr文件也就是签名请求的证书。这个稍微麻烦一点。


先执行如下的命令:

1
openssl req -new -key cert.key -out cert.csr


会给出如下的提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Enter pass phraseforcert.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) [Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:demo
Organizational Unit Name (eg, section) []:localhost
Common Name (eg, your name or your server'shostname) []:localhost
Email Address []:demo@abc.com
Please enter the following'extra'attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

注意,冒号后面是需要我们填写的,最后两个我都没有填。按理,只要是带空的中括号的似乎都可以选填。

上面的执行完后,再ls一下,会发现出现了一个cert.csr文件。


好吧,我们干到一半了。再往下走。


3.制作解密后的私钥

先将cert.key文件复制一份为cert.key.org

1
cpcert.key cert.key.org


然后再执行如下的命令:

1
openssl rsa -incert.key.org -out cert.key

这时会出现如下的提示:

1
Enter pass phraseforcert.key.org:

输入之前我们设置的密码后,会提示:

1
writing RSA key


4.接下来,最后一步,用cert.csr和cert.key生成cert.crt文件

执行如下的命令:

1
openssl x509 -req -days 365 -incert.csr -signkey cert.key -out cert.crt

如果有如下的提示就说明成功了:

1
Signature ok

再ls一下,会发现cert.crt文件已经生成


此时重启一下nginx:

1
service nginx restart


好吧,不出意外,一切就完事了。


绑定一下demo域名到127.0.0.1,然后在浏览器中访问https://demo应该就能看到效果了。


参考:http://blog.51yip.com/manual/nginx/











本文转自 ustb80 51CTO博客,原文链接:http://blog.51cto.com/ustb80/1339048,如需转载请自行联系原作者