VHosts: Running Multiple Sites on a Single AWS EC2 Instance

If you want to run multiple websites on a single AWS host (or really, any other Apache 2+ web server that requires manual configuration), this guide is for you!

I’ll walk you through the process of setting up your AWS server to support multiple site, domains, or subdomains using Apache’s Virtual Host (vhost) features. It’s this feature that allows Apache to detect the host/domain name being requested so that it can serve up the appropriate site from a list of options.

Don’t worry, it’s actually not as complicated as it sounds… and I’ll leave out all the wherefores and other technical mumbo jumbo, since that’s available elsewhere. You’re here for clarity.

Assumptions

Generally speaking, this guide assumes that you are using an Amazon Linux LAMP stack on a single EC2 instance, and that you are using Amazon’s Route 53 for your DNS service.

If you followed my earlier blog post, The Ultimate Guide to WordPress on AWS EC2, then you already meet all those assumptions.

1 | File System

To get started, we need to put a little thought into your file system.

You can set up your vhosts however and wherever you want, but I personally recommend that you:

  1. Create a new /var/www/vhosts/ directory to keep your vhost site folders and…
  2. Name each site directory after it’s respective domain/host name.

Keep in mind that there is no vhosts folder by default, but you can easily create the vhost folder by running the following command on your server:

$ sudo mkdir /var/www/vhosts/

Next, go ahead and create a new folder for each site/domain/subdomain you want to host. You can use a similar command to the above. For example…

$ sudo mkdir /var/www/vhosts/example.com

If you follow my lead, you will eventually end up with a file structure like this for your sites…

/var/www/vhosts/example.com
/var/www/vhosts/sub.example.com
/var/www/vhosts/sub2.example.com

2 | Adding a vhost Configuration File

Most Apache configurations don’t have a vhost.conf (the vhost configuration file) by default, so we just need to create an empty vhost file…

$ cd /etc/httpd/conf.d
$ sudo touch vhost.conf

When you’re ready, let’s edit the file with nano…

$ sudo nano /etc/httpd/conf.d/vhost.conf

Read on for what you need to add to that file.

3 | Set the Default Host / Site

Setting up a default host is an optional step. If you choose to do this, then any time Apache can’t find a better match in your vhosts file, it will give the user the default site.

To decide if this is right for you, ask yourself the following question:
“If something goes wrong with my configuration or server, is it preferable to see an error message or the wrong (i.e. default) website?”

If a system error is what you want, skip this step. If you’d rather a specified default site always show up, then you need a default host. To setup a default host, add the following to your vhost.conf, customizing as needed…

<br />
<VirtualHost *:80></p>
<p># Leave this alone. This setting tells Apache that<br />
# this vhost should be used as the default if nothing<br />
# more appropriate is available.</p>
<p>ServerName default:80</p>
<p># REQUIRED. Set this to the directory you want to use for<br />
# your “default” site files.</p>
<p>DocumentRoot /var/www/html</p>
<p># Optional. Uncomment this and set it to your admin email<br />
# address, if you have one. If there is a server error,<br />
# this is the address that Apache will show to users.</p>
<p>#ServerAdmin [email protected]</p>
<p># Optional. Uncomment this if you want to specify<br />
# a different error log file than the default. You will<br />
# need to create the error file first.</p>
<p>#ErrorLog /var/www/vhosts/logs/error_log</p>
<p></VirtualHost><br />

4 | Adding More Hosts / Sites

Next, we need to set up each new host/domain/subdomain you want your server to handle. This is similar to the above, with a couple key differences. Take a look…

<br />
<VirtualHost *:80></p>
<p># REQUIRED. Set this to the host/domain/subdomain that<br />
# you want this VirtualHost record to handle.</p>
<p>ServerName yourdomain.com</p>
<p># Optional. You can specify additional host names that<br />
# serve up the same site. This can be top-level, domains,<br />
# sub-domains, and can even use wildcard subdomains such<br />
# as *.yourdomain.com - just separate each host name<br />
# with a single space.</p>
<p>#ServerAlias www.yourdomain.com yourdomain.net</p>
<p># REQUIRED. Set this to the directory you want to use for<br />
# this vhost site's files.</p>
<p>DocumentRoot /var/www/vhosts/yourdomain.com</p>
<p># Optional. Uncomment this and set it to your admin email<br />
# address, if you have one. If there is a server error,<br />
# this is the address that Apache will show to users.</p>
<p>#ServerAdmin [email protected]</p>
<p># Optional. Uncomment this if you want to specify<br />
# a different error log file than the default. You will<br />
# need to create the error file first.</p>
<p>#ErrorLog /var/www/vhosts/logs/error_log</p>
<p># REQUIRED. Let's make sure that .htaccess files work on<br />
# this site. Don't forget to change the file path to<br />
# match your DocumentRoot setting above.</p>
<p><Directory /var/www/vhosts/yourdomain.com><br />
AllowOverride All<br />
</Directory></p>
<p></VirtualHost><br />

Hopefully, the example is self-explanatory.

Make sure you include one entire <VirtualHost *:80> block for each host/domain/subdomain you need to configure, each with it’s own settings. Every time, you should double-check the following rules…

  • ServerName
  • ServerAlias optional
  • DocumentRoot
  • ServerAdmin optional
  • ErrorLog optional
  • <Directory>

5 | Save & Restart Apache

Once you’re finished setting up all your Virtual Hosts, save and exit vhosts.conf (ctrl + x if you were using Nano) and restart Apache, like so…

$ sudo service httpd restart

6 | DNS / Route 53

There’s only one more step before this all starts working…

For each domain/subdomain you just set up, you need to create an A record that points to your server’s public ip address. Once that’s done, you’re finished!

Finding Your Public IP

In case you forgot, you can find the public IP address of your instance on your EC2 Dashboard. Select “Instances” on the left, then highlight your instance. Your public ip can be found in the info panel underneath.

Where to find your public IP

Subdomain Wildcard

If you are only setting up subdomains, you may want to consider a wildcard DNS record so that any subdomains you create in vhosts.conf work automatically. This way, you don’t need to set up DNS each subdomain separately – set it up once and forget about it. If your instance’s public ip address was 123.45.67.89, your wildcard record would look something like this…

aws-route53-wild

Fix WordPress Permissions

After copying all your files to the server, you may need to fix your WordPress permissions to ensure that everything works as expected (media uploads, plugin installs and updates, etc). Run the following commands to repair your WordPress permissions…

we need to recursively change the ownership and permissions of WordPress’s files and folders…

$ sudo -i
$ cd /var/www/vhosts/yourdomain.com (use your own vhost site path here!)
$ find . -type d -exec chmod 0755 {} \;
$ find . -type f -exec chmod 0644 {} \;
$ chown -R ec2-user:apache .
$ chmod -R g+w .
$ chmod g+s .

The idea here is this: by giving the ec2-user user ownership, you can use SFTP to modify your files (since there are virtually no SFTP clients that support sudo – not even Transmit); And by granting apache group access, you can use media uploads, auto-updates, etc.

Related Articles

Comments

Reply

You have two links on this page, The Ultimate Guide to WordPress on AWS EC2 and AWS Backups & Maintenance, but they don’t work because the URLs are set to nouveauframework.com instead of noveauframework.org.

Reply

Hey there,

<

blockquote cite=”You can set up your vhosts however and wherever you want, but I personally recommend that you:

Create a new /var/www/vhosts/ directory to keep your vhost site folders”>

Why /var/www/vhosts/example.com instead of /var/www/html/example.com?

Reply

Arun, I was also wondering if there was a particular reason why the author recommended it like that.

But I personally set mine up in the html folder and it works just the same.

Reply

Thanks for the reply Mel. I did it just like yours. I think it was just the authors personal preference. Would love to know the reason (if any) though.

Leave a Comment

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