Deploying a Full Node.js Application with NGINX and SSL Using Lets Encrypt

Posted by


Node.js is a popular open-source, cross-platform runtime environment that allows developers to build server-side applications using JavaScript. In this tutorial, we will learn how to deploy a Node.js application with NGINX as a reverse proxy and SSL encryption using Let’s Encrypt.

Step 1: Install NGINX

First, we need to install NGINX on our server. You can do this by running the following commands:

sudo apt update
sudo apt install nginx

After the installation is complete, start the NGINX service by running:

sudo systemctl start nginx

You can also enable NGINX to start automatically on boot by running:

sudo systemctl enable nginx

Step 2: Install Node.js

Next, we need to install Node.js on our server. You can do this by following the official installation instructions for your operating system, or you can use a Node version manager like nvm.

If you are using nvm, you can install the latest LTS version of Node.js by running:

nvm install --lts

Step 3: Deploy Node.js Application

Now, it’s time to deploy your Node.js application on the server. You can do this by copying your application files to a directory on your server. For example, you can create a directory at /var/www/myapp and copy your app files there.

sudo mkdir /var/www/myapp
sudo cp -r /path/to/your/app/* /var/www/myapp

Once your files are copied, navigate to the directory and install the required dependencies by running:

cd /var/www/myapp
npm install

You can start your Node.js application by running:

node app.js

Step 4: Configure NGINX as a Reverse Proxy

Next, we need to configure NGINX to act as a reverse proxy for our Node.js application. Create a new configuration file for your app by running:

sudo nano /etc/nginx/sites-available/myapp

Add the following configuration to the file:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000; # Change the port if your app runs on a different port
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Save and close the file. Then, create a symbolic link to enable the configuration by running:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled

Finally, restart NGINX to apply the changes by running:

sudo systemctl restart nginx

Step 5: Set Up SSL Encryption with Let’s Encrypt

To secure your website with SSL encryption, we can use Let’s Encrypt, a free and automated certificate authority. Start by installing the Certbot client by running:

sudo apt install certbot python3-certbot-nginx

Next, run the following command to request a new SSL certificate for your domain:

sudo certbot --nginx -d example.com

Follow the on-screen instructions to complete the certificate installation. Once the certificate is installed, Certbot will automatically configure NGINX to use HTTPS.

Step 6: Enable Automatic Renewal

Let’s Encrypt certificates expire after 90 days, so it’s important to set up automatic renewal. Certbot provides a systemd timer to take care of this for you. Enable the timer by running:

sudo systemctl enable certbot.timer

You can test the renewal process by running:

sudo certbot renew --dry-run

If there are no issues, the renewal process will be run automatically as scheduled.

That’s it! Your Node.js application is now deployed with NGINX as a reverse proxy and SSL encryption using Let’s Encrypt. You can access your application securely at https://example.com.

0 0 votes
Article Rating
39 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@arashutosh5236
2 months ago

everything is working except the nginx part.. i guess there might be some changes around it.. i'll do check it out on google

@itsmrani
2 months ago

best video on internet

@47_sychogamer47
2 months ago

Auto renew ssl certificate command error of certbot this was error:-

Failed to renew certificate (my-domain) with error: Some challenges have failed.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

All simulated renewals failed. The following certificates could not be renewed:

/etc/letsencrypt/live/(my-domain)/fullchain.pem (failure)

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

1 renew failure(s), 0 parse failure(s)

@ProgrammingTipsNTricksTelugu
2 months ago

thanks for the amazing content dude

@omjavir07
2 months ago

Perfect

@satyapriyobiswas7900
2 months ago

ssh wala part windows wale gitbash mai vi kar sakte hai .

@patshalaaa
2 months ago

Your tutorials are awesome. is there any video of you which deploys a project using AWS serverless ? I need to deploy my project made of HTML, Handlebars, Node, mongodb, expressjs

@ujjwalkirti8196
2 months ago

can anyone provide me a way to add around 20 env variables?

@developeraccount438
2 months ago

can we use Cloudflare for SSL?

@aluexpress987
2 months ago

Very easy explanation. Is there any free alternative of Aws that does not need credit card?.

@priyankagupta1250
2 months ago

Thanks a lot

@masteradvisor594
2 months ago

i thought he was going to manually configure ssl cert in nginx config but its through bot. Lets move to another video

@PriyankaVShahasane
2 months ago

Thank you

@MamtaG-g2p
2 months ago

can we deploy node.js application without any operating system

@prajjawalpandit2106
2 months ago

It was a great help, things in the video are very clear and helpful.

@yubrajkhatri3519
2 months ago

Yesss finally api host karlya

@vinaykumarpandey1006
2 months ago

very helpful , please make it on yaml also on K8s

@MyMrAbhishek
2 months ago

Best ever !!!!!

@AKHAND_BHARAT1238
2 months ago

😮😮😮😮so simple 🎉

@Ympatel1912
2 months ago

Kisi ko freenom login ho raha hai domain ke liya ..