Conditional VirtualHost in Apache
I usually spin up a new project directory and PHPStorm project each time I have to improve or fix something in a Drupal contrib extension. Then after I finished the task (and especially if I’m running out of storage), I just simply delete these project directories, without cleaning up the corresponding virtual hosts in my local Apache configuration.
This obviously causes issues on httpd
service [re]starts, but I have learned to tolerate that I have a couple of messages logged to my console. Like this one:
AH00112: Warning: DocumentRoot [/Users/zoli/projects/foo/workroom/public_html] does not exist
Yes, this also means that my server log is never ever empty. I only delete the virtual hosts of missing projects if I need to debug Apache 🙃. But I try to be more aware of my laziness. From Apache 2.4.34, there is a directive that might help in such cases – this is <IfFile>
. If you are using it (I try to do so) then you won’t pollute your log unnecessarily.
Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
<IfFile /Users/zoli/projects/foo/dist/foo/public_html/index.php>
<VirtualHost *:80>
DocumentRoot "/Users/zoli/projects/foo/dist/foo/public_html"
ServerName test.foo.localhost
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/Users/zoli/projects/foo/dist/foo/public_html"
ServerName test.foo.localhost
SSLEngine on
SSLCertificateFile "/Users/zoli/projects/foo/ssl/test.foo.localhost.crt"
SSLCertificateKeyFile "/Users/zoli/projects/foo/ssl/test.foo.localhost.key"
</VirtualHost>
</IfFile>