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.  Remember to make a backup of your instance before proceeding.

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

Shares

Please Leave a Question or Comment

Subscribe
Notify of
guest

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

97 Comments
Inline Feedbacks
View all comments
Utkarsh
2 years ago

Hi ,
When I’m doing the above thing , my phpmyadmin not working which is on http://MY-IP/phpmyadmin, what should I do regarding this ??

James Wilson
2 years ago

Hey, Brian, this is really brilliant reading with lots of information! I have no idea about “Setting Up the Server” reading. By the way, now, I am so clear about it and got some valuable information. Thanks and keep up the good work!

S Roy
S Roy
3 years ago

Thank you. That all worked for me. It is possible to assign SSL certificates for the individual hosted domains also? Many thanks.

S Roy
S Roy
3 years ago
Reply to  Brian Shim

I was just wondering if I add the following lines in /etc/apache2/sites-available/000-default.conf for each hosted domains, will it solve the ssl issue?

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

ServerName mydomain.com
SSLEngine on
SSLCertificateFile /var/www/html/hosted_websites/mydomain/ssl/x.crt
SSLCertificateKeyFile /var/www/html/hosted_websites/mydomain/ssl/private.key
SSLCertificateChainFile /var/www/html/hosted_websites/mydomain/ssl/z.ca-bundle

Dev Bhusal
Dev Bhusal
3 years ago
Reply to  Brian Shim

yes you just need to install ssl again for different domain

Mohammad Zahir Zohair
Mohammad Zahir Zohair
3 years ago

can i host multiple domains in one Microweber instance installed on ec2 AWS?
So basically, i want to install a microweber on one ec2 container then register multiple blogs/users on the same microweber. is it possible ?

any help and idea would be appreciated!

turindeco
turindeco
3 years ago

Great tutorial! I’m use this VirtualHost config to 2 domains on my ec2 and it’s work!!!!! thaaanks!!

Anonymous
Anonymous
4 years ago

this way only running one website

d m
d m
5 years ago

Single Instance, Multiple domain with indvidual ip is that possible?

Joel (@joeljerushan)
5 years ago

Everytime i miss something i bump here ;)

Conor Hughes
6 years ago

Really good tutorial, thank you!

bitkahuna
6 years ago

i installed php-fpm on an ec2 instance, but it seems that only works with a single domain as best i can tell ??

alberto
alberto
6 years ago

September 2017 on AWS EC2 Instance. It works !! Thanks

Jason
Jason
6 years ago

I keep having an issue where the server locks up by using this method. It gives me connection errors, and I have to reboot in order to get everything working again.

Ionut Tatu
Ionut Tatu
6 years ago

Still works like a charm in 2017. Thanks!

Brian Shim
Brian Shim
6 years ago
Reply to  Ionut Tatu

Great! Glad it worked for you!

Brian

Utsav
6 years ago

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…

Amir Shaheen
7 years ago

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

Dick Goodwin
6 years ago
Reply to  Amir Shaheen

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?

Dick Goodwin
6 years ago
Reply to  Brian Shim

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

Dick Goodwin
6 years ago
Reply to  Brian Shim

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

Abhijit
Abhijit
7 years ago

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 ?