Linux – Unable to visit virtual host over port 443

apache-2.4google-compute-enginelinuxvirtualhost

I have a Google Compute instance (Debian 8 and Apache) running using Google Compute protocol forwarding to be able to use multiple external IP addresses with one virtual machine instance:

Google Compute Protocol Forwarding (full article)

Google Compute Engine supports Protocol Forwarding, which lets you
create forwarding rule objects that can send packets to a non-NAT’ed
target instance. Each target instance contains a single virtual
machine instance that receives and handles traffic from the
corresponding forwarding rules.

The Problem

I am not able to visit one of the virtual host domains/IP's over port 443. I need this, because I want to enable HTTPS on all these domains.

I have tried multiple options to achieve opening port 443, but none of them works:

Option 1) Virtual name-based hosts:

/etc/apache2/ports.conf

Listen 80

<IfModule ssl_module>
    NameVirtualHost *:443
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    NameVirtualHost *:443
    Listen 443
</IfModule>

/etc/apache2/sites-enabled/example.com.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/website1
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/website1
</VirtualHost>

Option 2) Virtual IP-based hosts:

/etc/apache2/ports.conf

Listen 80

<IfModule ssl_module>
    NameVirtualHost *:443
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    NameVirtualHost *:443
    Listen 443
</IfModule>

<VirtualHost 192.0.2.5>
 DocumentRoot /var/www/website1
 <Directory /var/www/website1>
  Require all granted
 </Directory>
</VirtualHost>

...

Both options work for port 80, but not for port 443.

Best Answer

In ports.conf you need

Listen 443

and in /etc/apache2/sites-enabled/example.com.conf you need:

SSLEngine On
SSLCertificateFile      /path/to/file.pem
SSLCertificateKeyFile /path/to/file.key

where file.pem and file.key are certificate and key

Related Topic