Tool Logo
Apache

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

How to Point Domain and Host Node Express Project on Apache Web Server VPS Hosting
5 min read
#Apache

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.
  2. Navigate to Manage DNS:

    • Look for the option to manage your DNS settings. This is usually found in the domain management section.
  3. 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 |

  4. 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_Path
      

      Example:

      scp -P 22 miniblog.zip [email protected]:
      
  5. Get Access to Remote Server via SSH:

    ssh -p PORT USERNAME@HOSTIP
    

    Example:

    ssh -p 22 [email protected]
    
  6. Verify that all Required Software is Installed:

    apache2 -v
    node -v
    npm -v
    pm2 --version
    mongod --version
    
  7. Install Apache:

    sudo apt install apache2
    
  8. Install Node and npm:

    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&
    sudo apt-get install -y nodejs
    
  9. Install PM2:

    sudo npm install -g pm2@latest
    
  10. Add PM2 Process on Startup:

    sudo pm2 startup
    
  11. Install MongoDB 6.0.3 on Ubuntu 22.04:

    • Follow the instructions in InstallConfigMongoDB.md.
  12. Verify Apache2 is Active and Running:

    sudo service apache2 status
    
  13. Verify Web Server Ports are Open and Allowed through Firewall:

    sudo ufw status verbose
    
  14. Run ls Command to Verify that the Zip File is Present:

    ls
    
  15. Unzip the Copied Zip File:

    unzip zip_file_name
    

    Example:

    unzip miniblog.zip
    
  16. Move Project Folder from User Home to Web Server Public Directory:

    sudo mv project_folder_name Destination_Path
    

    Example:

    sudo mv miniblog /var/www
    
  17. Go to Your Project Folder:

    cd /var/www/project_folder_name
    

    Example:

    cd /var/www/miniblog
    
  18. Install Dependencies:

    npm install
    
  19. Install Node.js MongoDB Driver (If Needed):

    npm install mongodb
    
  20. Create Virtual Host File:

    sudo nano /etc/apache2/sites-available/your_domain.conf
    
  21. 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>
    
  22. Check Configuration is Correct or Not:

    sudo apache2ctl configtest
    
  23. Enable the Proxy Module with Apache:

    sudo a2enmod proxy
    sudo a2enmod proxy_http
    
  24. Enable Virtual Host:

    cd /etc/apache2/sites-available/
    sudo a2ensite your_domain.conf
    
  25. Restart Apache2:

    sudo service apache2 restart
    
  26. Start Node Express Application Using PM2:

    cd /var/www/miniblog
    sudo NODE_ENV=production pm2 start app.js --update-env
    
  27. Save PM2 Process:

    sudo pm2 save
    
  28. Check PM2 Status:

    sudo pm2 status
    
  29. Connect to MongoDB Shell with Super User:

    mongosh --port 27017 --authenticationDatabase "database_name_where_user_stored" -u "username" -p "password"
    
  30. Show Database:

    show dbs
    
  31. Create New Database:

    use database_name
    
  32. Create New User:

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

    show users
    
  34. Exit Mongo Shell:

    quit()
    
  35. Restart MongoDB:

    sudo service mongod restart
    
  36. Open .env File:

    cd /var/www/miniblog
    sudo nano .env
    
  37. 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"
    
  38. Restart Apache2:

    sudo service apache2 restart
    
  39. Start Node Express Application Using PM2:

    cd /var/www/miniblog
    sudo NODE_ENV=production pm2 start app.js --update-env
    
  40. Save PM2 Process:

    sudo pm2 save
    
  41. Check Error Logs If You Get Any Error:

    cd /var/log
    su
    cd apache2
    cat error.log
    
  42. 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