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:
-
Login to Your Domain Provider Website:
- Go to your domain provider’s website and log in to your account.
-
Navigate to Manage DNS:
- Look for the option to manage your DNS settings. This is usually found in the domain management section.
-
Add the Following Records:
| Type | Host/Name | Value | | ---- | --------- | ----------------------- | | A | @ | Your Remote Server IP | | A | www | Your Remote Server IP | | AAAA | @ | Your Remote Server IPv6 | | AAAA | www | Your Remote Server IPv6 |
-
On Local Windows Machine:
-
Compress Your Project Folder into a Zip File.
-
Open Command Prompt.
-
Copy Zip File from Local Windows Machine to Linux Remote Server:
scp -P Port_number Source_File_Path Destination_PathExample:
scp -P 22 miniblog.zip [email protected]:
-
-
Get Access to Remote Server via SSH:
ssh -p PORT USERNAME@HOSTIPExample:
ssh -p 22 [email protected] -
Verify that all Required Software is Installed:
apache2 -v node -v npm -v pm2 --version mongod --version -
Install Apache:
sudo apt install apache2 -
Install Node and npm:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - && sudo apt-get install -y nodejs -
Install PM2:
sudo npm install -g pm2@latest -
Add PM2 Process on Startup:
sudo pm2 startup -
Install MongoDB 6.0.3 on Ubuntu 22.04:
- Follow the instructions in
InstallConfigMongoDB.md.
- Follow the instructions in
-
Verify Apache2 is Active and Running:
sudo service apache2 status -
Verify Web Server Ports are Open and Allowed through Firewall:
sudo ufw status verbose -
Run
lsCommand to Verify that the Zip File is Present:ls -
Unzip the Copied Zip File:
unzip zip_file_nameExample:
unzip miniblog.zip -
Move Project Folder from User Home to Web Server Public Directory:
sudo mv project_folder_name Destination_PathExample:
sudo mv miniblog /var/www -
Go to Your Project Folder:
cd /var/www/project_folder_nameExample:
cd /var/www/miniblog -
Install Dependencies:
npm install -
Install Node.js MongoDB Driver (If Needed):
npm install mongodb -
Create Virtual Host File:
sudo nano /etc/apache2/sites-available/your_domain.conf -
Add the Following Code in Virtual Host File:
Example Code:
<VirtualHost *:80> ServerName www.example.com ServerAdmin [email protected] ProxyPreserveHost On ProxyPass / http://127.0.0.1:8000/ ProxyPassReverse / http://127.0.0.1:8000/ <Directory "/var/www/miniblog"> AllowOverride All </Directory> </VirtualHost> -
Check Configuration is Correct or Not:
sudo apache2ctl configtest -
Enable the Proxy Module with Apache:
sudo a2enmod proxy sudo a2enmod proxy_http -
Enable Virtual Host:
cd /etc/apache2/sites-available/ sudo a2ensite your_domain.conf -
Restart Apache2:
sudo service apache2 restart -
Start Node Express Application Using PM2:
cd /var/www/miniblog sudo NODE_ENV=production pm2 start app.js --update-env -
Save PM2 Process:
sudo pm2 save -
Check PM2 Status:
sudo pm2 status -
Connect to MongoDB Shell with Super User:
mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password" -
Show Database:
show dbs -
Create New Database:
use database_name -
Create New User:
db.createUser({ user: "username", pwd: passwordPrompt(), roles: [{ role: "readWrite", db: "database_name" }] }) -
Verify Users:
show users -
Exit Mongo Shell:
quit() -
Restart MongoDB:
sudo service mongod restart -
Open .env File:
cd /var/www/miniblog sudo nano .env -
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" -
Restart Apache2:
sudo service apache2 restart -
Start Node Express Application Using PM2:
cd /var/www/miniblog sudo NODE_ENV=production pm2 start app.js --update-env -
Save PM2 Process:
sudo pm2 save -
Check Error Logs If You Get Any Error:
cd /var/log su cd apache2 cat error.log -
Clear Error Logs (Optional):
sudo bash -c 'echo > /var/log/apache2/error.log'
Extra PM2 Information:
-
Add PM2 Process on Startup:
sudo pm2 startup -
List All PM2 Processes:
sudo pm2 list -
Kill PM2 Process:
sudo pm2 kill -
Delete App from PM2 Process:
sudo pm2 delete app_name -
Save PM2 Process:
sudo pm2 save -
Save PM2 Process with --force Flag:
sudo pm2 save --force -
Restore Last Saved PM2 Process:
sudo pm2 resurrect -
Clear PM2 Dump File:
sudo pm2 cleardump -
Remove PM2 Process from Startup:
sudo pm2 unstartup
