How to Deploy a Node.js Application to a Server with Nginx

fidilen
3 min readApr 28, 2023

--

Deploying a Node.js application to a server with Nginx is a great way to ensure that your application is up and running quickly and securely. Nginx is a powerful web server that provides a range of features, including caching, load balancing, and security. With Nginx, you can set up multiple Node.js applications on the same server and keep them running smoothly.

Using Nginx as a reverse proxy has many benefits. It increases website performance by caching static content, reducing server load, and providing load balancing and failover capabilities. It also provides better security by hiding the origin server and protecting against DDoS attacks. Additionally, Nginx can be used as a web server, providing a single point of entry for multiple applications and services, and can be used to route requests based on URL or other criteria.

In this guide, we’ll discuss how to deploy a Node.js application to a server with Nginx.

Install Nginx

sudo apt update
sudo apt install nginx

Setup Firewall

If you already have setup a firewall, ensure that Nginx is allowed.

ufw app list

The command above should list out the available applications.

If you have no firewall yet, let’s install one!

sudo apt install ufw

Before you do anything further, you will have to ensure that the firewall allows ssh so that you won’t get locked out when you try to access your server via ssh again.

ufw allow ssh

Ensure that you have both Nginx and OpenSSH listed in the allowed apps by running the command:

ufw app list

To allow Nginx, run the command below. Select any of the commands depending on your site configuration.

sudo ufw allow 'Nginx Full'
sudo ufw allow 'Nginx HTTPS'
sudo ufw allow 'Nginx HTTP'

Configure Nginx

To check the status of Nginx, enter the command:

sudo systemctl status nginx

To start the Nginx,

sudo systemctl start nginx

Let’s create a directory inside the directory /var/www

sudo mkdir -p /var/www/domain.com/src

Ensure that you have set correct permissions:

sudo chmod -R 755 /var/www/domain.com
sudo chown -R $USER:$USER /var/www/domain.com/src

Next, create a simple html file:

nano /var/www/domain.com/src/index.html
<html>
<head>
<title>domain</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

To save and close nano, Ctrl + O > Enter > Ctrl + X.

Now to deploy this page, you will need to add a configuration at /etc/nginx/sites-available and enable it at /etc/nginx/sites-enabled/

sudo nano /etc/nginx/sites-available/domain.com
server {
listen 80;
listen [::]:80;

root /var/www/domain.com/src;
index.html;

#server names or ip addresses are separated by spaces
server_name domain.com www.domain.com ipaddress;

#error_page 404 /not-found.html;

location / {
try_files $uri $uri/ =404;
}
}
sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com

To test if configuration is correct:

nginx -t

If no issues occur, reload Nginx:

sudo systemctl reload nginx

Finally, it’s deployed. Open a browser and enter your server’s IP address or domain name.

Conclusion

In conclusion, deploying a Node.js application to a server with Nginx is a straightforward process that requires minimal setup. With the right configuration, you can easily deploy your Node.js application and ensure it is running optimally. With Nginx, you can also ensure that your application is secure and that it can handle any incoming traffic.

Need Assistance?

Contact me via Discord or any socials!

--

--

fidilen
0 Followers

Full Stack Developer · Bot Developer · Web Developer · API Developer · SQL Developer https://fidilen.com