Skip to content

Latest commit

 

History

History
260 lines (253 loc) · 6.02 KB

Deploy_NodeExpress_Apache.md

File metadata and controls

260 lines (253 loc) · 6.02 KB

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. Restarting PM2 with the processes it manages every time your server boots/reboots is critical. One of PM2’s key features is support for a startup script (generated dynamically based on the default init system on your server), that restarts PM2 and your processes at every server restart.

  • Login to Your Domain Provider Website
  • Navigate to Manage DNS
  • Add 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 Make Your Project Folder a Zip File using any of the software e.g. winzip
  • Open Command Prompt
  • Copy Zip File from Local Windows Machine to Linux Remote Server
Syntax:- scp -P Port_number Source_File_Path Destination_Path
Example:- scp -P 22 miniblog.zip [email protected]:
  • Get Access to Remote Server via SSH
Syntax:- ssh -p PORT USERNAME@HOSTIP
Example:- ssh -p 22 [email protected]

Note:- Run Below Commands on Your Remote Server Linux Machine or VPS, Not on Your Local Windows Machine

  • Verify that all required softwares are 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
sudo service apache2 status
  • Verify Web Server Ports are Open and Allowed through Firewall
sudo ufw status verbose
  • Run ls command to verify that the zip file is present
ls
  • Unzip the Copied Zip File
Syntax:- unzip zip_file_name
Example:- unzip miniblog.zip
  • Move Project Folder from User Home to Web Server Public Directory
Syntax:- sudo mv project_folder_name Destination_Path
Example:- sudo mv miniblog /var/www
  • Go to Your Project Directory
Syntax:- cd /var/www/project_folder_name
Example:- 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 Following Code in Virtual Host File
  • Change the IP and Port According to your Node Application 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
Syntax:- mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password"
Example:- mongosh --port 27017 --authenticationDatabase "admin" -u "superuser" -p "Hello123456"
  • Show database
show dbs
  • Create New Database
Syntax:- use database_name
Example:- use blogdb
  • Create New User
Syntax:- db.createUser({user:"username", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]})
Example:- db.createUser({user:"rahul", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"blogdb"}]})
  • Verify Users
show users
  • Exit Mongo Shell
quit()
  • Access Mongo Shell as User to Verify the User Credentials
Syntax:- mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password"
Example:- mongosh --port 27017 --authenticationDatabase "blogdb" -u "rahul" -p "Hello123456"
  • Exit Mongo Shell
quit()
  • Restart MongoDB
sudo service mongod restart
  • Open .env
cd /var/www/miniblog
sudo nano .env
  • Make below 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
  • You can check error logs If you get any error:
cd /var/log
su
cd apache2
cat error.log
  • You can 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 Process
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