如何为Apache中的其他端口设置SSL证书?

简单的问题,我只是想知道如何在Web服务器的其他端口中安装SSL证书。 我正在尝试使Web应用程序能够拥有有效的SSL证书。 我用的是apache2。 我已经尝试编辑虚拟主机文件。 我甚至不知道我在做什么。

您在apache的/etc/apache2/ports.conf进行了修改,以通知apache监听这些不同的端口:

 Listen 8080  Listen 446  

步骤将是:

  1. 创建SSL证书:

    • 制作目录以添加证书:

       mkdir -p /etc/apache2/ssl/example.com 
    • 创建自签名证书:

       sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/example.com/apache.key –out /etc/apache2/ssl/example.com/apache.crt 
  2. 使用以下sudo a2enmod ssl启用ssl模块: sudo a2enmod ssl

  3. 使用sudo nano /etc/apache2/sites-available/example.confVirtualhost文件(称为example.conf)中创建条目

      ServerAdmin webmaster@localhost ServerName example.com DocumentRoot /var/www/html    ServerAdmin webmaster@localhost ServerName example.com DocumentRoot /var/www/html # 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/example.com/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/example.com/apache.key   
  4. 通过将端口添加到/etc/apache2/ports.conf文件,告诉apache监听新端口:

     Listen 8080  Listen 446   Listen 446  
    • 这告诉apache在端口446上侦听SSL流量,而不是443
  5. 启用配置文件:

     sudo a2ensite example 
  6. 重启apache:

     sudo systemctl restart apache2 

首先你应该阅读这些答案:

  • 如何设置其他VirtualHost
  • 将phpMyAdmin端口从80更改为另一个号码
  • 如何创建和启用Let的加密HTTPS证书

基于以上答案,步骤如下:

  • 创建一个专用于其他端口的新VirtualHost配置文件。 我们假设这是端口99 ,配置文件名是https-99.conf

     sudo nano /etc/apache2/sites-available/https-99.conf 

    https-99.conf的内容应如下所示:

      Listen 99  ServerName www.example.com DocumentRoot /var/www/html-99  Options None FollowSymLinks AllowOverride None # To enable .htaccess Overrides: AllowOverride All DirectoryIndex index.html index.php Order allow,deny Allow from all Require all granted  ErrorLog ${APACHE_LOG_DIR}/https-99.error.log CustomLog ${APACHE_LOG_DIR}/https-99.access.log combined SSLEngine on SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem   

    复制以上内容并在nano使用: Shift + Insert for paste; Ctrl + OEnter进行保存; 按Ctrl + X退出。

  • 启用配置文件:

     sudo a2ensite https-99.conf 
  • 生成Let的加密证书文件:

     sudo letsencrypt --apache certonly --rsa-key-size 4096 --email email@example.com -d www.example.com 

    其中email@example.comwww.example.com必须是真实的。

  • 将端口99打开到防火墙:

    • 如果您使用UFW,您可以通过以下命令执行此操作: sudo ufw allow 99/tcp

    • 如果您使用IPTables : sudo iptables -A INPUT -p tcp -m tcp --dport 99 -j ACCEPT

  • 创建DocumentRoot目录:

     sudo mkdir /var/www/html-99 
  • DocumentRoot目录中放入一些简单的内容:

     echo 'Hello!!!' | sudo tee /var/www/html-99/index.html 
  • 重新加载Apache的配置:

    • Ubuntu 14.04: sudo service apache2 reload
    • Ubuntu 16.04: sudo systemctl reload apache2.service
  • 尝试通过浏览器打开https://www.example.com:99 。 结果应该是:

    在此处输入图像描述