Lifecomeback

Deploying your Node.js Express application on a VPS can help you achieve more control and flexibility over your app’s environment. Here’s how you can get started

Key Things to Avoid

  • Skipping Environment Variables: Always use environment variables for configuration, and never expose sensitive data in your codebase.
  • Hard-Coding Ports and Paths: Use flexible, environment-based configurations to make your app easier to manage across different environments.
  • Ignoring Log Files: Monitor logs regularly to identify issues before they become critical.
  • Running as Root: Avoid running your app as the root user. Create a dedicated user for running your Node.js application.
  • Leaving Nginx or Database Defaults: Customize and secure your Nginx and database configurations to protect against unauthorized access.

Step 1: Buy a VPS Hosting Server

  1. Choose a VPS hosting provider like Hostinger, DigitalOcean, Linode, or any other reliable service.
  2. Select an appropriate plan based on your app’s expected traffic and resource needs.
  3. Select Your Operating System: For most Node.js deployments, Ubuntu is a popular choice. Make sure to select Ubuntu as your operating system.
  4. Once your VPS is set up, you will receive SSH credentials (username, IP address, and possibly a private key) from your hosting provider.

Step 2: Connect to Your VPS

  1. Open your terminal and connect to your VPS using SSH:

bash

Copy code

ssh username@your_server_ip

Replace username with the SSH username and your_server_ip with your VPS IP address. Enter your password if prompted.

Step 3: Install Node.js and NPM

  1. Update the package index:

bash

Copy code

sudo apt update

  1. Install Node.js and NPM using the following command:

bash

Copy code

sudo apt install -y nodejs npm

  1. Verify the installation:

bash

Copy code

node -v

npm -v

Step 4: Upload Your Node.js Project to the VPS

  1. Use SCP or Git to transfer your project files to the VPS.
    • Using SCP:

bash

Copy code

scp -r /path/to/your/project username@your_server_ip:/path/to/destination

  1. Using Git:
    • Install Git on your VPS:

bash

Copy code

sudo apt install -y git

  1. Clone your project from a repository:

bash

Copy code

git clone https://github.com/yourusername/your-repository.git

Step 5: Install Project Dependencies

  1. Navigate to your project directory:

bash

Copy code

cd /path/to/your/project

  1. Install all dependencies listed in your package.json file:

bash

Copy code

npm install

  1. Install Express (if it’s not already in your dependencies):

bash

Copy code

npm install express

Step 6: Configure Your Environment

  1. Create a .env file in your project directory to store environment variables (e.g., database URL, API keys).
  2. Add any necessary environment variables, such as:

makefile

Copy code

PORT=8080

MONGO_URL=your-mongodb-url

Step 7: Install a Database

If you’re using MongoDB, follow these steps:

  1. Install MongoDB:
  2. If your database is hosted externally (e.g., MongoDB Atlas), make sure you have your connection URL ready.

Step 8: Run Your Application

  1. To start your application, you can use:

bash

Copy code

node app.js

Or use npm if your package.json has a start script:

bash

Copy code

npm start

  1. For production, use a process manager like PM2:
    • Install PM2:

bash

Copy code

sudo npm install -g pm2

  1. Start your app with PM2:

bash

Copy code

pm2 start app.js

  1. Make PM2 auto-start on system reboots:

bash

Copy code

pm2 startup

pm2 save

Step 9: Set Up Nginx as a Reverse Proxy

  1. Install Nginx:

bash

Copy code

sudo apt install -y nginx

  1. Configure Nginx to forward requests to your Node.js app:
    • Open the Nginx configuration file:

bash

Copy code

sudo nano /etc/nginx/sites-available/default

  • Replace the contents with the following configuration:

nginx

Copy code

server {

    listen 80;

    server_name your_domain_or_ip;

    location / {

        proxy_pass http://localhost:8080;

        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;

    }

}

To save the changes you made to the Nginx configuration file using the nano text editor, follow these steps:

  1. Press CTRL + X: This command initiates the exit process in nano.
  2. Press Y: When prompted to “Save modified buffer?”, press Y to confirm that you want to save the changes.
  3. Press Enter: nano will then ask for the file name to write to. Since you’re editing an existing file, simply press Enter to save the file with the current name.
  4. Test the Nginx configuration:

bash

Copy code

sudo nginx -t

  1. Restart Nginx:

bash

Copy code

sudo systemctl restart nginx

Step 10: Configure a Firewall

  1. Allow traffic on ports 22 (SSH) and 80 (HTTP):

bash

Copy code

sudo ufw allow 22

sudo ufw allow 80

sudo ufw enable

Step 11: Access Your Application

Open your web browser and navigate to your server’s IP address or domain name. You should see your Node.js Express application running successfully.

Step 12: If you are using Mysql database.

How to Set Up MySQL on a VPS for Your Node.js Application

Step 1: Install MySQL

First, you need to install MySQL on your VPS.

For Ubuntu/Debian:

bash

Copy code

sudo apt update

sudo apt install mysql-server -y

For CentOS/RHEL:

bash

Copy code

sudo yum install mysql-server -y

Step 2: Secure the MySQL Installation

MySQL comes with a security script that helps you improve the security of your MySQL installation.

bash

Copy code

sudo mysql_secure_installation

You’ll be prompted to set up a root password, remove anonymous users, disallow root login remotely, and remove the test database. It’s recommended to answer “Yes” (Y) to all these prompts for better security.

Step 3: Log In to MySQL

Log in to the MySQL root account using the following command:

bash

Copy code

sudo mysql -u root -p

Enter the root password you set during the mysql_secure_installation process.

Step 4: Create a Database and a User

Once logged in, create a new database and a user for your Node.js application:

  1. Create a new database:

sql

Copy code

CREATE DATABASE your_database_name;

  1. Create a new user and set a password:

sql

Copy code

CREATEUSER’your_username’@’localhost’ IDENTIFIED BY’your_password’;

  1. Grant all privileges on the database to the new user:

sql

Copy code

GRANTALL PRIVILEGES ON your_database_name.*TO’your_username’@’localhost’;

  1. Flush the privileges to apply the changes:

sql

Copy code

FLUSH PRIVILEGES;

  1. Exit MySQL:

sql

Copy code

EXIT;

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top