Googled for Days: Adding Subdomains to Apache2

Andrew Walpole
{{Nested Loops}}
Published in
2 min readApr 25, 2016

I’ll often need to know something, and google can often lead me to the answer. But sometimes finding the right information isn’t so cut and dry — especially in dealing with software. This is one of those stories.

I’m in the process of moving my website from shared hosting to Google Cloud. I figured it would be cheaper to consolidate my personal dot com with my need for a node.js playground.

So I chose the Bitnami MEAN stack because it not only sets me up with node.js, but also hosts the main domain over apache and installs PHP. So, minus MySQL, it’s pretty much LAMP + MEAN which is exactly what I wanted.

Details here if you’re interested: https://cloud.google.com/launcher/solution/bitnami-launchpad/meanstack?q=node

Ok, so setup was a breeze, maybe some minor SSH issues, but nothing big. Then came time to migrate my site, andrewwalpole.com. I set up an A URL Record on my host’s DNS settings and transferred over my files. Worked great!

Next up subdomains, because I have quite a few on my old site. For example, I want art.andrewwalpole.com to host it’s own website from a unique directory on the server like /htdocs/art.andrewwalpole.com/. Time to Google. Lots of results, but no clear answers. Some people talk about setting up a CNAME on your domain DNS settings, others point toward using .htaccess redirects. I knew enough to know I wanted to do this in Apache, and when it came to that more confusion ensued. Lots of stackoverflow questions answered, but nothing cut and dry, and each thing I tried seemed to almost work, but then break something else. Here’s the solution I finally found that worked:

Find your Apache httpd.conf file. In Bitnami’s setup it’s /opts/bitnami/apache2/conf.

To set up subdomains, you have to add <VirtualHost> properties, but what was not clearly stated online is that you have to have a <VirtualHost> for the main www domain as well, otherwise everything will redirect to the first subdomain:

#www.andrewwalpole.com - in www folder
<VirtualHost *:80>
ServerName www.andrewwalpole.com
DocumentRoot "/opt/bitnami/apache2/htdocs/www"
</VirtualHost>
#art.andrewwalpole.com - in art.andrewwalpole.com folder
<VirtualHost *:80>
ServerName art.andrewwalpole.com
DocumentRoot "/opt/bitnami/apache2/htdocs/art.andrewwalpole.com"
</VirtualHost>
#redirect blog.andrewwalpole.com to medium.com
<VirtualHost *:80>
ServerName blog.andrewwalpole.com
RedirectPermanent / http://medium.com/nested-loops
</VirtualHost>

Restart Apache (In Bitnami: sudo /opt/bitnami/ctlscript.sh restart apache) and you should be good to go! Add a new <VirtualHost> property for each subdomain you want to set up. Also note that the DocumentRoot path might be something else on a non-Bitnami setup, like /var/www/.

--

--

Andrew Walpole
{{Nested Loops}}

Developer, Designer, Teacher, Learner, Innovation Dabbler