SPA server setup on AWS
host your single page application on an AWS server
First you need a server
and a domain
Note:
- you can get a free 1GB AWS server (for learning purpose) for One year
- You can get free domain from freenom.com for One year
You need to bind your server IP in your DNS nameserver
- in freenom it will take some time to update it
Now It’s time to setup Nginx :)
- first login to your server and enter to sudo with
sudo su
to get all permissions - first you need to install Nginx on your server (read this docs)
$ sudo apt update
$ sudo apt install nginx
$ sudo ufw allow 'Nginx HTTP'
To check nginx status systemctl status nginx
Managing the Nginx process
sudo service nginx start
— to start nginxsudo service nginx stop
— to stop nginxsudo service nginx restart
— to restart nginxsudo service nginx reload
— to reload nginxsudo systemctl enable nginx
— to re-enable nginx
after starting nginx, now you can see default nginx page with your IP
browse to http://<your_public_ip>
You can get your public ip with curl icanhazip.com
here you will se nginx default page
Now it’s time to configure your domain to nginx
we will go with the nginx default configuration
open nginx default configuration file
first navigate to /etc/nginx/sites-available
and open default
file with nano
editor — and add the below
server {
listen 80;
listen [::]:80;# your domain name
server_name my-website.com;# your build path on same server
root /var/www/html/develop-website;
index index.html index.htm index.nginx-debian.html;location / {
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
}
this is simple http configuration if you want to move to https then you need to add ssl_certificate
, ssl_certificate_key
add below configuration for redirecting to https
server {
root /var/www/html; # build path
index index.html index.htm index.nginx-debian.html;
server_name <your_domain_here>; location / {
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
} listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/<domain>/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; server {
if ($host = <domain>) {
return 301 https://$host$request_uri;
} # managed by Certbot listen 80 ;
listen [::]:80 ;
server_name <your_domain_name>;
return 404; # managed by Certbot
}
Secure your domain with let’s encrypt (read this document)
1. Installing Certbot
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt install python-certbot-nginx# verify nginx configuration
$ sudo nginx -t
2. Obtaining an SSL Certificate
$ sudo certbot --nginx -d example.com -d www.example.com
3. Verifying Certbot Auto-Renewal
$ sudo certbot renew --dry-run
Note: the above step 3 will automatically generate ssl configuration and add certificate for that domain. you need to run this command every time for each subdomain you want to add and get new certificate for that subdomain
to add certificate only on sugdomain add -d subdomain only
$ sudo certbot --nginx -d staging.example.com
checkout other blogs
circleci-configuration