Host Multiple Sites on a Single Amazon EC2 Instance

Here’s how to host multiple sites on your EC2 instance running the Amazon Linux AMI.  You might want to host different websites on a single instance, or perhaps just different subdomains, i.e., “subdomain.yoursite.com” (this is what I wanted to do).

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.
    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
    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
    ErrorLog "logs/yourdomain.com-error_log"
    CustomLog "logs/yourdomain.com-access_log" common
    </VirtualHost>
  6. 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.


If you are using this to expose something like phpMyAdmin to the world, you might want to add additional security above the native login screen.  This is a great article on how to add an Apache login screen.

Let me know how it went for you.  – Brian



87 thoughts on “Host Multiple Sites on a Single Amazon EC2 Instance

  1. Man I’ve been struggling with this for a long time now. I want to host site1.com and site2.com on the same instance. so i created 2 … however, both sites get redirected to the 1st specified…

  2. I am using t2.micro. In my system when company signup like company1 so its url will be company1.domain.com

    how can i get this with script is there any api please guide me through that

    1. I have it all set up, but I am accessing my ec2 instance by putting its IP address in my browser. When I do that it gives me the first website in the list. How can I tell it to give me some other instance besides the first one?

        1. OK, that’s cool. Another way of asking the question is: what is the use of vhosts? How do they work in the wild?

          Thanks for any insights. This AWS is new to me and I’m trying to learn a lot all at once.

          Dick

        2. OK, I figured it out. If I had been going through the normal route of making domain names in DNS and pointing both website names at the same address, then it would have worked.

          So since this is just for testing, added the following two lines to my etc/hosts file:
          website-1-name
          website-2-name

          After that when I put http://website-1-name into a browser, the correct website comes up, and when I put in http://website-2-name, the other website comes up.

          I still don’t know HOW it works, but at least it works. Thanks for your help.
          Dick

  3. how many subdomains does single ec2 instance can handle. i want to deploy an application with 300 subdomains, Is it possible with a single instance? If yes , then how which instance to choose and if not how many would be required and what size ?

    1. Hi Abhijit,

      This is a common question but the answer depends on what size instance you are using, how complex the site is (i.e., is it plain HTML or does it connect to a database, etc.) and how many visitors you expect to have at any one time.

      I can say that an EC2 small instance that I used was able to handle a WordPress site with a few thousand visitors per day.

      Brian

        1. Hi Muhammad,

          I don’t recommend Multisite unless you really know what you’re doing and if you are sure you need it. It has some complications that could trip you up unless you are familiar with it.

          Best,
          Brian

  4. Hi ,

    I created EC2 instance in aws and installed wamp server 3.0.16 configure and working well. But I want to add another one domain in same wamp server for another web site.Thans in advance

  5. thanks for info, but having real troubles… i can get index.html or other html files to work at virtualhosts but not php. it is perhaps also complicated by wanting unique IP addresses per website, which i’ve done with elastic ip and configuring the virtual hosts at the associated private ip. i’m running php-fpm so maybe that’s the issue?

  6. Do all of these sites read from the same wp-database on the backend? Or did you setup a separate database for each site?

  7. I was hoping to know which EC2 instance is most suitable for hosting multiple websites. I am planning to buy hosting from a managed hosting platform (this one: http://www.cloudways.com/en/amazon-cloud-hosting.php ) (since using AWS directly is a lot technical for me) and they have 6 options. The instructions you have provided to host multisite on AWS may be different for different instance types?

    1. Hi Oliver,

      The limiting factor is not so much how many websites you are hosting but the amount of traffic and what kind of sites. I am using an EC2 small instance to host a WordPress site which gets around 1.5K hits a day, and it’s fine.

      The instructions I provided for hosting multiple websites should be the same regardless of the type of instance.

      Best,
      Brian

  8. Hi Brian,

    Thanks for the great recipe ! We still in the learning process and insides like these are very helpful to understand the processes.

    Now we have a small problem dough, you may able to give us an idea in how to sort it out…

    We have a single instance, 2 separate domains pointing to 2 separate wp installs for low traffic experiment sites. Now the back end works beautifully, however the first site installed (maybe alphabetic order too) takes precedent over the other one, so both domains show only one site. All our attempts to introduce the vhost lines (as proposed on step 5) on httpd.conf failed with a server error…it also fail if we introduce a new vhost.conf under conf.d… it maybe because there is a super default host in place that we are missing.

    Domain1 > (WP back-end) site1 OK – (showing) http://www.site1.com – OK
    Domain2 > (WP back-end) site2 OK – (showing) http://www.site1.com – Showing the 1st site instead…?

    We appreciate any ideas.

    Thank you

    1. Hi E.Alen,

      The only thing I can think of is that there is another .conf file interfering with the one you are modifying. Check for included .conf files…

      Best,
      Brian

      1. Hi Brian,

        Thank you very much for your reply!

        We have checked it all… there is no other conf under the conf directory, and the other conf files on the conf.d directory also don’t seemed to have anything to do with the httpd apach…. it seems a very simple thing missing, but at the same time we can’t see what we are doing wrong.

        Is there a way to see which is the active apache default directive and where is written?

        Thank you

    2. HI,

      Solution to this is to put all sites in single tag.

      ServerAdmin webmaster@yourdomain.com
      DocumentRoot “/var/www/html/website1_folder”
      ServerName yourdomain.com
      ErrorLog “logs/yourdomain.com-error_log”
      CustomLog “logs/yourdomain.com-access_log” common

      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

  9. Hi Mark,

    Awesome, thanks for sharing very informative it would be awesome if you can guide / write us on how to install SSL certificate for Individual domains if any wants HTTPS when we follow above approach ??

    Thanks in Advance

    Regards
    Santhosh

  10. Please help to create a subdomain(m.securedomain.com) of http://www.securedomain.com and host (m.securedomain.com) website content on same AWS-EC2 .
    Methord which we already try:
    Step: 1. Logged in my AWS Account. went to AWS Route 53–> Create Hosted Zone for “m.securedomain.com”.
    Step: 2. Add New Record Set “A” with my Elastic IP(Same of the main domain http://www.securedomain.com)
    Step: 3. Copy “m.securedomain.com” NS value and Paste Add New “NS” Create Record Set http://www.securedomain.com.
    Step: 4. Create a separate folders “m.securedomain.com” under /var/www/; while my main website is located at /var/www/html. Add new file into “/var/www/m.securedomain.com/index.html”
    Step: 5. Goto httpd folder at /etc/httpd/conf and open httpd.conf then add the following lines:

    ServerName securedomain.com
    DocumentRoot “/var/www/html”

    ServerAdmin webmaster@localhost
    ServerName m.securedomain.com
    ServerAlias m.securedomain.com
    DocumentRoot “/var/www/m.securedomain.com/”

    Step: 6. Restart httpd services
    Step: 7. Login my godaddy account–> Select Domain–> DNS Zone File–> Add A Record with(A) Record Type: A B) Host: m C) Points To: Elastic IP of securedomain.com)

    Still it does not working. Please help me to resolve this issue?

  11. Nice to see you’re still answering your comments, so I know you’re still watching them.

    Just wanted to drop a thank you on this. I just needed the info on the Apache config, and this was a perfect reference for those of us that know our way around *mostly*. Turned an hour of dinking around into a 5 minute job.

    Thanks so much!

Leave a Comment or Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.