本文介绍如何为CentOs下Apache服务器上的网站配置SSL证书。包括两种情况:
1)为在Apache上运行的网站配置SSL;
2)为Apache反向代理的Tomcat网站配置SSL。
实现以上两项后,本文也自然包含了为Apache下多个VirtualHost分别配置SSL的方法。
Apache如何反向代理Tomcat 参见
小白指南:CentOs下配置Apache 反向代理Tomcat
环境:
服务器:阿里云CentOs7
Apache version :2.6.4
Tomcat version: 8.5.45
前提
待配置ssl的网站可以正常访问。
1、购买并申请SSL证书
通过阿里云SSL证书服务购买并申请免费SSL证书,每个SSL证书对应一个二级域名。我购买了两个:
www.test.cn:对应Apache上的网站。
level2.test.cn:对应Tomcat上的网站。
下载SSL相关文件下载到本地,是一个压缩包,包内有三个文件(以www.test.cn为例):
1234567_www.test.cn.key
1234567_www.test.cn_chain.crt
1234567_www.test.cn_public.crt
2、安装ssl模块
默认Apache是没有安装SSL模块的,使用命令安装:
yum install -y mod_ssl
3、打开443端口
在阿里云ECS控制台上配置安全规则组,监听443端口。
4、上传ssl文件
服务器 httpd 目录下新建子目录ssl:
mkdir ssl
把三个SSL文件上传到ssl目录。如何上传看 这里第3.2小节。
5、配置www.test.cn的SSL
1)修改ssl.conf文件
vi /etc/httpd/conf.d/ssl.conf
找到下面内容,把 DocumentRoot 和 ServerName 改成自己网站的对应内容
<VirtualHost _default_:443>
# General setup for the virtual host, inherited from global configuration
DocumentRoot "/var/www/html/test"
ServerName www.test.cn:443
继续向下,找到这三行:修改为自己的对应文件路径和名称。
SSLCertificateFile /etc/httpd/ssl/1234567_www.test.cn_public.crt
……
SSLCertificateKeyFile /etc/httpd/ssl/1234567_www.test.cn.key
……
SSLCertificateChainFile /etc/httpd/ssl/1234567_www.test.cn_chain.crt
……
修改完毕后保存并退出。到此配置就完成了,不过我们可能还希望当用户用http://访问网站时,自动跳转到https访问。
2)http强制跳转https
修改vhost-name.conf
vi /etc/httpd/vhost-conf.d/vhost-name.conf
找到:
<VirtualHost *:80>
DocumentRoot /var/www/html/test
ServerName http://www.test.cn
</Virtualhost>
<Directory /var/www/html/test>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
添加三行内容
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
修改完毕后如下:
<VirtualHost *:80>
DocumentRoot /var/www/html/test
ServerName http://www.test.cn
</Virtualhost>
<Directory /var/www/html/test>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
</Directory>
保存修改并退出。
重新启动Apache。浏览器输入www.test.cn可以正常访问到https://www.test.cn。
6、配置level2.test.cn的SSL
1)上传文件参见第4步。
2)修改ssl.conf文件
vi /etc/httpd/conf.d/ssl.conf
a. 复制<VirtualHost _default_:443>
中的所有内容并在文件末尾粘贴。
:set number
:16,217 copy 218
上面命令功能:显示行号,把第16到217行复制到第218行。
把复制的<VirtualHost _default_:443>
修改为<VirtualHost *:443>
,这将是level2.test.cn的配置内容。
b.找到 DocumentRoot 和 ServerName :
<VirtualHost *:443>
# General setup for the virtual host, inherited from global configuration
#DocumentRoot "/var/www/html/test"
ServerName level2.test.cn:443
ProxyPreserveHost On
ProxyPass / http://tomcatIp:8080/
ProxyPassReverse / http://tomcatIp:8080/
ProxyPreserveHost On
ProxyPass /project-war-name http://tomcatIp:8080/project-war-name
ProxyPassReverse /project-war-name http://tomcatIp:8080/project-war-name
……
修改的内容包括:
注释DocumentRoot;
修改ServerName 为level2.test.cn:443;
新增6行内容。
c.继续向下,找到这三行:修改为自己的对应文件路径和名称。
SSLCertificateFile /etc/httpd/ssl/7654321_level2.test.cn_public.crt
……
SSLCertificateKeyFile /etc/httpd/ssl/7654321_level2.test.cn.key
……
SSLCertificateChainFile /etc/httpd/ssl/7654321_level2.test.cn_chain.crt
……
保存修改并退出。
3)http强制跳转https
参见5.2),修改完全相同。
重新启动Apache。
浏览器输入level2.test.cn可以正常访问https:/level2.test.cn。
浏览器输入level2.test.cn/project-war-name可以正常访问https:/level2.test.cn/project-war-name。
7、参考文章
https://blog.csdn.net/u010071211/article/details/80106640
https://blog.csdn.net/zy517863543/article/details/82027567
https://www.jb51.net/article/143599.htm
That’s all.Thank you!