Setting up Apache Vhosts on AWS Linux

Tim Williams • June 11, 2016

If you plan to host multiple sites on a single AWS instance and you are using Apache, this is the guide for you!

1) Navigate to your instance’s Apache configuration directory:

[ec2-user@ip-999-99-99-99 ~]$ cd /etc/httpd/conf.d

2) Create a vhosts config file:

[ec2-user@ip-999-99-99-99 ~]$ sudo nano vhosts.conf

3) Supply some <VirtualHost> directives to point to the document root of each of your sites.

<VirtualHost *:80>

  # The name your website should respond to

  ServerName foo.com

  # Tell Apache where your document root is

  DocumentRoot /var/www/html/foo.com

  # Add this line if you are allowing .htaccess overrides.

  <Directory /var/www/html/foo.com>
    AllowOverride All
  </Directory>

</VirtualHost>

<VirtualHost *:80>

  # The name your website should respond to

  ServerName bar.com

  # Tell Apache where your document root is

  DocumentRoot /var/www/html/bar.com

  # Add this line if you are allowing .htaccess overrides.

  <Directory /var/www/html/bar.com>
    AllowOverride All
  </Directory>

</VirtualHost>

4) Restart Apache for the changes to take effect:

[ec2-user@ip-999-99-99-99 conf.d]$ sudo service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[ec2-user@ip-999-99-99-99 conf.d]$ 

5) Test your changes:

If you don’t have your site files in place yet, you can do a simple page to test and make sure apache is listening.

[ec2-user@ip-999-99-99-99 conf.d]$ echo '<h1>Hello World</h1>' > /var/www/html/foo.com/index.html

Then on your local machine, add the server’s public IP address to your hosts file with the new domain you setup:

Tims-MacBook-Pro:~ tim$ sudo nano /etc/hosts
* added to the end of the file *
999.99.99.99    foo.com

6) If everything went smoothly you should see your test file run when you hit the domain you setup from a browser!

Hello World