DEV Community

Cover image for AWS Linux - Setting up Apache Vhosts
Alexandre Freire
Alexandre Freire

Posted on • Updated on

AWS Linux - Setting up Apache Vhosts

  • This tutorial was writed by Tim Williams on thewebhacker.com
  • I reposting this article because it is most easy for me found after. I will stay very happy if help you too.

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
Enter fullscreen mode Exit fullscreen mode

2) Create a vhosts config file:

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

3) Supply some 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>
Enter fullscreen mode Exit fullscreen mode

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]$ 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)