2, 首先安装https模块 yum install mod_ssl
3. 配置Apache Web服务器
首先,修改下面的配置文件。仅需配置红色部分 SSLCertificateFile 和 SSLCertificateKeyFile
# vim /etc/httpd/conf.d/ssl.conf
#
# When we also provide SSL we have to listen to the
# the HTTPS port in addition.
#
Listen 443 https
##
## SSL Virtual Host Context
##
<VirtualHost _default_:443>
# General setup for the virtual host, inherited from global configuration
DocumentRoot "/var/www/html/a.com"
ServerName www.a.com:443
### overwrite the following parameters ###
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key
### The following parameter does not need to be modified in case of a self-signed certificate. ###
### If you are using a real certificate, you may receive a certificate bundle. The bundle is added using the following parameters ###
SSLCertificateChainFile /etc/pki/tls/certs/example.com.ca-bundle
然后,重新启动httpd服务使更改生效
# systemctl restart httpd // 或者 /etc/init.d/httpd restart
Web服务器现在可以使用HTTPS
4. 调整虚拟主机
Apache Web服务器可以配置为多个Web站点。这些站点在httpd的配置文件中以虚拟主机的形式定义。例如,让我们假设我们的Apache Web服务器托管站点为www.a.com,网站所有的文件都保存在/var/www/html/a.com/目录。
对于虚拟主机,典型的HTTP配置是这样的。
# vim
# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost *:80>
ServerName www.a.com
Redirect permanent / https://www.a.com/
DocumentRoot "/var/www/html/a.com"
<Directory "/var/www/html/a.com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key
<Directory /var/www/html/virtual-web>
AllowOverride All
</Directory>
ServerAdmin email@example.com
DocumentRoot /var/www/html/a.com
ServerName www.a.com
</VirtualHost>
需要按照上面的配置,定义每个虚拟主机。添加虚拟主机后,重新启动Web服务。
# systemctl restart httpd
现在的虚拟主机就可以使用HTTPS
5. 强制Apache Web服务器始终使用https
如果由于某种原因,你需要站点的Web服务器都只使用HTTPS,此时就需要将所有HTTP请求(端口80)重定向到HTTPS(端口443)。 Apache Web服务器可以容易地做到这一点。
1,强制主站所有Web使用(全局站点)
如果要强制主站使用HTTPS,我们可以这样修改httpd配置文件:
# vim /etc/httpd/conf/httpd.conf
ServerName www.example.com:80
Redirect permanent / https://www.example.com
重启Apache服务器,使配置生效:
# systemctl restart httpd
2,强制虚拟主机(单个站点)
如果要强制单个站点在虚拟主机上使用HTTPS,对于HTTP可以按照下面进行配置:
# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost *:80>
ServerName www.a.com
Redirect permanent / https://www.a.com/
</VirtualHost>
重启Apache服务器,使配置生效:
# systemctl restart httpd
单个站点全部使用HTTPS,则 http://www.a.com 会强制重定向跳转到 https://www.a.com