Apache2 VirtualHost IfPort?

I use mod_macro to solve this issue on a server that hosts a ton of different domains... Install the module (differs per OS/Distro), then configure something like this:

LoadModule macro_module         libexec/apache22/mod_macro.so

<Macro VHost $host>
        <VirtualHost *:80>
                DocumentRoot /usr/local/www/$host/data
                ServerName $host
                ServerAlias *.$host

                ScriptAlias /cgi-bin/ "/usr/local/www/$host/cgi-bin/"
                IncludeOptional etc/apache22/vhosts/$host
        </VirtualHost> 
</Macro>

<Macro VHostSSL $host>
        <VirtualHost *:80>
                DocumentRoot /usr/local/www/$host/data
                ServerName $host
                ServerAlias *.$host

                ScriptAlias /cgi-bin/ "/usr/local/www/$host/cgi-bin/"
                IncludeOptional etc/apache22/vhosts/$host
        </VirtualHost>
        <VirtualHost *:443>
                DocumentRoot /usr/local/www/$host/data
                ServerName $host
                ServerAlias *.$host

                SSLEngine on
                SSLCertificateFile /usr/local/www/$host/ssl/$host.crt
                SSLCertificateKeyFile /usr/local/www/$host/ssl/$host.key

                ScriptAlias /cgi-bin/ "/usr/local/www/$host/cgi-bin/"
                IncludeOptional etc/apache22/vhosts/$host
        </VirtualHost> 
</Macro>

Use VHostSSL example.com 
Use VHost    example.net

Super easy to add a new domain; any domain specific configurations get tossed in the include file.


using John's solution i get this

Apache 2 is starting ...
AH00526: Syntax error on line 53 of .../httpd-vhosts.conf:
SSLEngine not allowed here

as John said,the best way is to have 2 virtual hosts ;but my virtual host code was more than 150 lines (lots of reverse proxies) as i didn't want to have 2 of each code (and a very long config file) i ended up doing this which works:

1.Create a virtual host for non-ssl virtual host.

2.Create another virtual host and reverse proxy to the first virtual host

<VirtualHost *:443>
    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile "...cert.crt"
    SSLCertificateKeyFile "...server.ssl.key"
    ... (any ssl specific config)

    ProxyPreserveHost On
    ProxyPass / http://localhost:80/
    ProxyPassReverse http://localhost:80/ http://yourdomain.com/
</VirtualHost>

this is not at all a good or performance friendly solution but if the reason behind not wanting 2 virtual hosts is to prevent having 2 verions of all virtual host configs (which means changing 2 lines each time you want to change something) this works.


Another option to avoid duplication would be to keep the virtualhost config in a specific file, and pull it in with an include:

/etc/path/to/config/example.com.conf:

ServerName example.com.conf
DocumentRoot /var/www/something
# Any other config you want to apply to both vhosts

And your virtual hosts file:

<VirtualHost *:443>
    SSLEngine on
    # Other SSL directives
    Include /etc/path/to/config/example.com.conf
</VirtualHost>
<VirtualHost *:80>
    Include /etc/path/to/config/example.com.conf
</VirtualHost>