Set up a Railo/Apache/MySQL host on Ubuntu - Part 4: install subdomains [END]

This article will show you how to install subdomains on your host. This is the fourth article and also the last one of the series "Set up a Railo/Apache/MySQL host on Ubuntu", which is composed of 4 parts:
- Part 1: install Apache and Railo
- Part 2: install MySQL and enable remote access
- Part 3: install FTP server and set up user access
- Part 4: install subdomainss
In the first 3 parts, I showed you how to install Railo, Apache, MySQL and FTP server on Ubuntu. If you would like to follow the series from the beginning, you can read part 1 at Set up a Railo/Apache/MySQL host on Ubuntu - Part 1: install Railo and Apache, part 2 at Set up a Railo/Apache/MySQL host on Ubuntu - Part 2: install MySQL and enable remote access and part 3 at Set up a Railo/Apache/MySQL host on Ubuntu - Part 3: install FTP server and set up user access. However, the topics of these articles are quite independent. So you don't need to read the first 3 articles if you are only interested in installing subdomains after already having the host up and running.
At the end of every step, if necessary I will include a little part called Step verification for you to know if you have completed that step successfully. It's important that you check this as a step depends on the one before it to be completed. So if you could not get one step done successfully, there is chance you will encounter errors at the steps that follow.
PART 4: Install subdomains
1. Assumptions and notes
1.1 You have SSH access
Similar to the first 3 parts, this article assumes that you already installed Ubuntu 9 and you got SSH access to your host. Plus, you have permission to run certain commands on it. If you receive error saying "Permission denied" or something similar, try to use sudo before the command and login with your password when asked at the first time.
1.2 If you are using Windows, you have a Unix-mode editor installed
During this process, you will need to edit some config files. If you are on Windows, make sure you use an editor that supports Unix-mode to do this. A good and free one is Crimson Editor. This is very important. You have to edit the files under this editor otherwise they will cause errors when you reupload them to the server. Do NOT use Notepad.
1.3 You already set up your sites
You should by now have set up your sites and known where the files in those sites are located. If you follow the first article about installing Railo, your sites will be sitting under the /srv/www/ folder.
There are no specifc rules of where these folders have to be. So if someone else installed Railo and set up the sites for you, ask that person where he/she puts the files.
This article assumes that you have set up 2 sites and their files are under the following folders:
- Site 1's files are at /srv/www/mysite1.com/
- Site 2's files are at /srv/www/mysite2.com/
And you would like to install 2 subdomains for site 1 and 2 subdomains for site 2 as follows:
- Subdomain 1 for site 1: http://subdomain1.site1.com/
- Subdomain 2 for site 1: http://subdomain2.site1.com/
- Subdomain 1 for site 2: http://subdomain1.site2.com/
- Subdomain 2 for site 2: http://subdomain2.site2.com/
2. Install subdomains for Apache
2.1 Configure main domain
Before you can set up subdomains, you need to configure the main domain. Open the Apache config file at /etc/apache2/httpd.conf and add in the following block for the main domain if it's not already there. Note: in case you're wondering why the config for the main domain was not there yet, the reason is this config is only required to set up subdomains. Without it, your main domain still works.
<VirtualHost 12.34.56.78:80> DocumentRoot /srv/www/mysite1.com/public_html ServerName mysite1.com <Directory "/srv/www/mysite1.com/public_html/"> allow from all Options +Indexes </Directory> </VirtualHost>
Don't forget to replace 12.34.56.78 with your host IP address.
2.1 Configure subdomains
Now subdomains are ready to be configured. Their files can sit in any folder under the root folder on your host. In this article, I assume that your subdomains' files are in the following locations:
- Site 1 subdomain 1's files are at /srv/www/subdomain1.mysite1.com/
- Site 1 subdomain 2's files are at /srv/www/subdomain2.mysite1.com/
- Site 2 subdomain 1's files are at /srv/www/subdomain1.mysite2.com/
- Site 2 subdomain 2's files are at /srv/www/subdomain2.mysite2.com/
Settings for subdomains are in fact very similar to those of the main domain. Open that same Apache config file at /etc/apache2/httpd.conf and add in these blocks following the main domain block:
For subdomain 1:
<VirtualHost 12.34.56.78:80> DocumentRoot /srv/www/subdomain1.mysite1.com/public_html ServerName subdomain1.mysite1.com <Directory "/srv/www/subdomain1.mysite1.com/public_html/"> allow from all Options +Indexes </Directory> </VirtualHost>
For subdomain 2:
<VirtualHost 12.34.56.78:80> DocumentRoot /srv/www/subdomain2.mysite1.com/public_html ServerName subdomain2.mysite1.com <Directory "/srv/www/subdomain2.mysite1.com/public_html/"> allow from all Options +Indexes </Directory> </VirtualHost>
Do the same for subdomains of site 2.
3. Install subdomains for Railo
We have completed the necessary configurations for Apache, now we need to do the similar things for Railo. Open Railo config file at /opt/soft/railo/conf/resin.conf and look for the section that contains site 1 configs. It looks something like this:
<host id="mysite1.com"> <host-alias>www.mysite1.com</host-alias> <root-directory>/srv/www/mysite1.com</root-directory> <access-log path="/srv/www/mysite1.com/logs/access.log"/> <web-app id="/" root-directory="public_html"/> </host>
Add in another block for your subdomain 1 of site 1:
<host id="subdomain1.mysite1.com"> <host-alias>subdomain1.mysite1.com</host-alias> <root-directory>/srv/www/subdomain1.mysite1.com</root-directory> <access-log path="/srv/www/subdomain1.mysite1.com/logs/access.log"/> <web-app id="/" root-directory="public_html"/> </host>
And another one for subdomain 2 of site 2:
<host id="subdomain2.mysite1.com"> <host-alias>subdomain2.mysite1.com</host-alias> <root-directory>/srv/www/subdomain2.mysite1.com</root-directory> <access-log path="/srv/www/subdomain2.mysite1.com/logs/access.log"/> <web-app id="/" root-directory="public_html"/> </host>
Do the same for subdomains of site 2.
4. Restart Apache and Railo
That's all the settings you need to do to set up subdomains. Now restart Apache and Railo for them to take effect. To restart the servers, from SSH:
$ sudo /etc/init.d/apache2 restart
If you didn't receive any errors, all the subdomain setups are complete.
5. Set up A/AAAA Records
The next step requires you to change some settings inside your host in order to enable subdomains. The specific steps depend on what host you are using. Below are generally what you are looking for when you login to your host. The demostrated sreenshots are taken with Linode (http://www.linode.com/index.cfm):
-
After login, go to the area called something like "DNS Manager"
-
You will be presented with the list of domains you are currently registered with your host. Select the one you want to add subdomains
-
Look for the section about "A/AAAA Records" and add a new one. Depending on your host, there should be already one record there for www so it is a good example for you to follow.
-
Once clicked add, fill in the details as follows:
- Hostname (or subdomain name) is the name of the subdomain you want to add such as subdomain1, subdomain2
- IP Address is your host IP address
- TTL can be left as Default
Again, the steps about are what I did with Linode and I think they should not be too much different if you are using another host. If you get stuck, please leave a comment. Note: it will take a while for the new subdomains to take effect. It is 15 minutes on Linode.
5. Wrapping up
This is the end of part 4 of the series "Set up a Railo/Apache/MySQL host on Ubuntu". If you follow the first 3 parts as well, you should now have a fully functional host equipped with Railo, Apache, MySQL, FTP server and a number of subdomains.
And that is it! That is the end of the whole series. I hope that you enjoyed reading and learnt new things that help you set up your host.
Thanks a lot for reading, especially those who read the entire series. Please let me know what you think about this article (and this series if you did read them all) by leaving a comment below. Is it too brief? Is it too detailed? Is it easy to follow? And more importantly, does it work for you?
3 Comments
-
sergio eurico28 Feb 2013, 3:52 PM
Thank you for this guide.
I really love it!!
Do you know if can i use all this steps on Amazon EC2? -
Vinh Khoa Nguyen28 Feb 2013, 9:58 PM in reply to sergio eurico
Hi Sergio, thanks for reading. Learning to use the AWS environment is definitely something on my to do list, but right now I haven't got much experience with it so sorry that I could not help you in this case. I hope you will find a solution for your scenario soon. Cheers.
Hey, that's a clever way of thiknnig about it.