【问题标题】:How to apply different SSL certificates to different domains on the same IP?如何将不同的 SSL 证书应用于同一 IP 上的不同域?
【发布时间】:2019-01-18 23:17:05
【问题描述】:

如何将不同的 SSL 证书应用于同一 IP、同一服务器和同一虚拟主机上的不同域(我使用的是 apache 2.2)?现在它不工作,需要在 apache 2.2 上使用单个虚拟主机。

你有我正在尝试的东西:

<VirtualHost *:80>
  ServerName main_url.com
  ServerAlias *.main_url.com
  DocumentRoot /app_path
  <Directory "/app_path">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>



NameVirtualHost *:443

<VirtualHost *:443>
  ServerName main_url.com
  ServerAlias *.main_url.com
  DocumentRoot /app_path
  <Directory "/app_path">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
  SSLEngine on
  SSLCertificateFile /certs/main_url_com.crt
  SSLCertificateKeyFile /certs/main_url_com.key
  SSLCertificateChainFile /certs/main_url_com.ca-bundle
</VirtualHost>

<VirtualHost *:80>
  ServerName url_site1.com
  Redirect permanent / https://url_site1.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName url_site1.com
  DocumentRoot /app_path
  <Directory "/app_path">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
  SSLEngine on
  SSLCertificateFile /certs/url_site1.crt
  SSLCertificateKeyFile /certs/url_site1.key
  SSLCertificateChainFile /certs/url_site1.ca-bundle
</VirtualHost>


<VirtualHost *:80>
  ServerName url_site2.com
  Redirect permanent / https://url_site2.dk/
</VirtualHost>

<VirtualHost *:443>
  ServerName url_site2.com
  DocumentRoot /app_path
  <Directory "/app_path">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
  SSLEngine on
  SSLCertificateFile /certs/url_site2.crt
  SSLCertificateKeyFile /certs/url_site2.key
  SSLCertificateChainFile /certs/url_site2.ca-bundle
</VirtualHost>

所有帮助将不胜感激。

谢谢

法比奥

【问题讨论】:

  • 您可能需要检查您的 OpenSSL 安装是否支持 SNI:SSL with Virtual Hosts Using SNI。除此之外,配置非常简单,因为您不需要做任何与基于 ip/port 的设置不同的事情。

标签: apache ssl virtualhost


【解决方案1】:

如果您熟悉服务器配置,这非常容易。请按照下面提到的步骤进行操作,您一定会实现您想要的。

1.您必须为两个域创建两个不同的目录。

mkdir -p /etc/apache2/ssl/example1.com
mkdir -p /etc/apache2/ssl/example2.com

2。接下来您必须激活 SSL 模式

sudo a2enmod ssl
sudo service apache2 restart

3.为第一个域创建自签名 SSL 证书

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/example1.com/apache.key –out /etc/apache2/ssl/example1.com/apache.crt

4.之后填写您询问的详细信息:-

Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:Clifornia
Locality Name (eg, city) []:Los Angeles
Organization Name (eg, company) [Internet Widgits Pty Ltd]:AFffas LLC
Organizational Unit Name (eg, section) []:Dept of marketing
Common Name (e.g. server FQDN or YOUR name) []:example1.com                  
Email Address []:johndoe@example1.com

5.对第二个(example2.com)采取相同的步骤

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/example2.com/apache.key -out /etc/apache2/ssl/example2.com/apache.crt

6.现在需要创建虚拟主机

sudo nano /etc/apache2/sites-available/example1.com
sudo nano /etc/apache2/sites-available/example2.com

接下来打开每个文件并粘贴下面的配置。此配置是两个独立配置文件的简化版本:位于 /etc/apache2/sites-available/default 的默认虚拟服务器配置文件和位于 /etc/apache2/sites-available/default-ssl 的默认 SSL 配置。

此配置包含促进多个 SSL 证书的重要更改。而默认 SSL 配置具有以下行,将证书指定为服务器的默认证书,

<VirtualHost _default_:443>

下面的配置没有对默认证书的引用。这是关键。

总体而言,默认配置文件提供了各种有用的指令和其他配置选项,您可以将它们添加到虚拟主机中。但是,以下信息将为服务器提供在一个 IP 地址上设置多个 SSL 证书所需的一切

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName example1.com
        DocumentRoot /var/www

</VirtualHost>


<IfModule mod_ssl.c>
<VirtualHost *:443>

        ServerAdmin webmaster@localhost
        ServerName example1.com
        DocumentRoot /var/www

        #   SSL Engine Switch:
        #   Enable/Disable SSL for this virtual host.
        SSLEngine on

        #   A self-signed (snakeoil) certificate can be created by installing
        #   the ssl-cert package. See
        #   /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
        #   If both key and certificate are stored in the same file, only the
        #   SSLCertificateFile directive is needed.
        SSLCertificateFile /etc/apache2/ssl/example1.com/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/example1.com/apache.key
</VirtualHost>

</IfModule>

这些配置文件中有几行需要自定义。

ServerAdmin:这只是您网站管理员的电子邮件地址 ServerName:这是您的域名。确保在没有前缀 www 的情况下写入它。 DocumentRoot: 这是您保存站点信息的目录。目前它指向 apache 默认目录。对于 2 个不同的虚拟主机,您可能会有不同的服务器根目录。 SSLCertificateFile: 该指令指向证书文件的位置。每个站点的证书都存储在我们在本教程前面创建的目录中。 SSLCertificateKeyFile : 该指令指向证书密钥的位置。每个站点的证书密钥都存储在我们在本教程前面创建的目录中。 设置两个域的配置。在单独的 SSL 证书在两台服务器上都可以使用之前,我们还有更多的步骤。

7.编辑 ports.conf 文件 确保多个证书在一个 VPS 上工作所需的最后一步是告诉服务器侦听端口 443。将粗体行添加到 apache 端口配置文件中。

sudo nano /etc/apache2/ports.conf 


NameVirtualHost *:80
NameVirtualHost *:443

Listen 80

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to 
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

8.激活虚拟主机

sudo a2ensite example1.com
sudo a2ensite example2.com

然后重启apache

sudo service apache2 restart

现在您应该可以访问两个站点,每个站点都有自己的域名和 SSL 证书。

【讨论】:

  • 如果您能解释提问者在他的设置中出错的地方会很有帮助。
  • 您好!我认为提问者需要让服务器在端口 443 上侦听才能在同一台服务器上使用多个证书。请查看第 7 步
猜你喜欢
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 2021-10-10
相关资源
最近更新 更多