How to Deploy Node Js App on VPS Server
Step 1. Purchase VPS Hosting
Begin by purchasing a Virtual Private Server (VPS) hosting plan from a reliable provider. Make sure to choose a plan that meets your project's requirements in terms of CPU, RAM, and storage. This will be the server where you will deploy your application.
Step 2. Copy the VPS IP Address
Once your VPS is set up, you will receive an IP address. This IP address is crucial as it allows you to access your server remotely. Make sure to copy and save this IP address for future use.
Step 3. Connect to Your VPS via SSH
Open your terminal on your local machine. To establish a secure connection to your VPS, type the following command, replacing the IP with your VPS's IP address:
bashssh root@159.100.9.20
This command will log you into your VPS as the root user. You may be prompted to enter a password or a passphrase if you have set up SSH keys.
Step 4. Update and Upgrade Your VPS
To ensure your VPS has the latest security patches and software updates, run the following commands:
bashsudo apt update sudo apt upgrade
These commands will update the package lists and upgrade all installed packages to their latest versions.
Step 5. Install Node.js on Your VPS
Node.js is essential for running JavaScript applications on the server. Install it by executing:
bashsudo apt install nodejs
This command will download and install Node.js on your VPS, allowing you to run JavaScript applications.
Step 6. Install Git
Git is a version control system that you will use to clone your project repository. Install it with:
bashsudo apt install git
This command will install Git, enabling you to manage your project's source code.
Step 7. Clone Your GitHub Project
Navigate to your GitHub repository and copy the repository URL. Use the following command to clone your project onto the VPS:
bashgit clone <Repo URL>
Replace <Repo URL> with the actual URL of your GitHub repository. This will create a local copy of your project on the server.
Step 8. Navigate to Your Project Directory
Once your project is cloned, change into the project directory using the cd command:
bashcd <Folder Name>
Replace <Folder Name> with the name of the directory created by the git clone command.
Step 9. Install the PM2 Package with npm
PM2 is a process manager for Node.js applications. It helps keep your application running smoothly. Install it using npm:
bashnpm install pm2 -g
The -g flag installs PM2 globally, making it accessible from anywhere on your server.
Step 10. Run Your Main File with PM2
Finally, start your application using PM2 to ensure it runs continuously, even if the server restarts:
bashpm2 start <main-file>
Replace <main-file> with the name of your application's main file. PM2 will handle the process management, keeping your app running efficiently. Your application should now be up and running on your VPS.