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.

Step-by-Step Instructions

1. Login to Your Domain Provider Website

Navigate to Manage DNS settings.

2. Add DNS 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
            
        

3. Prepare Your Project

Zip your project folder using any compression software (e.g., WinZip).

4. Copy Zip File to Remote Server

Use the following command:

            
                scp -P Port_number Source_File_Path Destination_Path
            
        

Example:

            
                scp -P 22 miniblog.zip username@your.server.ip:
            
        

5. Access Remote Server via SSH

Connect to your server:

            
                ssh -p PORT username@your.server.ip
            
        

6. Verify Required Software

Check installed software:

            
                apache2 -v
                node -v
                npm -v
                pm2 --version
                mongod --version
            
        

7. Install Required Software

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
            
        

8. Add PM2 Process on Startup

            
                sudo pm2 startup
            
        

9. Install MongoDB

Follow the instructions from here.

10. Verify Apache2 is Active and Running

            
                sudo service apache2 status
            
        

11. Verify Web Server Ports are Open

            
                sudo ufw status verbose
            
        

12. Unzip the Copied Zip File

            
                unzip miniblog.zip
            
        

13. Move Project Folder to Web Server Directory

            
                sudo mv miniblog /var/www
            
        

14. Go to Your Project Directory

            
                cd /var/www/miniblog
            
        

15. Install Dependencies

            
                npm install
            
        

16. Create Virtual Host File

            
                sudo nano /etc/apache2/sites-available/your_domain.conf
            
        

Add the following code to the file:

            
                
                    ServerName www.example.com
                    ServerAdmin contact@example.com
                    ProxyPreserveHost On
                    ProxyPass / http://127.0.0.1:8000/
                    ProxyPassReverse / http://127.0.0.1:8000/
                    
                        AllowOverride All
                    
                
            
        

17. Check Apache Configuration

            
                sudo apache2ctl configtest
            
        

18. Enable Proxy Module

            
                sudo a2enmod proxy
                sudo a2enmod proxy_http
            
        

19. Enable Virtual Host

            
                sudo a2ensite your_domain.conf
            
        

20. Restart Apache2

            
                sudo service apache2 restart
            
        

21. Start Node Express Application using PM2

            
                cd /var/www/miniblog
                sudo NODE_ENV=production pm2 start app.js --update-env
            
        

22. Save PM2 Process

            
                sudo pm2 save
            
        

23. Check PM2 Status

            
                sudo pm2 status
            
        

24. Connect to MongoDB Shell

To create a super user, follow this guide.

            
                mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password"
            
        

25. Show Databases

            
                show dbs
            
        

26. Create New Database

            
                use database_name
            
        

27. Create New User

            
                db.createUser({user:"username", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]})
            
        

28. Verify Users

            
                show users
            
        

29. Exit Mongo Shell

            
                quit()
            
        

30. Restart MongoDB

            
                sudo service mongod restart
            
        

31. Open .env File

            
                cd /var/www/miniblog
                sudo nano .env
            
        

Make the following 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"
            
        

32. Restart Apache2

            
                sudo service apache2 restart
            
        

33. Start Node Express Application using PM2

            
                cd /var/www/miniblog
                sudo NODE_ENV=production pm2 start app.js --update-env
            
        

34. Save PM2 Process

            
                sudo pm2 save
            
        

35. Check Error Logs

If you encounter any errors:

            
                cd /var/log
                su
                cd apache2
                cat error.log
            
        

36. 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
  • Restore Last Saved PM2 Process: sudo pm2 resurrect
  • Clear PM2 Dump File: sudo pm2 cleardump
  • Remove PM2 Process from Startup: sudo pm2 unstartup