DEV Community

Harrsh Patel
Harrsh Patel

Posted on • Edited on

How to deploy server using Nginx

Terminologies -

  1. Project name - Blogify
  2. Domain name - harrsh.com

To host the website or server using Nginx, we would have to follow the following steps:


1. Create server

For this blog, we will create the server using AWS.
We can create an ec2 t2.small instance with Ubuntu OS.


2. Install Nginx

Update the fresh Ubuntu server.

sudo apt update
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

Install Nginx

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

3. Clone repositories

We will create a folder named server and will clone a GitHub repository of a simple Express server.

mkdir server
git clone https://github.com/rwieruch/node-express-server
Enter fullscreen mode Exit fullscreen mode

We will also clone the ReactJS website for this blog.

mkdir client
git clone https://github.com/rwieruch/node-express-server
Enter fullscreen mode Exit fullscreen mode

For hosting the static websites we place it in the project folder of Nginx.
Go to the folder www.

sudo cp -r client /var/www/html/blogify
Enter fullscreen mode Exit fullscreen mode

4. Configure Nginx

We will create a new Nginx configuration file in Nginx's sites-available folder. We usually create the file with the name of the Domain name to be used for that file.

cd /etc/nginx/sites-available
sudo nano harrsh.com
Enter fullscreen mode Exit fullscreen mode

We will write the following code block to configure Nginx with our API and website

// API server block
server {
    server_name api.harrsh.dimboo.io;

    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

// API server block
server {
    listen 80;
    server_name api.harrsh.com;
}

// Website server block
server {
    server_name harrsh.com;
    location / {
        root /var/www/html/blogify/dist;
        try_files $uri $uri/ /index.html;
    }
}

// Website server block
server {
    listen 80;
    server_name harrsh.com;
}
Enter fullscreen mode Exit fullscreen mode

We will now mirror sites-available to sites-enabled

sudo ln -s /etc/nginx/sites-available/harrsh.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

5. Start API server

To automatically restart the server we will use pm2 package.

npm i pm2 -g
pm2 start npm --name "blogify-server" -- start
pm2 startup
Enter fullscreen mode Exit fullscreen mode

6. Configure DNS records

To point the domains to our server, you need to add the A record in our DNS records to redirect to our server.


7. Install SSL certificates

To get the SSL certificates of our API and Website, we will use the service of Certbot.

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx
Enter fullscreen mode Exit fullscreen mode

To renew the certificates automatically

sudo certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

Top comments (0)