8

My current httpd.conf file looks something like this:

<VirtualHost *:443>
    DocumentRoot /var/www/html/www.example1.com
    ServerName www.example1.com
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/www.example1.com/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/www.example1.com/private.key
    SSLCACertificateFile  /var/www/ssl/www.example1.com/bundle.crt
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /var/www/html/www.example2.com
    ServerName www.example2.com
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/www.example2.com/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/www.example2.com/private.key
    SSLCACertificateFile  /var/www/ssl/www.example2.com/bundle.crt
</VirtualHost>

I am hoping to be able to store the "ServerName" in some type of variable to be able to simplify the file (to something like this):

Define server_name = ServerName

<VirtualHost *:443>
    DocumentRoot /var/www/html/${server_name}
    ServerName ${server_name}
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/${server_name}/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/${server_name}/private.key
    SSLCACertificateFile  /var/www/ssl/${server_name}/bundle.crt
</VirtualHost>

Is there any way I can do something like this? I looked around and could not find any solutions that would help with this but I could be taking the wrong approach.

1
  • An interesting question, clearly expressed; +1 from me! – MadHatter Nov 20 '13 at 7:32
9

try mod_macro

Here is an example:

## Define a VHost Macro for repetitive configurations

<Macro VHost $host $port $dir>
  Listen $port
  <VirtualHost *:$port>

   ServerName $host
   DocumentRoot $dir

   # Public document root
   <Directory $dir>
       Require all granted
   </Directory>

   # limit access to intranet subdir.
   <Directory $dir/intranet>
     Require ip 10.0.0.0/8
   </Directory>
  </VirtualHost>
</Macro>

## Use of VHost with different arguments.

Use VHost www.apache.org 80 /vhosts/apache/htdocs
Use VHost example.org 8080 /vhosts/example/htdocs
Use VHost www.example.fr 1234 /vhosts/example.fr/htdocs
1
  • This seems to be the best way to simplify my conf file how I want it. Great suggestion. – RonSper Nov 21 '13 at 1:53
0

I'm thinking maybe you could create a config file with your variable declarations, include it in the httpd.conf file, and then run a cron job to change the server name to whatever is currently the server name using sed.

Example cron job:

set servername = hostname
sed -i -e 's/(?-s)define servername .+/define servername $servername/g' /etc/httpd/conf.d/variables.conf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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