centos下配置https服务器

简介:

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

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,如需转载请自行联系原作者
目录
相关文章
|
1天前
|
Linux 网络安全 数据库
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
|
1天前
|
Linux
CentOS 7 配置yum阿里源 (三步即可)
CentOS 7 配置yum阿里源 (三步即可)
|
7天前
|
安全 Go 网络安全
【Go语言专栏】Go语言中的HTTPS与TLS配置
【4月更文挑战第30天】本文介绍了在Go语言中配置HTTPS服务器和处理TLS证书的方法。首先,TLS证书由证书颁发机构(CA)颁发,用于验证服务器身份和加密通信。接着,展示了如何使用`crypto/tls`包加载自签名证书启动HTTPS服务器。在生产环境中,通常使用CA签名的证书,这需要获取证书链和私钥。为了自动续期证书,可以利用Go的`acme/autocert`包与ACME服务交互。掌握这些技能对于确保Web服务的安全至关重要。
|
8天前
|
Web App开发 前端开发 Java
SpringBoot配置HTTPS及开发调试
在实际开发过程中,如果后端需要启用https访问,通常项目启动后配置nginx代理再配置https,前端调用时高版本的chrome还会因为证书未信任导致调用失败,通过摸索整理一套开发调试下的https方案,特此分享
18 0
SpringBoot配置HTTPS及开发调试
|
12天前
|
网络协议 Java 应用服务中间件
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
9 0
|
12天前
|
安全 Linux 网络安全
记录_centos搭建ftp服务器
记录_centos搭建ftp服务器
11 0
|
14天前
|
运维 Apache 数据安全/隐私保护
CentOS6.5搭建SVN服务器(Apache+SVN)
CentOS6.5搭建SVN服务器(Apache+SVN)
|
14天前
|
Linux 网络安全 开发工具
Centos7 sendmail服务安装与配置
该文本描述了在Linux系统中设置邮件服务的步骤。首先,启用httpd的邮件发送功能,然后安装sendmail、sendmail-cf和dovecot。接着配置/sendmail.mc,设定IP和邮件域名。在dovecot配置文件中启用imap、pop3和lmtp协议,取消明文认证限制,设定mail_location,并开启SSL。创建用户mail3和mail4,给予相应权限。停止postfix服务,编辑访问控制、提交配置、本地主机名等文件。最后,重置sendmail、dovecot和saslauthd服务。
36 0
|
14天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置PXE服务
PXE是Intel开发的预启动执行环境,允许工作站通过网络从远程服务器启动操作系统。它依赖DHCP分配IP,DNS服务分配主机名,TFTP提供引导程序,HTTP/FTP/NFS提供安装源。要部署PXE服务器,需关闭selinux和防火墙,安装dhcpd、httpd、tftp、xinetd及相关服务,配置引导文件和Centos7安装源。最后,通过syslinux安装引导文件,并创建pxelinux.cfg/default配置文件来定义启动参数。
52 0
|
2月前
|
前端开发
webpack如何设置devServer启动项目为https协议
webpack如何设置devServer启动项目为https协议
164 0