Apache Vhost Macro / Variables

[update: 2020-05-20] There does seem to be a conflict with Let’s Encrypt certbot. You’ll need to manually run updates. I’ll update once I figure it out, how to do it more automated. Looks like possible race condition for the ghost, and then the SSL… but since you’re defining them by macro’s it’s not so bad.

steps seem to be:

  1. DNS to point to the server
  2. Create vhost only (not SSL yet), and ensure it works.
  3. Certbot certificate only
  4. Add Use VhostSSL $domain line.
  5. Renewals are certificates only…? <– untested

[Original Post] I have had to manage my share of vhosts, and it’s the same vhosts all the time. Maybe slight changes. They usually look something like this:

<VirtualHost *:80>
    ServerName domain.tld
    ServerAlias www.domain.tld

    DocumentRoot /var/www/project/public
    <Directory /var/www/project/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

But repeated for EVERY domain name on the server. There’s an easier way, since apache 2.4.5.

You define the template macro:

<Macro VhostMacro $domain>
<VirtualHost *:80>
    ServerName $domain
    ServerAlias www.$domain

    DocumentRoot /var/www/$domain/public
    <Directory /var/www/$domain/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>

    ErrorLog /var/log/apache2/$domain_error.log
    CustomLog /var/log/apache2/$domain_access.log combined
</VirtualHost>
</Macro>

You call the macro:

Use VhostMacro lloydleung.com
Use VhostMacro site2.tld
Use VhostMacro something.com

You can extrapolate as much as you want.

Remember to restart/reload apache to load the new configs. Otherwise apache won’t know.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *