Set up Automatic Virtual Hosts with Nginx and Apache

Setting up for Apache

Apache Logo

In this section, I’ll assume you have a running Apache instance that serves your PHP sites as usual. This could be via PuPHPet or with your friendly neighborhood *AMP stack, irrelevant – as long as it works.

First, we need to make sure mod_vhost_alias is enabled. This is the Apache mod used to give us the functionality we need. There are two ways to do this. In more streamlined installations, the command a2enmod will be available in the terminal, and all it takes issudo a2enmod vhost_alias. In others, again due to the curse of Linux, you’ll need to look inside the main configuration file, either/etc/apache2/apache2.conf or /etc/apache2/httpd.conf or the like. If this is your case, try and find a line containingmod_vhost_alias. If it’s not there, add the following line:

LoadModule vhost_alias_module modules/mod_vhost_alias.so

Note that the path at the end needs to match the location of Apache modules on your system. This also varies due to the curse of Linux, but is often in /etc/apache2/mods-available.

Just like Nginx, Apache usually loads its Vhost configurations from /etc/apache2/sites-available and by extension/etc/apache2/sites-enabled. It usually also comes with a module for easy enabling and disabling of sites, like so: sudo a2ensite mysite.conf. This is, for all intents and purposes, identical to the Nginx section above on creating a symlink to enable a new configuration. It loads these alphabetically, so we’ll make a new configuration file in sites-enabled (or do it in sites-available and enable the site subsequently) called 000-default.conf.

The first statement we put in there will be:

UseCanonicalName Off

This is required so that we can dynamically assign aliases to the URLs Apache receives. Then, we define the VirtualHost block:

<VirtualHost *:80>
	ServerName vhosts.fqdn
	ServerAlias *.local.com
	VirtualDocumentRoot /var/www/%1+
</VirtualHost>

The %1 means “Take the first part of the dot-separated domain name”. For other options, and if you wish to configure it differently, see the docs.

If we now put the folder something into /var/www and inside it index.php with some Hello World content, it should all work. But in this case, it’ll output the contents of the PHP file as text. We need to configure the rest of the VirtualHost’s parameters as usual. I’ll paste my PuPHPet version below – your configuration may vary.

UseCanonicalName Off
<VirtualHost *:80>
  ServerName vhosts.fqdn
  ServerAlias *.local.com

  VirtualDocumentRoot /var/www/%1

  <Directory ~ "/var/www/.*">
    Options Indexes FollowSymlinks MultiViews
    AllowOverride All
    Require all granted
    
    <FilesMatch "\.php$">
      Require all granted
      SetHandler proxy:fcgi://127.0.0.1:9000
      
    </FilesMatch>

  </Directory>
</VirtualHost>

Now, if you reload Apache configuration with sudo service apache2 restart or whichever command works on your distribution, you should be able to access something.local.com in your browser and once again see it working if you put some PHP files intovar/www/something/public.

https://www.sitepoint.com/set-automatic-virtual-hosts-nginx-apache/

Tags