AWS multiple domains point to single EC2 Linux instance - amazon-web-services

Is it possible to setup multiple domains to point with one single linux ec2 instance?
For example :
I have registered following 3 domain and I need to point to below path in single Linux EC2 instance:
www.domain1.com - var/www/html/domain1
www.domain2.com - var/www/html/domain2
www.domain3.com - var/www/html/domain3

Very much possible. You can add A records in the dns zone of your domain registrar pointing to the elastic ip of your ec2 instance to get the requests routed.If you are having public dns instead of an elastic ip, you can add CNAME record instead of A record
Further you can use apache inside your ec2 to route the requests as you wish to required application path. Steps below. (This assumes that you have the document roots to be served for the 3 domains copied to the respective paths mentioned in your question)
Step 1 : Configure your second and 3rd domains also to point to the same EC2 public IP using domain registrar (go daddy in your case) DNS Zones configuration. (Assuming all 3 domains are yet to go live)
Step 2 : Create virtual hosts file for the 3 domains which you want. Content given below.
File 1: /etc/httpd/conf.d/domain1.com.conf with content
<VirtualHost *:80>
ServerName www.domain1.com
ServerAlias domain1.com
DocumentRoot /var/www/html/domain1
ErrorLog /var/www/html/www.domain1.com/error.log
CustomLog /var/www/html/www.domain1.com/access.log combined
</VirtualHost>
File 2: /etc/httpd/conf.d/domain2.com.conf
<VirtualHost *:80>
ServerName www.domain2.com
ServerAlias domain2.com
DocumentRoot /var/www/html/domain2
ErrorLog /var/www/html/www.domain2.com/error.log
CustomLog /var/www/html/www.domain2.com/access.log combined
</VirtualHost>
File 3: /etc/httpd/conf.d/domain3.com.conf
<VirtualHost *:80>
ServerName www.domain3.com
ServerAlias domain3.com
DocumentRoot /var/www/html/domain3
ErrorLog /var/www/html/www.domain3.com/error.log
CustomLog /var/www/html/www.domain3.com/access.log combined
</VirtualHost>
Once this is done, you can restart apache and check.
Hope this helps
Note: In case this is not working for you or if you are facing some errors, please do include the apache version, OS and version as well in the response :)

Yes, it is like normal Linux server. You can use as many domains as you want. Just point all of them to the IP address of the instance (recommending using Elastic IP). A record is better as it is faster to translate.
Then it is upon Apache Server, it controlls what directory to use with each domain.

Related

How to setup port to subdomain in AWS?

I have a domain (e.g. example.com) registered with godaddy.com and use AWS for hosting.
Nameserver on Godaddy later added to AWS
I have an ubuntu (t2.medium) apache server installed. My project in Docker has LAMP, ELK, Node, React, and Postgres installed. Everything works on the local system using direct ports. Few ports open in my project i.e.
example.com:3000 react app [frontend]
example.com:5601 kibana app
example.com:5050 postgradmin app
example.com/radius php applicatinn [backend]
example.com:8080 phpmyadmin app
my desired URLs are:
example.com:3000 react app [frontend] => http://example.com
example.com:5601 kibana app => http://kibana.example.com
example.com:5050 postgradmin app => http://postgradmin.example.com
example.com/radius php applicatinn [backend] => http://example.com/radius
example.com:8080 phpmyadmin app => http://phpmyadmin.example.com
I have tried a few things but nothing is working. on route53
tried reverse proxy on apache by adding separate conf files e.g.
default.conf
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot "/var/www/html"
ServerName example.com
<Directory "/var/www/html/">
AllowOverride all
</Directory>
</VirtualHost>
kibana.conf
Listen 5601
<VirtualHost *:5601>
ServerAdmin contact#example.com
ServerName kibana.example.com
</VirtualHost>
pgadmin.conf
Listen 5050
<VirtualHost *:5050>
ServerAdmin contact#example.com
ServerName pgadmin.example.com
</VirtualHost>
when I run docker-compose up on EC2, http://example.com/radius works fine. but others do not (e.g. http://kibana.example.com). but if try with port directly (e.g. example.com:5601) it's working. but I want to use a sub-domain not a domain with a port.
How to do that?
reverse proxy I tried as mentioned in the description but it's not working properly
First, if you want your domains to be publicly accessible. You need to create records for all domains.
Second, if you want to access by only domains without port, you should create multiple blocks of virtual host listening to only port 80, and then do the routing base on domains your server received
Example can be found here

Domain and subdomains under one IP

I have a goal to host few websites as subdomains.(Apache2 at Ubuntu 18.04 at Google Cloud Platform)
Directories:
/var/www/domain.com/public_html- main directory for real registered domain
my subdomains
/var/www/test1.domain.com/public_html
/var/www/test2.domain.com/public_html
/var/www/test3.domain.com/public_html
Access to subdomains by address test(1,2,3).domain.com
I'm new with Apache2 so it's possibe I want something unreal or use Apache2 in wrong way. If so could you show my mistakes?
I tried to configure Virtual hosts but unsuccessful.
Maybe it is possible make it via htaccess?
domain.com.conf
<VirtualHost *:80>
ServerAdmin admin#mail.com
DocumentRoot /var/www/domain.com/public_html
ServerName domain.com
ServerAlias www.domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
test1.domain.com.conf
<VirtualHost *:80>
ServerAdmin admin#mail.com
DocumentRoot /var/www/test1.domain.com/public_html
ServerName test1.domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The trouble was not in Apache configuration. It was correct.
If you will have the same goal as mine, first of all you need
create subdomains at your domain registrar
create new A records under main domain in cloud DNS for each subdomain(Google Cloud Platform in my case)
wait (half hour in my case)
And only then configure Apache webserver.

Two domains in a single AWS ec2 instance

I have two domains domain1.com & domain2.com & want to run both domains on a single ec2 instance.
I have created 2 hosted zones for both domains and added nameserver in my domain provider.
domain1.com's code resides in - /var/www/html
domain2.com's code resides in - /var/www/domain2
Added virtual host for both respective domains in
/etc/httpd/conf/httpd.conf
virtual host looks like below:
<VirtualHost *:80>
ServerAdmin webmaster#yourdomain.com
DocumentRoot "/var/www/html"
ServerName domain1.com
</VirtualHost>
So when I run domain1.com that works perfectly fine, but when run domain2.com it show code from /var/www/html but it should show code from /var/www/domain2
So please help me to identify what I am doing wrong?
Give this a try:
# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName www.domain1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/domain2"
ServerName www.domain2.com
# Other directives here
</VirtualHost>
This will set up two VirtualHosts that respond to domain1.com and domain2.com, respectively. Note that the first VirtualHost for domain1.com will be seen as the primary host and will be the default responder for all requests that don't match any ServerNames.
Other examples can be found here.

setup sub domain amazon using Route 53

im really confused regarding setting up sub domain on amazon using their DNS service (hosted zones) and another 3rd party registrar. This is because i found different solutions some include add CNAME record and others say add using virtual host in the apache server
so should i do both or adding the CNAME in the records is enough.
so far, I have tried the first solution
I have added CNAME record (for example, let s assume my website is example.com)
so I have added CNAME subdomain.example.com pointing to example.com/subdomain. I have waited for 1 day so the DNS settings to be propagated, still when I check the website i get an error.
i've also tried to add virtual host in my conf file, but it didn't work at all.
I think the answer is very clear in the referenced link, but to clarify more what you should do , Follow the following steps:
1 - Go to your AWS managment console -> Route 53.
According to your question, it looks that you are already using AWS DNS service, which means you have already created hosted zone (in case you don't have hosted zone create one )
For more info, follow the documentation http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/creating-migrating.html
2 - Click on the hosted zone, then click on create record set
the record set should be type A pointing to your #IP of your server (Elastic IP or Load balancer IP)
Example:
subdomain.example.com -> Type A -> #IP
By the way you can use CNAME record for www.subdomain.example.com that points to subdomain.example.com
3 - the last step is to add Virtual Host in your configuration file of your appache server.
open this file /etc/httpd/conf/httpd.conf.d
Add the following Virtual Hosts
<VirtualHost *:80>
ServerAdmin webmaster#yourdomain.com
DocumentRoot "/var/www/html/domain_folder"
ServerName example.com
ErrorLog "logs/example.com-error_log"
CustomLog "logs/example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#yourdomain.com
DocumentRoot "/var/www/html/subdomain_folder"
ServerName subdomain.example.com
ErrorLog "logs/subdomain.example.com-error_log"
CustomLog "logs/subdomain.example.com-access_log" common
</VirtualHost>
4 - Restart httpd by typing this at the command line:
> sudo service httpd restart
PS :
=> if this doesn't work, 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.
=> also make sure NameVirtualHost *.80 is uncomment

Route a specific path to a specific EC2 instance using Route 53

I'm not sure if this is even possible, but if so I'm looking for the best way to do it.
Say I want to host my blog for example.com on it's own EC2 instance, and I want the path to my blog to be example.com/blog
Is it possible to route all requests to example.com/blog/* to one instance, and all other requests to that domain elsewhere?
My web server is Apache.
Thanks!
You can now do this with Application Load Balancer and path-based routing: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html#path-based-routing-aws-cli
Certainly it's possible, but not with DNS nor with an ELB. The most common solution to this is to use a web server that issues a 301 or 302 redirect.
In your case, example.com would point to whatever the main site is. The web server (nginx or Apache httpd, perhaps) hosting example.com would have a redirect for example.com/blog/* that is found at another destination.
Here's an SO post on using Nginx for a redirect and for using Apache for a redirect.
Yes, but you would have to proxy your requests through an instance handling example.com. How you configure this depends on your web server.
Some examples on how to configure this:
nginx: http://wiki.nginx.org/HttpProxyModule
Apache: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
Since you are using Apache2 Server, so you can achieve this very easily by creating a Virtual Host.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vchost1.com.conf
Create Virtual Configuration using the above command, it copies the complete code of content from the default file provided by the apache2 Server.
sudo nano /etc/apache2/sites-available/vchost1.com.conf
start configuring host & domain according to your requirements
<VirtualHost *:80>
ServerAdmin admin#domain.com
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
here also, you can multiple Virtual Host configurations in a single file, start configuring and enjoy hacking.
you can also multiple web applications in a single instance by using the same method and theis reference.
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart