Deploy a Node Express Project on Apache Web Server with GitHub Integration
Table Of Content
- 1. Understanding PM2
- 2. Setting Up DNS Records
- 3. Pushing Project to GitHub
- 4. Accessing Remote Server via SSH
- 5. Verifying Software Installation
- 6. Installing Apache and Node.js
- 7. Installing PM2
- 8. Adding PM2 Process on Startup
- 9. Installing MongoDB
- 10. Setting Up GitHub SSH Key
- 11. Clone Project from GitHub
- 12. Moving Project to Web Server Directory
- 13. Setting Up Apache Virtual Host
- 14. Configuring Apache Virtual Host
- 15. Enabling Virtual Host and Restarting Apache
- 16. Starting Node Express Application with PM2
- 17. Automating Deployment with GitHub Actions
- Conclusion
Deploy a Node Express Project on Apache Web Server with GitHub Integration
In this guide, we’ll walk you through deploying a Node Express project on an Apache web server using VPS hosting, including integrating GitHub to automate the deployment process.
1. Understanding PM2
PM2 is a robust process manager for Node.js applications. It ensures that your processes are automatically restarted whenever your server reboots, providing essential stability and reliability for your applications.
2. Setting Up DNS Records
-
Login to your domain provider website.
-
Navigate to Manage DNS.
-
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
3. Pushing Project to GitHub
- Open your project in VS Code.
- Add a
.gitignore
file and create a.env.example
file. - Push your project to GitHub as a private repository.
4. Accessing Remote Server via SSH
SSH into your remote server using the command:
ssh -p PORT USERNAME@HOSTIP
5. Verifying Software Installation
Check that all required software is installed:
apache2 -v
node -v
npm -v
pm2 --version
mongod --version
git --version
6. Installing Apache and Node.js
Install Apache and Node.js on your server:
sudo apt install apache2
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&
sudo apt-get install -y nodejs
7. Installing PM2
Install PM2 globally on your server:
sudo npm install -g pm2@latest
8. Adding PM2 Process on Startup
Ensure PM2 starts on server boot:
sudo pm2 startup
9. Installing MongoDB
Install MongoDB on your server (follow specific installation instructions for MongoDB).
10. Setting Up GitHub SSH Key
-
Generate SSH keys on your server:
ssh-keygen -t ed25519 -C "your_email@example.com"
-
Add the public SSH key to your GitHub account.
11. Clone Project from GitHub
Clone your project from GitHub to your server:
git clone https_repo_path
12. Moving Project to Web Server Directory
Move your project folder to the web server’s public directory:
sudo mv project_folder_name /var/www
13. Setting Up Apache Virtual Host
Create a virtual host file for your domain:
sudo nano /etc/apache2/sites-available/your_domain.conf
14. Configuring Apache Virtual Host
Add the following configuration for your domain in the virtual host file:
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin contact@example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
<Directory "/var/www/project_folder_name">
AllowOverride All
</Directory>
</VirtualHost>
15. Enabling Virtual Host and Restarting Apache
Enable the virtual host and restart Apache:
sudo a2ensite your_domain.conf
sudo service apache2 restart
16. Starting Node Express Application with PM2
Start your Node Express application using PM2:
cd /var/www/project_folder_name
sudo NODE_ENV=production pm2 start app.js --watch --update-env --ignore-watch="user_upload_path"
17. Automating Deployment with GitHub Actions
-
Create Deployment Scripts:
-
Create
.scripts/deploy.sh
:#!/bin/bash set -e echo "Deployment started..." git pull origin master echo "New changes copied to server !" echo "Installing Dependencies..." npm install --yes echo "PM2 Reload" pm2 reload app_name echo "Deployment Finished!"
-
Set file permissions:
git update-index --add --chmod=+x deploy.sh
-
-
Create GitHub Actions Workflow:
-
Create
.github/workflows/deploy.yml
:name: Deploy on: push: branches: ["master"] pull_request: branches: ["master"] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Deploy to Server uses: appleboy/ssh-action@master with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} port: ${{ secrets.PORT }} key: ${{ secrets.SSHKEY }} script: "cd /var/www/project_folder_name && ./.scripts/deploy.sh"
-
-
Add Repository Secrets on GitHub:
- Go to GitHub Repo > Settings > Secrets and Variables > Actions.
- Add secrets:
HOST
,PORT
,USERNAME
, andSSHKEY
.
-
Generate SSH Key for GitHub Actions:
ssh-keygen -f key_path -t ed25519 -C "your_email@example.com"
-
Add SSH Key to GitHub and Remote Server:
- Add the public SSH key as a deploy key on GitHub.
- Use the private key for GitHub Actions secrets.
-
Commit and Push Changes to GitHub Repo.
- Your deployment should now be automated. Track the process via the GitHub Actions tab.
Conclusion
By following this comprehensive guide, you can deploy your Node Express project on an Apache web server with VPS hosting and integrate GitHub for automated deployments. This setup will help ensure a smooth and efficient deployment process for your applications.