Apache Wild-Card Development Sites

I work on a Ubuntu machine and the easiest way to get a LAMP development environment working was for me to just install Apache, PHP and MySQL. I want my sites to be testable locally with a *.dev.test domain.

So if I visit sherriw.dev.test I want it to resolve to a sherriw/ directory in one place (for example /home/dev/projects/).

Here’s how to get Apache to resolve wildcards:

<VirtualHost *:80>

    # INFO: This configuration works for any *.dev.test local domains. Requires apache module vhost_alias.

    ServerName dev.test
    ServerAlias *.dev.test
    VirtualDocumentRoot /home/dev/projects/%1/public

    DirectoryIndex index.html index.php

    # Send 404 errors back to index.php for handling by the framework.
    FallbackResource /index.php

    <Directory />
            Options Indexes FollowSymLinks
            Require all granted
    </Directory>

	LogLevel warn

	ErrorLog ${APACHE_LOG_DIR}/error-projects.log
	CustomLog ${APACHE_LOG_DIR}/access-projects.log combined
</VirtualHost>

This file is found in /etc/apache2/sites-available/projects-dev.conf.

You still need to add entries to your /etc/hosts file for each domain you’ll be resolving:

127.0.0.1       project1.dev.test
127.0.0.1       my-project.dev.test
127.0.0.1       experimental.dev.test

I actually have a bunch of symlinks in /home/dev/projects/ which link to my actual project repos in various places on my hard drive.

Note that these projects must all have a public/ directory in this example.

Leave a Reply

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