How to Deploy a Node.js Application on a Server
Deploying a Node.js application on a server allows you to make your app accessible to the public over the internet. Here’s a step-by-step guide to help you deploy a Node.js application on a server.
Step 1: Set Up a Server
First, you need to have a server to host your application. You can use cloud platforms like Amazon Web Services (AWS), DigitalOcean, Google Cloud, or Heroku. Let’s consider using a basic VPS (Virtual Private Server) from DigitalOcean for this tutorial.
- Create a VPS Instance: Choose a droplet from DigitalOcean (or any other provider).
- Access the Server via SSH: Use an SSH client (such as terminal for Linux/macOS or PuTTY for Windows) to log into the server.
ssh root@your_server_ip
Step 2: Install Node.js
Once you’re connected to your server, the next step is to install Node.js. You can do this by following these commands:
- Update package lists:
sudo apt update
- Install Node.js and npm:
Install Node.js along with npm (Node Package Manager) using the following command:
sudo apt install nodejs npm
- Verify the installation:
Check if Node.js and npm were installed successfully:
node -v
npm -v
Step 3: Transfer Your Application to the Server
You need to move your Node.js application to the server. You can do this via SCP (Secure Copy Protocol), Git, or any other file transfer method. Here’s how to do it using Git:
- Install Git (if not already installed):
sudo apt install git
- Clone your application’s repository:
git clone https://github.com/your-repository.git
Alternatively, you can use SCP to copy your local files:
scp -r /local/app/path root@your_server_ip:/remote/path
Step 4: Install Dependencies
Navigate to the project directory and install the application dependencies.
- Navigate to your app directory:
cd /path/to/your/app
- Install dependencies:
npm install
Step 5: Configure Environment Variables
Most applications use environment variables for configurations (like database connections). You can create a .env
file in your app’s root directory and store your variables there.
touch .env
Add environment variables to this file:
PORT=3000
DATABASE_URL=your-database-url
Step 6: Run the Application with a Process Manager
You’ll need a process manager like PM2 to keep your application running in the background and to automatically restart it in case of failure.
- Install PM2 globally:
sudo npm install pm2 -g
- Start your app:
pm2 start app.js --name "my-app"
- Save the PM2 process list so that the application starts on server reboots:
pm2 save
- Enable PM2 to start on boot:
pm2 startup
Step 7: Set Up Reverse Proxy with Nginx
To make your Node.js application accessible via a domain or IP address, you need to set up a reverse proxy using Nginx.
- Install Nginx:
sudo apt install nginx
- Configure Nginx:
Open the configuration file:
sudo nano /etc/nginx/sites-available/default
Add the following proxy settings:
server {
listen 80;
server_name your_domain_or_ip;
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;
}
}
- Restart Nginx:
Save the configuration and restart Nginx:
sudo systemctl restart nginx
Step 8: Test Your Application
Now your Node.js application should be accessible via your server’s IP address or domain name. Visit http://your_domain_or_ip
in your browser to see if the app is running successfully.
Step 9: Set Up SSL (Optional)
For added security, you can configure SSL for your domain using Let’s Encrypt.
- Install Certbot:
sudo apt install certbot python3-certbot-nginx
- Generate SSL certificate:
sudo certbot --nginx -d your_domain
- Test the SSL:
Visithttps://your_domain
to ensure SSL is working.
Conclusion
Deploying a Node.js application on a server involves setting up the server, installing Node.js, transferring your app, managing it with PM2, and configuring Nginx for reverse proxy. This guide gives you a comprehensive walkthrough of how to get your Node.js app live and accessible to users.