Thursday 19 January 2012

Multiple Websites on Amazon EC2 Linux with Virtual Hosts



 n 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.









http://calebogden.com/multiple-websites-amazon-ec2-linux-virtual-hosts/




No comments:

Post a Comment