Adding virtual hosts on Bitnami Apache

Adding virtual hosts on Bitnami Apache

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:

  1. My client has a website hosted on a domain (which we shall call example.com, for confidentiality reasons)
  2. We want to build a web application on app.example.com, which will be entirely separate from example.com.
  3. 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:

  1. 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.
  2. 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:
    • /etc/apache2/sites-available
    • /etc/httpd/sites-available

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

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:

  1. 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
~$ mkdir app.example.com
~$ cd app.example.com
~$ mkdir htdocs
  1. Create a file called httpd-vhosts.conf under 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.

httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot /var/www/app.example.com
    ServerName app.example.com
    Include /opt/bitnami/apps/app.example.com/httpd-app.conf
</VirtualHost>

httpd-app.conf

<Directory "/opt/bitnami/apps/ipa.iasg.com.sg/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>
  1. Find the bitnami-apps-vhosts.conf file 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:

bitnami-apps-vhosts.conf

Include "/opt/bitnami/apps/app.example.com/httpd-vhosts.conf"
  1. Restart Apache through Bitnami using Bitnami’s ctlscript.sh script. 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

Conclusion

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:


Leave a Reply

Your email address will not be published.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.