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.
- 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.
- Click the Edit button
- Click on the IP address in the “@” row at the top. Write it down, just in case you need to revert back to it.
- Replace the IP address with the IP address of your EC2 instance.
- 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.
- 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.
- Go to the html folder where your sites will reside. Usually this is located at /var/www/html
- Create separate folders for each of the sites you want to host.
- Next, go to your httpd folder. Usually this is at /etc/httpd/conf
- 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).
- 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>
- 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
Hi, i have site abc.com (wordpress) on a ip and abc.com/new (core php). and i want to transfer it on new domain on same IP . what should i do. Both are on aws server
Copy the files to a new directory on the server, then follow the instructions in this post using that folder name. – Brian
Thank you for the help! I now bring you… infinitepizza dot com !
Haha, nice!
very interesting blog. None of these referenced files or folders exist upon first instance set up. etc/httpd/conf does not exist.
Hi Philip,
Look for slight variations of the directory names, like with a “2” in it. Otherwise, you might have a different flavor of Linux installed than I have.
Best wishes,
Brian
Thank you for your explanation. I have one question, should the seperate folders be put in /var/www or /var/www/html?
Nevermind, I found the answer :D
But I am not finding the httpd folder in the /etc, any idea?
It’s probably /etc/apache2/
Thanks for this Brian! Very clear.
Thanks Tom! Glad it was useful! – Brian
Is it possible to load if ABC.com point it to ip/admin
If xyz.com point it to ip/web like that
Hi Kiran,
Yes, I think you can use the technique in this article to do that.
Brian
Hello,
i have a website “e.g abcd.com”.
Where we are managing blog posts of different states (e.g. newyork,california etc).
Now can it be possible in AWS server to show the links like
newyork.abcd.com
california.abcd.com
where newyork or california are not the subdomains there are a parameter based on that the posts are filterd in my website.
Can you help me by guiding what will be the best way to achieve it.
Hi Subir,
I believe those have to be third-level subdomains defined at the DNS level.
However, you could point those domains at particular subdirectories, such as /california, /newyork, etc. Then you could use .htaccess or other method to make your posts appear to be in these directories…?
This is theoretical. I haven’t actually tried this.
Brian
Thanks Brian, I am still playing around with ec2 free tier and this was a great walk through of the virtual host settings.
Hi Adnan,
Glad my post was helpful!
I experimented with the EC2 free tier and it would crash every few weeks, even with a very lightly visited site, probably due to lack of memory… They are great for playing around with though.
Thanks,
Brian
Great Post … Definitely glad that I found your blog I am hosting for a couple of clients that is use hostgator … I must say their service has gone bad … Since I just started using AWS for a enterprise client I thought how would I host my smaller clients in AWS and thanks to you I have a solution …
Hi Devon,
Thanks for your comments! Glad my article was helpful!
Brian
Good article, getting ready to try it out. Thanks.
Thanks for this Information.. Works like a charm.
Thanks for this
Thanks so much for your article. i just edit /etc/httpd/conf/httpd.conf and it works.
Hi Do Hoa,
Great! Glad it worked for you!
Brian
i just modify something like that:
NameVirtualHost *:80
ServerAdmin vinhdh@hoavinh.com
DocumentRoot /var/www/html/toidangtin
ServerName toidangtin.com
ServerAlias toidangtin.com *.toidangtin.com
ErrorLog logs/toidangtin.com-error_log
CustomLog logs/toidangtin.com-access_log common
ServerAdmin vinhdh@hoavinh.com
DocumentRoot /var/www/html/giangkhoi
ServerName giangkhoi.com
ServerAlias giangkhoi.com *.giangkhoi.com
ErrorLog logs/giangkhoi.com-error_log
CustomLog logs/giangkhoi.com-access_log common
for some web browser works correctly with www. or without www. you need take note to correct DNS setup in domain manager following one of 3 sample following (belongs to domain name prodvider):
===========================================
NAME TYPE ADDRESS
@ A 54.169.85.XXX (Ec2 public IP)
www A 54.169.85.XXX
===========================================
NAME TYPE ADDRESS
yourdomain.com A 54.169.85.XXX (Ec2 public IP)
http://www.yourdomain.com A 54.169.85.XXX (Ec2 public IP)
===========================================
NAME TYPE ADDRESS
yourdomain.com A 54.169.85.XXX (Ec2 public IP)
http://www.yourdomain.com CNAME yourdomain.com
===========================================
Hi,
I changed my dns with “A”, but… ¿what I must set in dns1 and dns2 points?
Sorry, I don’t know. Please let me know if you figure it out.
Thanks,
Brian
you need to have your DNS alread hosted somewhere like GoDaddy (in the example). they offer the option to use a free DNS like “ns1.godaddy.com, ns2.godaddy.com” and then you go to “advanced settings” and then set your A and CNAME entries. hope it help you..
Is there any apps that we can use to make Amazon EC2 works like user friendly hosting (something like if i buy hosting from godaddy, they give us control panel and for reseller, we have WHM).
Thanks
Hi Legofreak,
I don’t know of a product that does exactly what you’re asking for, but that is a very good idea. Bitnami does have a lot of pre-made images for Amazon EC-2 and a separate control panel… http://bitnami.com/stacks
Brian
I checked Bitnami out and my understanding of “image” is something like Apple’s Time Machine? (the EC2 would be just to run that particular app contained in the images, this may be wrong …)
Usually if we buy a reseller webhosting, they give us access to WHM and cPanel, basically I’d like to know if possible to build this on my own, using Amazon EC2 :)
Sorry, I don’t know how to set that up..
Brian
Very helpful article. I would like to ask if this configuration is acceptable for auto scaling instances. Will I have problems with auto scaling
Hi Vasilis,
Thank you for your post! Hmmm.. I have never used auto scaling instance so unfortunately I don’t know the answer to your question… Sorry.. Let me know if you find out.
Thanks,
Brian
you can use autoscaling for this type of deployments provided you update the ami of your instance in timely manner if you make any changes to the wordpress sites eg: blog posts etc,,