Multiple Websites on Amazon EC2 Linux with Virtual Hosts

In my previous article outlining how to setup an instance of WordPress on Amazon’s EC2 Cloud hosting a Linux server with Mac, I described a single website setup for the server. In an addendum to that article, I’m going to give short instructions on setting up multiple websites on that instance using apache’s virtual hosts. The setup is remarkably simple. There are two main steps: 1) Setting up your sites structure, and 2) Setting up your vhost configuration. This tutorial assumes you have terminal and ssh access into your site, for instructions on how to set those up, see the SSH portion of my previous article.

Setup Website Structure

Start by setting up your wordpress (or any) site in the document root folder. Having followed my previous instructions, it will be located at /var/www/html, and you should be able to browse to it via a program like Coda or Transmit.

From this location, copy in new wordpress installations. The easiest way is to download them to your computer and drag them in, however, here are the terminal:

Go to Document Root

cd /var/www/html

Download new version of WP into your document root

wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz -C /var/www/html
mv wordpress calebogden.com

From here you’ll want to rename your folder from wordpress to whatever you’re site is called, as shown above mine was called calebogden.com. You should (in a GUI) see a similar structure to this:

NOTE: You may have to give your folders the correct permissions. If you get any permission errors, use these lines:

chown -R apache /var/www/html
chmod -R 755 /var/www/html

Now that you’ve created all the sites you’d like to host, do a quick restart on your apache service:

service httpd restart

Grand! Now that you have your sites setup we can work on apache’s configuration. The config file has tons of options, but my requirements (and thus setup) were quite simple. I’ll show you the baseline setup on how to get these work, and if you have additional questions on advanced configuration you can check out the documentation.

To setup your vhost, browse in SSH to your apache configuration folder:

etc/httpd/conf.d

If you don’t have a file named vhost.conf, you should go ahead and create it. Make sure your file permissions are right (644). This can all be done through VIM, but using a GUI here is going to be a ton easier, as you’ll appreciate the luxury of copy / paste.

The structure of the config is simple, new virtual hosts are setup through creating new nodes in the config files. A node can look like the one below:

<VirtualHost *>
ServerName example.com
DocumentRoot /var/www/html/example.com
</VirtualHost>

The simplest setup is with a name-based approach, although IP approaches are available, since the previous tutorial functions on one elastic IP from Amazon EC2, we’ll stick with name based. To read about the differences, check out this post.

After you have your nodes setup (you can literally just replace example.com with your directories / website), you’ll need to make sure the head of your vhost.conf file has the following directive:

NameVirtualHost *

The NameVirtualHost directive always you to specify the IP address on which the server will receive requests, for our purposes here, accepting them with a * is fine, although you may want to further specify your IP address. If you do specify your IP address, keep in mind you can also specify the port. You read more about the NameVirtualHost directive here.

After you get all your items setup, you should have something similar to the following:

NameVirtualHost *

<VirtualHost *>
ServerName example.com
DocumentRoot /var/www/html/example.com
</VirtualHost>

<VirtualHost *>
ServerName calebogden.com
DocumentRoot /var/www/html/calebogden.com
</VirtualHost>

<VirtualHost *>
ServerName cssbutton.me
DocumentRoot /var/www/html/cssbutton.me
</VirtualHost>

That’s pretty much it! Make sure to restart your apache service and you’re good to go. Enjoy hosting multiple sites from your EC2 Linux instance :-). I’ve opened up comments on this post to answer any questions about the setup.

Enjoy this article? Share it with your friends, and of course follow me for more like it. Also, add my feed to your favorite reader for more inane garble. Garble garble garble.

11 Responses to Multiple Websites on Amazon EC2 Linux with Virtual Hosts

Logansanoh says:

Hi,

I have tried this exactly how you have it and I can only ever get the first (alphabetically) enabled site to work for all domains.

IP: first site – title from page
public dns: first site – title from page
domain1: first site – title 1 from DNS service
domain2: first site – title 2 from DNS service

So it appears my DNS is ok.

If i a2dissite each of them and enable 1, they work.
If I enable 2, only the first one works.

Please help :)

Mark Walker says:

Thanks for this guide, it’s great, I’m a little lost however;

After I’ve set this up on my Amazon server all I have to do is point my a-name to the elastic IP I assigned to it?

I only ask as it tends to take some time for DNS to update so I’d like to do it right the first time :D

Thanks again dude!

Neil Jones says:

Hey Caleb, just wanted to thank you for this. Saved me time and is a perm link in my armory! I do alot of iphone development stuff, so was a bit hesitant to step into ec2. This made it an easy step! Thanks again

Neil Jones

Sean says:

Caleb,

Great tutorial, very helpful. Working through some WordPress multisite installs. Looks like this might work. Are there any SEO disadvantages to hosting more than one site using this method?

Thanks

Caleb Ogden says:

Hi Sean, I’m no SEO expert, I know there are endless nuances involved in how Google and other engines rate, but I’ve seen steady search-related growth on a host of my sites using this hosting method for over a year, so I’d assume it’s not a huge factor.

Raj says:

Hi Caleb, wonderful tut.. Must see tutorial for every freelance developer. I’ve followed your Amazon EC2 tutorial and after setting up multiple site using vhost, I couldn’t get phpmyadmin to work.

catering equipment South Africa says:

Hello there, I discovered your website by the use of Google at the same time as looking for a similar topic, your web site got here up, it looks great. I’ve added to favourites|added to bookmarks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>