Apache2 with SSL do I have to copy VirtualHost blocks

apache-2.2mod-ssl

In Apache2 on ubuntu I have my site listening on 80, and now I want to add SSL. Is there a way to enable the SSLEngine for port 443 so I do not have to copy the entire VirtualHost block?

When I do this:

Listen 80
Listen 443
NameVirtualHost *
<VirtualHost *>
  SSLEngine On
  ... a bunch more lines...
</VirtualHost>

It is turning on the SSLEngine for port 80. Is there a way to use only the one VirtualHost block, and only turn on the SSLEngine for port 443? So I can do something like this?

Listen 80
Listen 443
NameVirtualHost *
<VirtualHost *>
   <IfPort 443>
      SSLEngine On
   </IfPort>
   ... a bunch of lines I don't want to copy into another VirutalHost block...
</VirtualHost>

Best Answer

You can't make one vhost do both HTTP and HTTPS, because they are separate vhosts servicing separate protocols. Instead, you should put all of the common configuration into a separate file, and then include that file in both the SSL and non-SSL vhosts for the domain.

Minimal example:

# /etc/apache2/sites-available/example.com
<VirtualHost *:80>
  Include /etc/apache2/domains/example.com
</VirtualHost>

<VirtualHost 192.0.2.1:443>
  SSLEngine On
  SSLCertificateFile /etc/ssl/example.com_crt
  SSLCertificateKeyFile /etc/ssh/example.com_key

  Include /etc/apache2/domains/example.com
</VirtualHost>

# /etc/apache2/domains/example.com
ServerName example.com
ServerAlias www.example.com

ServerAdmin webmaster@example.com
DocumentRoot /home/example/public_html
ErrorLog /home/example/apache/error.log
Related Topic