Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Last active January 27, 2020 15:51
Show Gist options
  • Save webmechanicx/04b2ba9f2d2274a316aaa7a89fe676ca to your computer and use it in GitHub Desktop.
Save webmechanicx/04b2ba9f2d2274a316aaa7a89fe676ca to your computer and use it in GitHub Desktop.
Host Multiple Sites on a Single Amazon EC2 Instance

Assign a Static IP Address

The first thing you should do, if you haven’t already, is assign a static IP address to your EC2 instance. You can do this in the Amazon EC2 control panel (go to Elastic IPs -> Allocate New Address). Assign the IP address to your instance, and write it down for later.

Point the URLs to Your EC2 IP Address

So, the next step is to go ahead and point your URL at your EC2 IP address. Log into your domain hosting account. In this case, I’ll describe how to do it for Go Daddy. Launch the Domains control panel and click the first domain that you want to point to your EC2 server.

  1. Click the tab that says “DNS Zone File”. You will be editing the A record of the zone file to point to the EC2 IP address.
  2. Click the Edit button
  3. Click on the IP address in the “@” row at the top. Write it down, just in case you need to revert back to it.
  4. Replace the IP address with the IP address of your EC2 instance.
  5. If you want to map subdomains (like “mobile.yoursite.com”), add those as well, but instead of “@” for the host, enter the subdomain name (i.e., “mobile”). Use the same EC2 IP address in the next field.
  6. Repeat this process for all of the domain names that you want to point to your EC2 server.

Setting Up the Server

Now, SSH into your EC2 instance using a terminal window (Mac) or Putty or Cygwin on a PC. There are many online tutorials on how to set this up.Then follow these instructions.

  1. Go to the html folder where your sites will reside. Usually this is located at /var/www/html
  2. Create separate folders for each of the sites you want to host.
  3. Next, go to your httpd folder. Usually this is at /etc/httpd/conf
  4. Open httpd.conf using your favorite editor and uncomment this line by removing the hash in front. Better to command:

sudo nano /etc/httpd/conf/httpd.conf

NameVirtualHost *.80

If your httpd.conf file does not have this line, don’t add it. Newer versions of Apache don’t need it. (If you are unsure, you can add it and you’ll just get a warning when you restart httpd). 5. Add the following lines to your httpd.conf file for each domain name and subdomain that you want to add. Below I give three examples. The middle one shows a subdomain. Substitute your email address, folder names, and domain names as appropriate.

<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
DocumentRoot "/var/www/html/website1_folder"
ServerName yourdomain.com
ServerAlias www.yourdomain.com
ErrorLog "logs/yourdomain.com-error_log"
CustomLog "logs/yourdomain.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
DocumentRoot "/var/www/html/website2_folder"
ServerName subdomain.yourdomain.com
ErrorLog "logs/yourdomain.com-error_log"
CustomLog "logs/yourdomain.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
DocumentRoot "/var/www/html/website3_folder"
ServerName anotherdomain.com
ServerAlias www.anotherdomain.com
ErrorLog "logs/yourdomain.com-error_log"
CustomLog "logs/yourdomain.com-access_log" common
</VirtualHost>
  1. Restart httpd by typing this at the command line: sudo service httpd restart

Did it work? If not, here is one possible cause. There might be another configuration file interfering with this one. Check for another .conf file in /etc/httpd/conf.d. Often your primary httpd.conf will include another .conf file in this directory. Make sure it doesn’t have some Virtual Host settings which are interfering with yours. If so, comment them out.

Another example of the apache directives:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName trinitytuts.com
    ServerAlias www.trinitytuts.com
    ServerAdmin youremail@trinitytuts.com
    #SetEnv VAR_IF_YOU_NEED_ONE VALUE
    RewriteEngine on
    DocumentRoot "/var/www/trinitytuts/"
    LogLevel warn
    ServerSignature Off

    #And whatever you need take a look to apache documentation
</VirtualHost>

<VirtualHost *:80>
   ServerName api.trinitytuts.com
   ServerAdmin webmaster@trinitytuts.com
   DocumentRoot "/var/www/api"
</VirtualHost>

For detail you can visit TrinityTuts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment