Over the weekend, I’ve spent a substantial chunk of time figuring out how to add a virtual host onto a client’s subdomain. In laymen’s terms, this means that:
- My client has a website hosted on a domain (which we shall call example.com, for confidentiality reasons)
- We want to build a web application on app.example.com, which will be entirely separate from example.com.
- To save on cost, we want to host app.example.com on the same server that example.com is using (i.e. create a virtual host on the web server).
This means that we have to configure our web server so that it will serve a different webroot depending on the domain it is being accessed from.
The “usual” way in Apache
If you are running a standard LAMP stack for your web server, adding a virtual host will involve the following:
- Adding an appropriate DNS record for the subdomain, i.e. an A record pointing to the same IP as your root domain (example.com in our case), or a CNAME record pointing to your root domain.
- Finding your Apache install directory, then registering the virtual host onto the appropriate file(s) or folder(s). In the standard Apache installations we find today, virtual hosts are usually found in:
To add a virtual host, you would navigate to either one of these folders, create a new configuration file under it, and add the virtual host:
~$ cd /etc/sites-available ~$ vim app.example.com.conf
<VirtualHost *:80> DocumentRoot /var/www/app.example.com ServerName app.example.com </VirtualHost>
Then, you would make a symbolic link to the newly-created file in the adjacent
sites-enabled folder, and then restart Apache:
~$ ln -s /etc/apache2/sites-available/app.example.com.conf /etc/apache2/sites-enabled/app.example.com.conf ~$ service apache2 restart
Finally, you would have to put your web files into the
DocumentRoot folder that was specified in the virtual host file, and your new virtual host will start working.
Article continues after the advertisement:
The Bitnami Apache way
If your server is using Apache through Bitnami, however, adding virtual hosts becomes an entirely different ballgame on the server-side. I still don’t know the entirety of how Bitnami works, but I can tell you what worked for me:
- Create a new folder for your virtual host under
/opt/bitnami/apps, then create another folder under it which will serve as your webroot.
~$ cd /opt/bitnami/apps ~$ sudo mkdir app.example.com ~$ sudo chown bitnami:bitnami app.example.com ~$ cd app.example.com ~$ sudo mkdir htdocs ~$ sudo chown bitnami:bitnami htdocs
- Create a file called
httpd-vhosts.confunder your virtual host’s folder (NOT the webroot), and point your virtual host to the web root folder that you’ve just created, as shown below. Note the additional line as compared to the vanilla Apache version, which imports a configuration file containing additional settings for the virtual host.
<VirtualHost *:80> DocumentRoot /opt/bitnami/apps/app.example.com/htdocs ServerName app.example.com Include /opt/bitnami/apps/app.example.com/httpd-app.conf </VirtualHost>
<Directory "/opt/bitnami/apps/app.example.com/htdocs"> Options -Indexes +FollowSymLinks AllowOverride All # The configurations in this file are mainly to allow Apache # access to our webroot, as the Bitnami apps folder is # restricted by default. <IfVersion < 2.3 > Order allow,deny Allow from all </IfVersion> <IfVersion >= 2.3> Require all granted </IfVersion> </Directory>
- Find the
bitnami-apps-vhosts.conffile under Bitnami’s Apache configuration folder:
/opt/bitnami/apache2/conf/bitnami. Add the following line into the file, so that configurations for our newly-added virtual host will be loaded by Apache:
- Restart Apache through Bitnami using Bitnami’s
ctlscript.shscript. Note that as Apache was installed as part of Bitnami, it isn’t an actual installed program on the OS, so you can’t restart it directly.
~$ /opt/bitnami/ctlscript.sh restart apache
If everything is set up correctly, the virtual host should begin to work, and your subdomain will accept HTTP requests. If you want your virtual host to have HTTPS support, you’ll need additional settings on your Apache configuration, as well as an additional virtual host entry for port 443.
Did I miss anything out in the steps above? Leave a comment to let me know if you spot anything!
Article continues after the advertisement: