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 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
- 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:
- Query:
scp -P Port_number Source_File_Path Destination_Path
- Example:
scp -P 22 miniblog.zip raj@216.32.44.12:
- Get Access to Remote Server via SSH:
- Query:
ssh -p PORT USERNAME@HOSTIP
- Example
:
ssh -p 22 raj@216.32.44.12
- Verify that all required software is installed:
- Query:
apache2 -v node -v npm -v pm2 --version mongod --version
- Install Apache:
- Query:
sudo apt install apache2
- Install Node and npm:
- Query:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\ sudo apt-get install -y nodejs
- Install PM2:
- Query:
sudo npm install -g pm2@latest
- Add PM2 Process on Startup:
- Query:
sudo pm2 startup
- Install MongoDB 6.0.3 on Ubuntu 22.04:
- Follow this guide: InstallConfigMongoDB.md
- Verify Apache2 is Active and Running:
- Query:
sudo service apache2 status
- Verify Web Server Ports are Open and Allowed through Firewall:
- Query:
sudo ufw status verbose
- Run ls command to verify that the zip file is present:
- Query:
ls
- Unzip the Copied Zip File:
- Query:
unzip zip_file_name
- Example:
unzip miniblog.zip
- 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
- Go to Your Project Folder:
- Query:
cd /var/www/project_folder_name
- Example:
cd /var/www/miniblog
- Install Dependencies:
- Query:
npm install
- Install Node JS MongoDB Driver (If needed):
- Query:
npm install mongodb
- Create Virtual Host File:
- Query:
sudo nano /etc/apache2/sites-available/your_domain.conf
- 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>
- Check Configuration is correct or not:
- Query:
sudo apache2ctl configtest
- Enable the Proxy module with Apache:
- Query:
sudo a2enmod proxy sudo a2enmod proxy_http
- Enable Virtual Host:
- Query:
cd /etc/apache2/sites-available/ sudo a2ensite your_domain.conf
- Restart Apache2:
- Query:
sudo service apache2 restart
- Start Node Express Application using pm2:
- Query:
cd /var/www/miniblog sudo NODE_ENV=production pm2 start app.js --update-env
- Save PM2 Process:
- Query:
sudo pm2 save
- Check PM2 Status:
- Query:
sudo pm2 status
- Connect to MongoDB shell with Super User:
- Query:
mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password"
- Show database:
- Query:
show dbs
- Create New Database:
- Query:
use database_name
- Create New User:
- Query:
db.createUser({user:"username", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]})
- Verify Users:
- Query:
show users
- Exit Mongo Shell:
- Query:
quit()
- Restart MongoDB:
- Query:
sudo service mongod restart
- Open .env:
- Query:
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:
- Query:
sudo service apache2 restart
- Start Node Express Application using pm2:
- Query:
cd /var/www/miniblog sudo NODE_ENV=production pm2 start app.js --update-env
- Save PM2 Process:
- Query:
sudo pm2 save
- Check error logs If you get any error:
- Query:
cd /var/log su cd apache2 cat error.log
- 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