Publishing a Node.js Application on a VPS: Step-by-Step Setup
Node.js is a powerful and popular platform that allows you to run JavaScript on the server side. To make your applications accessible on the internet, using a VPS (Virtual Private Server) is an ideal solution. In this article, we will explain step-by-step how to publish your Node.js application on a VPS.
1. VPS Selection and Setup
The first step is to choose a VPS provider that suits your needs. There are many options available on the market: DigitalOcean, Linode, Vultr, AWS (Amazon Web Services), Google Cloud Platform, etc. Each has different pricing plans, features, and geographic locations. Make a selection based on the size of your project, expected traffic, and your budget.
After selecting your VPS, you will need to choose an operating system. Popular Linux distributions such as Ubuntu, CentOS, and Debian are commonly used for Node.js applications. Ubuntu is generally considered more user-friendly for beginners.
After creating your VPS, you will need to connect to your server using SSH (Secure Shell). SSH allows you to securely access your server and run commands. You can connect to your server using the following command in your terminal:
ssh username@server_ip_address
For example:
ssh [email protected]
If you are connecting to your server for the first time, you will be prompted to enter a password or use an SSH key. SSH keys are more secure than passwords and are highly recommended.
1.1 Security Measures
It is important to ensure the security of your server. First, you may consider changing the default SSH port (22). To do this, edit the /etc/ssh/sshd_config
file and change the Port
line. After applying the changes, you will need to restart the SSH service:
sudo systemctl restart sshd
It is also important to configure a firewall. UFW (Uncomplicated Firewall) is a commonly used firewall on Ubuntu. You can use the following commands to install and enable UFW:
sudo apt update
sudo apt install ufw
sudo ufw allow OpenSSH
sudo ufw enable
Don't forget to add the port your application uses to UFW. For example, if your application uses port 3000:
sudo ufw allow 3000
2. Installation of Node.js and Necessary Tools
After connecting to your VPS, you will need to install Node.js and other tools that your application requires. The best way to install Node.js is to use Node Version Manager (NVM). NVM allows you to easily manage different Node.js versions.
To install NVM, you can use the following commands:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
After installation, you may need to restart your terminal or run the source ~/.nvm/nvm.sh
command.
After NVM is installed, you can install the Node.js version you want. To install the latest LTS (Long Term Support) version:
nvm install --lts
Then, to use this version:
nvm use --lts
Along with Node.js, npm (Node Package Manager) will also be installed. npm allows you to install and manage Node.js packages. To install your application's dependencies, go to the root directory of your application and run the following command:
npm install
If there are packages that require your application to be globally accessible, you can install them with the -g
parameter. For example, to install PM2 globally:
npm install -g pm2
3. Transferring Application Files to the VPS
There are several ways to transfer your application files to the VPS: Git, SCP (Secure Copy), or FTP (File Transfer Protocol). Git is the best option if it is used for version control.
3.1 Transferring with Git
If your application is in a Git repository, install Git on your VPS:
sudo apt update
sudo apt install git
Then, clone your application's repository to your VPS:
git clone
3.2 Transferring with SCP
SCP allows you to securely copy files from one server to another. You can use the following command to copy files from your local machine to your VPS:
scp -r local_directory username@server_ip_address:destination_directory
For example:
scp -r /home/user/application [email protected]:/var/www/
4. Running and Managing the Application
After transferring your application files to the VPS, you will need to run the application. However, you don't want the application to stop when your SSH connection is lost. Therefore, it is important to use a process manager such as PM2.
PM2 is a popular tool for managing and running Node.js applications. It automatically restarts your application, manages logs, and offers many other useful features.
After installing PM2, you can use the following command to run your application:
pm2 start app.js
Here, app.js
is the main file of your application. If your application is started in a different file, use the name of that file.
PM2 will automatically start and restart your application. To check the status of your application:
pm2 status
To see your application's logs:
pm2 logs
To automatically start your application during startup:
pm2 startup systemd
pm2 save
5. Reverse Proxy Setup (with Nginx)
Let's assume your application is running on a port like 3000. If you want users to access your application through a domain name like example.com
, you will need to set up a reverse proxy. Nginx is a popular and powerful reverse proxy server.
To install Nginx:
sudo apt update
sudo apt install nginx
After Nginx is installed, you will need to configure a virtual host for your application. Create a new configuration file: /etc/nginx/sites-available/example.com
(replace example.com
with your domain name) and add the following content:
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
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;
}
}
This configuration will redirect all incoming requests from example.com
and www.example.com
to localhost:3000
.
To activate the configuration file:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
To disable the default configuration (optional):
sudo rm /etc/nginx/sites-enabled/default
Restart Nginx:
sudo systemctl restart nginx
Now, when you visit example.com
, you should see your Node.js application running.
5.1 SSL Certificate Installation (with Let's Encrypt)
It is important to install an SSL certificate to ensure the security of your website. Let's Encrypt is a free and automated SSL certificate provider. Certbot is a tool that allows you to easily install Let's Encrypt certificates.
To install Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
To run Certbot and automatically install the SSL certificate:
sudo certbot --nginx -d example.com -d www.example.com
Certbot will automatically update your Nginx configuration and install the SSL certificate. It will also configure the certificates to be automatically renewed.
Conclusion and Summary
In this article, we explained step-by-step how to publish your Node.js application on a VPS. We covered topics such as VPS selection, installation of Node.js and necessary tools, transferring application files, running and managing the application, reverse proxy setup, and SSL certificate installation. By following these steps, you can successfully make your Node.js application accessible on the internet.
Remember, security should always be a priority. Don't forget to take measures to ensure the security of your VPS, such as configuring a firewall, changing the SSH port, and making regular updates. We wish you success!