Deploying your Node.js project on a VPS (Virtual Private Server) can be a game-changer in terms of performance, control, and customization. In this guide, I’ll walk you through the entire process of deploying your project, first by using the server’s IP address and then linking it to your custom domain. Let’s dive into it!
Step-by-Step Guide to Deploy Your Node.js Project on VPS
1. RUN PROEJCT ON VPS SERVER IP ADDRESS WITH PORT
The first thing you need to do is transfer your Node.js project files from your local machine to the VPS. You can use tools like SCP (Secure Copy Protocol) or FileZilla to securely upload your files to the server.
scp -r /path/to/your/project username@vps-ip:/path/on/server
Once the files are on the server, log in to your VPS via SSH and navigate to the project directory. Now, you need to ensure that Node.js and npm (Node Package Manager) are installed on the VPS.
update project hostname to your vps server ip and port
– Allow Project Port in VPS Firewall
By default, the VPS might block certain ports for security reasons. You need to allow the port your project is running on using the ufw
(Uncomplicated Firewall) command. This step is crucial to allow traffic to your application.
sudo ufw allow {your-port}
sudo ufw enable
sudo ufw status
5. Test Your Application with the VPS IP and Port
Once the project is running and the firewall is configured, test the application by visiting the IP and port in your browser.
http://your-vps-ip:your-port
If everything is configured correctly, your Node.js project should be live and accessible. If not, double-check the steps above, particularly the firewall rules and hostname configuration.
2. Deploy Your Node.js Project with a Domain Name via cloudflare
Now that your project is accessible via the VPS IP address, the next step is to link it to a domain name for a professional appearance.
Next, install Nginx on your VPS to act as a reverse proxy. This is necessary to route traffic from your domain name to your Node.js application running on a specific port. and create reverse proxy
sudo apt install nginx
sudo nano /etc/nginx/sites-enabled/domain.conf
Update the configuration to something like this:
server {
listen 80; # for via ip4 acess
listen [::]:80; #for via ip6 access
server_name your-domain.com;
location / {
proxy_pass http://localhost:your-port;
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;
}
}
Restart Nginx Server
For the changes to take effect, restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
3. Run NodeJS Project in Background on VPS Server
pm2
is a process manager for Node.js applications that allows you to run apps in the background and provides features like auto-restarting and monitoring.
npm install -g pm2
Go to Your Project directory run below command
pm2 start npm --name "myproject" -- run dev
Now You have successfully run your project in background process, hit your domain.com and get the output:)
Some Command of PM2
#give you logs of server running
pm2 logs myproject
#stop your project
pm2 stop myproject
#restart project
pm2 restart myproject
#list of all project
pm2 list
Use pm2
for a more robust solution with monitoring and management features.
Conclusion
Deploying your Node.js project on a VPS server with pm2 is a great way to ensure better performance, control, and flexibility. By following these steps, you should have your application live using both the VPS IP and your domain name.
I’ve included a video guide in this post that visually walks through these steps, making it easier for you to follow along.
If you encounter any issues, double-check the configuration files and server setup, or feel free to leave a comment for additional help.
Good luck with your deployment! 🎉