[原创] 多个域名配置不同的SSL证书访问同一VPS

以 CentOS7 Apache 为例,简述下如何为多个域名配置不同的SSL证书并访问同一VPS。

前提:
需要提前准备好 SSL 证书文件(自签名证书和认证的证书都可以,示例中的 SSL 证书,来自 Let's Encrypt)。

下面的示例实现了,以 HTTPS 方式,使用 ho1ho.com 和 50d.win 这两个域名,都可以访问相同的网站。

在 /etc/httpd/conf.d 下新建配置文件:50d.win.conf
vim /etc/httpd/conf.d/50d.win.conf

内容如下:
<VirtualHost *:443>
        ServerName www.50d.win
        ServerAlias 50d.win *.50d.win
        DocumentRoot /var/www/html

        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/50d.win/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/50d.win/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/50d.win/chain.pem
</VirtualHost>

另外再新建一个:ho1ho.com.conf
vim /etc/httpd/conf.d/ho1ho.com.conf

内容如下:
<VirtualHost *:443>
        ServerName www.ho1ho.com
        ServerAlias ho1ho.com *.ho1ho.com
        DocumentRoot /var/www/html

        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/ho1ho.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/ho1ho.com/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/ho1ho.com/chain.pem
</VirtualHost>

若需要将 HTTP 全部转向 HTTPS,可以添加如下转发规则:例如,修改 50d.win.conf 配置文件,追加如下代码:
<VirtualHost *:80>
        ServerName www.50d.win
        #ServerPath /domain
        ServerAlias 50d.win *.50d.win

        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

注意:如果默认站点的 SSL 配置,是在 /etc/httpd/conf.d/ssl.conf 文件中进行配置的话,那么还需要在此文件中明确指定 ServerName,否则在访问网站时可能会出现 SSL 配置出错的提示。例如,假设默认站点的 ServerName 是 www.ho1ho.com:
<VirtualHost _default_:443>
# General setup for the virtual host, inherited from global configuration
#DocumentRoot "/var/www/html"
#ServerName www.example.com:443
DocumentRoot "/var/www/html"
ServerName www.ho1ho.com
ServerAlias ho1ho.com *.ho1ho.com


参考文章:
  • https://www.rosehosting.com/blog/how-to-set-up-multiple-ssl-certificates-on-a-centos-vps-with-apache-using-one-ip-address/
  • https://httpd.apache.org/docs/2.4/vhosts/examples.html
  • http://stackoverflow.com/questions/12339044/how-to-run-multiple-sites-on-one-apache-instance

你可能感兴趣的:(apache,centos,ssl,https)