In this project, I set up multiple static websites on a single server using Nginx Virtual host technique by following steps:
- Created two subdomains (first.mdsahil.tech & second.mdsahil.tech) of a domain (mdsahil.tech) for two diffrent web sites.
- Spinned up an ubuntu server on AWS EC2 by following steps:
-
Created a security group:
aws ec2 create-security-group \ --group-name demo-sg \ --description "AWS ec2 CLI Demo SG" \ --tag-specifications 'ResourceType=security-group,Tags=[{Key=Name,Value=demo-sg}]' \ --vpc-id "VPC_ID"
-
Added inbound rules to the security group
demo-sg
:aws ec2 authorize-security-group-ingress \ --group-id "sg-07570e17ab8331f13" \ --protocol tcp \ --port 22 \ --cidr "0.0.0.0/0"
-
Created an EC2 instance with AMI for
ubuntu 20.04
:aws ec2 run-instances \ --image-id ami-0d70546e43a941d70 \ --count 1 \ --instance-type t2.micro \ --key-name bibin-server \ --security-group-ids sg-07570e17ab8331f13 \ --subnet-id subnet-00b5ede5e160caa59 \ --block-device-mappings "[{\"DeviceName\":\"/dev/sdf\",\"Ebs\":{\"VolumeSize\":30,\"DeleteOnTermination\":false}}]" \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=demo-server}]' 'ResourceType=volume,Tags=[{Key=Name,Value=demo-server-disk}]'
-
- Installed Nginx on the ubuntu server.
- Obtained an SSL certificate for a wildcard domain
*.mdsahil.tech
- Configured Nginx to host multiple static websites with the SSL certificate for
*.mdsahil.tech
by following steps:-
Created two vitual hosts for two diffrent static web sites in the directory
/etc/nginx/conf.d
# First Virtual host server { listen 443 ssl; root /var/www/first.mdsahil.tech/html; index index.html index.htm index.nginx-debian.html; ssl_certificate /etc/ssl/certs/wild_mdsahil_tech_chain.crt; ssl_certificate_key /etc/ssl/private/mdsahil_tech.key; server_name first.mdsahil.tech www.first.mdsahil.tech; location / { try_files $uri $uri/ =404; } }
# Second Virtual host server { listen 443 ssl; root /var/www/second.mdsahil.tech/html; index index.html index.htm index.nginx-debian.html; ssl_certificate /etc/ssl/certs/wild_mdsahil_tech_chain.crt; ssl_certificate_key /etc/ssl/private/mdsahil_tech.key; server_name second.mdsahil.tech www.second.mdsahil.tech; location / { try_files $uri $uri/ =404; } }
-
- Added the IP address of the server to two sub-domains
first.mdsahil.tech
&second.mdsahil.tech
of a domain*.mdsahil.tech
as A Record.
Then both of websites were accessible.