Setting Up Vhosts On A EC2 AMI

May 09, 2016 by admin Category: Blog 0 comments

In this brief tutorial we will go through setting up vhosts on a EC2

vhosts are a great way to use your EC2 for multiple websites instead of a single website at /var/www/html if you have your domain setup with Route 53 it is also a great way to create multiple subdomains for clients as some registars will charge you for every subdomain you setup. example client1.example.com

So lets get start i am assuming you already have a EC2 setup with the default location /var/www/html if not go to this blog post.

SoBytes Full EC2 Setup

Login to your EC2 with your pem file.

run the following commands.

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

If you have a existing website at /var/www/html let copy it across

cd /var/www
sudo cp -r html/* /var/www/vhosts/example.com

Ok great now lets create a vhosts config file.
[sourcecode]
cd /etc/httpd/conf.d
sudo touch vhost.conf
[/sourcecode]

Ok now lets edit the file

sudo vim /etc/httpd/conf.d/vhost.conf

Press i to insert and then paste in the code below.

<VirtualHost *:80>

  ServerName yourdomain.com
  DocumentRoot /var/www/vhosts/yourdomain.com
  #ErrorLog /var/www/vhosts/logs/error_log
  # REQUIRED. Let's make sure that .htaccess files work on 
  <Directory /var/www/vhosts/yourdomain.com>
    AllowOverride All
  </Directory>

</VirtualHost>

Permissions lets make sure our permissions are set.

sudo su 
chown -R apache:apache /var/www/vhosts
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

Ok now restart apache.

sudo service httpd restart

So it should now be all setup lets add a subdomain in route 53 add a wildcard record set.

*.example.com. A 52.51.46.200

And then add your subdomain folder to vhosts.

cd /var/www/vhosts/
sudo mkdir client1.example.com

Then add it to your vhosts file.

<VirtualHost *:80>

  ServerName client1.example.com
  DocumentRoot /var/www/vhosts/client1.example.com
  #ErrorLog /var/www/vhosts/logs/error_log
  # REQUIRED. Let's make sure that .htaccess files work on 
  <Directory /var/www/vhosts/client1.example.com>
    AllowOverride All
  </Directory>

</VirtualHost>

<VirtualHost *:80>

    ServerName example.com
    ServerAlias *.example.com
    DocumentRoot /var/www/vhosts/example.com
    <Directory /var/www/vhosts/example.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

</VirtualHost>

All done

Leave a Comment!

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