Setting Up a Node.js Web Application with Git Auto Deployment and Apache Proxy

Setting Up a Node.js Web Application with Git Auto Deployment and Apache Proxy

Deploying a Node.js application can be a complex task, but with a structured approach, it becomes manageable. This blog post outlines the complete setup of a Node.js application named YourProject, from installing dependencies to configuring Apache as a reverse proxy and enabling HTTPS.


Prerequisites

  • A server with Ubuntu installed.

  • Access to the root account or sudo privileges.

  • Basic understanding of Git, Node.js, and server management.


Steps to Deploy YourProject

1. Update System and Install Dependencies Start by updating the package manager and installing essential tools like Git and Node.js:

sudo apt-get update
sudo apt install git
curl -sL https://deb.nodesource.com/setup_20.x -o /tmp/nodesource_setup.sh
sudo bash /tmp/nodesource_setup.sh
sudo apt install nodejs

2. Set Up Git Deployment Create directories for your repository and deployment destination:

mkdir -p ~/apps/YourProject/repo
mkdir -p ~/apps/YourProject/dest
cd ~/apps/YourProject/repo
git --bare init

Create a post-receive Git hook to automate deployment:

nano hooks/post-receive

Add the following script:

#!/bin/bash -l
echo 'post-receive: Triggered'
cd ~/apps/YourProject/dest/
echo 'post-receive: git checkout...'
git --git-dir=/root/apps/YourProject/repo/ --work-tree=/root/apps/YourProject/dest/ checkout master -f
echo 'post-receive: npm install & build...'
npm install && cd client && npm install && npm run build
forever restart YourProject

Make the hook executable:

chmod ug+x hooks/post-receive

3. Configure Environment Variables Navigate to the destination directory and create an .env file for your environment variables:

cd ../dest
nano .env

Add the following configuration:

NODE_ENV=production
PORT=5000
MONGO_URL=mongodb+srv://random_user:random_password@randomproject.mongodb.net/RandomProject?retryWrites=true&w=majority&appName=RandomProject
JWT_SECRET=RandomSecretJWT
JWT_EXPIRES_IN=7d
CLOUD_NAME=randomcloud123
CLOUD_API_KEY=123456789012345
CLOUD_API_SECRET=RandomCloudSecretKey12345

4. Set Up Apache as a Reverse Proxy Install and configure Apache:

sudo apt update
sudo apt install apache2 -y
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite headers expires

Create a virtual host configuration for your domain:

sudo nano /etc/apache2/sites-available/yourdomain.com.conf

Add the following configuration:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / http://127.0.0.1:5100/
    ProxyPassReverse / http://127.0.0.1:5100/
</VirtualHost>

Enable the site and restart Apache:

sudo a2dissite 000-default
sudo a2ensite yourdomain.com.conf
sudo systemctl restart apache2

5. Add SSL Using Certbot Install Certbot and enable HTTPS:

sudo apt install certbot python3-certbot-apache
sudo certbot -d yourdomain.com -d www.yourdomain.com --apache --agree-tos -m musavir119s@gmail.com --no-eff-email --redirect

6. Install Forever for Process Management Install forever globally to keep your application running:

npm install forever -g

7. Clone the Repository Locally For local development, clone the repository:

mkdir ~/hostinger
cd ~/hostinger
git clone git@github.com:username/YourProject.git

Add the remote for deployment:

cd YourProject
git remote add hostinger ssh://root@148.135.136.174/root/apps/YourProject/repo/

Push your code to deploy it:

git push hostinger master

Conclusion With this setup, you can seamlessly deploy your Node.js application using Git hooks and manage it with Apache as a reverse proxy. This ensures a robust and secure deployment process with automated builds and SSL support.