Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 9 down vote accepted

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
share|improve this answer
    
Can you give me a short example of what the file should look like? Does it need a VirtualHost wrapper, or should I just move all the lines to it without any wrapper? –  dar Nov 11 '09 at 16:31
1  
I've added an example to my answer. –  womble Nov 11 '09 at 16:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.