DEV Community

CK
CK

Posted on • Originally published at blog.kapic.io on

How to deploy a NodeJS server with NGINX on a VPS

This is how I (currently, as of July 2, 2021) deploy NodeJS servers. Eventually I will figure out Docker, but for right now this is good enough. This tutorial also includes information for using Redis on the same VPS for caching.

1. Prepare a VPS

Create a VPS (I use Vultr–that's my referral link). I use Ubuntu; if you use another distro you will have to use your distro's package manager for installing software.

You'll probably want a domain name anyways, so point a domain (I will use example.com for the tutorial) at the VPS.

SSH into the VPS:

ssh root@example.com
Enter fullscreen mode Exit fullscreen mode

Update the machine:

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

Install programs that will be useful:

sudo apt install nginx redis-server python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Install a text editor (I prefer Neovim):

sudo apt install neovim
Enter fullscreen mode Exit fullscreen mode

2. Edit your NGINX files

nvim /etc/nginx/sites-available/example.com
Enter fullscreen mode Exit fullscreen mode

In the proxy_pass value, the port should be whichever port you plan to run your NodeJS server on.

server {
    listen 80;
    server_name example.com www.example.com;
    location / {
        proxy_pass http://localhost:3000/;
    }
}
Enter fullscreen mode Exit fullscreen mode

Run

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

3. Configure Redis

Edit your redis.conf file:

nvim /etc/redis/redis.conf
Enter fullscreen mode Exit fullscreen mode

Find the supervised key; set the value to systemd.

Restart Redis:

sudo systemctl restart redis.service
sudo systemctl restart redis
Enter fullscreen mode Exit fullscreen mode

4. Install NodeJS (via `nvm)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Exit your SSH session and start a new one:

ssh root@example.com
Enter fullscreen mode Exit fullscreen mode

Install NodeJS:

nvm install v15.11.0
Enter fullscreen mode Exit fullscreen mode

5. Clone your repo

git clone https://probablygithub.com/yourusername/yourrepo.git
Enter fullscreen mode Exit fullscreen mode

6. Start your server

Change directory into your server directory:

cd yourrepo.git
Enter fullscreen mode Exit fullscreen mode

Install your packages:

npm install
Enter fullscreen mode Exit fullscreen mode

Install pm2 globally:

npm install pm2 -g
Enter fullscreen mode Exit fullscreen mode

Start your server (change server.js to the path of your main file):

pm2 start server.js
pm2 startup
pm2 save
sudo reboot
Enter fullscreen mode Exit fullscreen mode

7. Configure SSL

Use LetsEncrypt:

sudo certbot --nginx -d example.com -d www.example.com
Enter fullscreen mode Exit fullscreen mode

Enter the required information, and soon enough you will have SSL for your server.

Congrats! You have a deployed NodeJS server with Redis for caching and SSL through LetsEncrypt!

Other considerations

You may want to create a different user so you are not running the server as root.

You may want to use ufw for added security. I would reference Brad Traversy's deployment strategy.

If this tutorial is broken at any point in the process, let me know by leaving a comment below. Thank you!

Top comments (0)