Black Background With Text Overlay Screengrab

How to Point Domain and Host Node Express Project on Apache Web Server VPS Hosting

What is PM2?
PM2 is a powerful, widely-used, and feature-rich production-ready process manager for Node.js. It ensures that your Node.js applications are continuously running, even after server reboots. One of PM2’s crucial features is its ability to generate a startup script, ensuring that PM2 and your processes restart automatically every time your server restarts.

Steps:

  1. Login to Your Domain Provider Website:
  • Go to your domain provider’s website and log in to your account.
  1. Navigate to Manage DNS:
  • Look for the option to manage your DNS settings. This is usually found in the domain management section.
  1. Add Following Records:
  • Type: A, Host/Name: @, Value: Your Remote Server IP
  • Type: A, Host/Name: www, Value: Your Remote Server IP
  • Type: AAAA, Host/Name: @, Value: Your Remote Server IPv6
  • Type: AAAA, Host/Name: www, Value: Your Remote Server IPv6
  1. On Local Windows Machine:
  • Compress Your Project Folder into a Zip File.
  • Open Command Prompt.
  1. Copy Zip File from Local Windows Machine to Linux Remote Server:
  • Query:
scp -P Port_number Source_File_Path Destination_Path
  • Example:
scp -P 22 miniblog.zip raj@216.32.44.12:
  1. Get Access to Remote Server via SSH:
  • Query:
ssh -p PORT USERNAME@HOSTIP
  • Example:
ssh -p 22 raj@216.32.44.12
  1. Verify that all required software is installed:
  • Query:
apache2 -v node -v npm -v pm2 --version mongod --version
  1. Install Apache:
  • Query:
sudo apt install apache2
  1. Install Node and npm:
  • Query:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\ sudo apt-get install -y nodejs
  1. Install PM2:
    • Query:
sudo npm install -g pm2@latest
  1. Add PM2 Process on Startup:
    • Query:
sudo pm2 startup
  1. Install MongoDB 6.0.3 on Ubuntu 22.04:
  2. Verify Apache2 is Active and Running:
    • Query:
sudo service apache2 status
  1. Verify Web Server Ports are Open and Allowed through Firewall:
    • Query:
sudo ufw status verbose
  1. Run ls command to verify that the zip file is present:
    • Query:
ls
  1. Unzip the Copied Zip File:
    • Query:
unzip zip_file_name
    • Example:
unzip miniblog.zip
  1. Move Project Folder from User Home to Web Server Public Directory:
    • Query:
sudo mv project_folder_name Destination_Path
    • Example:
sudo mv miniblog /var/www
  1. Go to Your Project Folder:
    • Query:
cd /var/www/project_folder_name
    • Example:
cd /var/www/miniblog
  1. Install Dependencies:
    • Query:
npm install
  1. Install Node JS MongoDB Driver (If needed):
    • Query:
npm install mongodb
  1. Create Virtual Host File:
    • Query:
sudo nano /etc/apache2/sites-available/your_domain.conf
  1. Add Following Code in Virtual Host File:
    • Example Code:
<VirtualHost *:80> ServerName www.example.com ServerAdmin contact@example.com ProxyPreserveHost On ProxyPass / http://127.0.0.1:8000/ ProxyPassReverse / http://127.0.0.1:8000/ <Directory "/var/www/miniblog"> AllowOverride All </Directory> </VirtualHost>
  1. Check Configuration is correct or not:
    • Query:
sudo apache2ctl configtest
  1. Enable the Proxy module with Apache:
    • Query:
sudo a2enmod proxy sudo a2enmod proxy_http
  1. Enable Virtual Host:
    • Query:
cd /etc/apache2/sites-available/ sudo a2ensite your_domain.conf
  1. Restart Apache2:
    • Query:
sudo service apache2 restart
  1. Start Node Express Application using pm2:
    • Query:
cd /var/www/miniblog sudo NODE_ENV=production pm2 start app.js --update-env
  1. Save PM2 Process:
    • Query:
sudo pm2 save
  1. Check PM2 Status:
    • Query:
sudo pm2 status
  1. Connect to MongoDB shell with Super User:
    • Query:
mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password"
  1. Show database:
    • Query:
show dbs
  1. Create New Database:
    • Query:
use database_name
  1. Create New User:
    • Query:
db.createUser({user:"username", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]})
  1. Verify Users:
    • Query:
show users
  1. Exit Mongo Shell:
    • Query:
quit()
  1. Restart MongoDB:
    • Query:
sudo service mongod restart
  1. Open .env:
    • Query:
cd /var/www/miniblog sudo nano .env
  1. Make below changes:
    • Example Changes:
HOST = 127.0.0.1 PORT = 8000 DATABASE_URL = "mongodb://127.0.0.1:27017" DBNAME = "Your_Database_Name" DBUSERNAME = "Your_Database_Username" DBPASSWORD = "Your_Database_Password" DBAUTHSOURCE = "database_name_where_user_stored"
  1. Restart Apache2:
    • Query:
sudo service apache2 restart
  1. Start Node Express Application using pm2:
    • Query:
cd /var/www/miniblog sudo NODE_ENV=production pm2 start app.js --update-env
  1. Save PM2 Process:
    • Query:
sudo pm2 save
  1. Check error logs If you get any error:
    • Query:
cd /var/log su cd apache2 cat error.log
  1. Clear Error Logs (Optional):
    • Query:
sudo bash -c 'echo > /var/log/apache2/error.log'

Extra PM2 Information:

  • Add PM2 Process on Startup:
  • Query:
sudo pm2 startup
  • List All PM2 Process:
  • Query:
sudo pm2 list
  • Kill PM2 Process:
  • Query:
sudo pm2 kill
  • Delete App from PM2 Process:
  • Query:
sudo pm2 delete app_name
  • Save PM2 Process:
  • Query:
sudo pm2 save
  • Save PM2 Process with –force flag:
  • Query:
sudo pm2 save --force
  • Restore Last Saved PM2 Process:
  • Query:
sudo pm2 resurrect
  • Clear PM2 Dump File:
  • Query:
sudo pm2 cleardump
  • Remove PM2 Process from Startup:
  • Query:
sudo pm2 unstartup