Inteliny
DevelopmentLevel: Intermediate30mVerified Production Blueprint

How to Deploy a Full-Stack MERN Application Using Docker, Nginx & GitHub Actions

Learn to deploy a full-stack MERN application using Docker, Nginx, and GitHub Actions by the end of this guide

Inteliny Engineering

Principal Architect

Overview & Architecture Scope

This comprehensive guide is designed for developers and DevOps engineers looking to deploy a full-stack MERN (MongoDB, Express, React, Node.js) application using Docker, Nginx, and GitHub Actions. By the end of this guide, you will have a fully deployed MERN application with a reverse proxy, SSL encryption, and automated deployment. To follow along, you should have basic knowledge of Docker, Nginx, and GitHub Actions, as well as a GitHub account and an Ubuntu server. This guide covers the entire deployment process, from setting up the project architecture to monitoring and logging. The MERN stack is a popular choice for web development, and using Docker, Nginx, and GitHub Actions makes the deployment process efficient and scalable. With this guide, you will be able to deploy your MERN application securely and efficiently.

Prerequisites & System Requirements

Ensure your development workstation or staging server fulfills the following prerequisites before initiating commands:

Node.js v18.0+ runtime environment
MongoDB v6.0+ database cluster
Active AWS or Cloudflare account with DNS access
Linux Ubuntu 22.04 LTS server instance
Basic knowledge of CLI bash & Git workflow

Target System Architecture Diagram

Browser
NGINX
Node / Express
MongoDB / Redis

Interactive Execution Checklist

0/10 Completed

Step-by-Step Implementation Guide

1

Step 1: Set Up Project Architecture

First, you need to set up your project architecture. This includes creating a new React app, setting up a Node.js server with Express, and creating a MongoDB database. You will also need to create a Dockerfile for your React app and your Node.js server.

Execute Command Terminal:

bash
docker build -t my-react-app .
docker build -t my-node-server .
Make sure to replace 'my-react-app' and 'my-node-server' with your actual app and server names.
2

Step 2: Configure Docker Compose

Next, you need to configure Docker Compose to manage your containers. This includes creating a docker-compose.yml file that defines your services and their dependencies.

Execute Command Terminal:

yaml
version: '3'
services:
  react-app:
    build: ./react-app
    ports:
      - '3000:3000'
  node-server:
    build: ./node-server
    ports:
      - '3001:3001'
Make sure to update the build paths and port numbers to match your project architecture.
3

Step 3: Set Up Nginx Reverse Proxy

To set up Nginx as a reverse proxy, you need to create an Nginx configuration file that defines your proxy settings. You will also need to install Nginx on your Ubuntu server.

Execute Command Terminal:

bash
sudo apt-get update
sudo apt-get install nginx
Make sure to update the Nginx configuration file to match your project architecture.
4

Step 4: Configure GitHub Actions

To automate your deployment, you need to configure GitHub Actions. This includes creating a new workflow file that defines your deployment steps.

Execute Command Terminal:

yaml
name: Deploy MERN App
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Login to DockerHub
        uses: docker/login-action@v1
      - name: Build and push images
        run: |
          docker build -t my-react-app .
          docker build -t my-node-server .
          docker tag my-react-app:latest ${{ secrets.DOCKER_USERNAME }}/my-react-app:latest
          docker tag my-node-server:latest ${{ secrets.DOCKER_USERNAME }}/my-node-server:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/my-react-app:latest
          docker push ${{ secrets.DOCKER_USERNAME }}/my-node-server:latest
Make sure to update the workflow file to match your project architecture and DockerHub credentials.
5

Step 5: Set Up SSL Encryption

To set up SSL encryption, you need to obtain an SSL certificate and update your Nginx configuration file to use the certificate.

Execute Command Terminal:

bash
sudo certbot certonly --nginx -d example.com
Make sure to update the domain name to match your project architecture.
6

Step 6: Configure Domain and Monitoring

To configure your domain and monitoring, you need to update your Nginx configuration file to use your domain name and set up monitoring tools such as Prometheus and Grafana.

Execute Command Terminal:

nginx
server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Make sure to update the domain name and monitoring tools to match your project architecture.
7

Step 7: Set Up Logging and Security Hardening

To set up logging and security hardening, you need to configure logging tools such as ELK Stack and implement security hardening measures such as firewall rules and access controls.

Execute Command Terminal:

bash
sudo ufw allow http
sudo ufw allow https
Make sure to update the logging and security hardening measures to match your project architecture.
8

Step 8: Implement Backup and Rollback Strategy

To implement a backup and rollback strategy, you need to configure backup tools such as MongoDB backup and implement rollback measures such as Docker container rollback.

Execute Command Terminal:

bash
sudo mongodump --uri mongodb://localhost:27017/
Make sure to update the backup and rollback strategy to match your project architecture.
9

Step 9: Optimize Performance

To optimize performance, you need to configure performance optimization tools such as PM2 and implement performance optimization measures such as caching and load balancing.

Execute Command Terminal:

bash
sudo pm2 start app.js
Make sure to update the performance optimization measures to match your project architecture.
10

Step 10: Troubleshoot Common Issues

To troubleshoot common issues, you need to identify common issues such as container crashes and network connectivity issues, and implement troubleshooting measures such as logging and monitoring.

Execute Command Terminal:

bash
sudo docker logs -f my-react-app
Make sure to update the troubleshooting measures to match your project architecture.

Pro Tips & Optimizations

Use a consistent naming convention for your containers and services.
Make sure to update your Docker Compose file to match your project architecture.
Use a reverse proxy to improve security and scalability.
Implement logging and monitoring to improve performance and troubleshooting.

Common Pitfalls to Avoid

Forgetting to update the Docker Compose file to match the project architecture.
Not implementing security hardening measures such as firewall rules and access controls.
Not configuring logging and monitoring tools to improve performance and troubleshooting.
Conclusion & Next Steps

By following these steps, you can deploy a full-stack MERN application using Docker, Nginx, and GitHub Actions. Remember to update your project architecture, configure Docker Compose, set up Nginx as a reverse proxy, and implement security hardening measures to improve security and scalability. With this guide, you can efficiently and securely deploy your MERN application and improve performance and troubleshooting.

Production Best Practices & Hardening

Security Hardening

Disable root SSH access, enforce key-based auth, and enable UFW firewall on ports 80/443.

Memory Management

Set Node.js max-old-space-size to 80% of total RAM to avoid Linux OOM-killer crashes.

Frequently Asked Questions

Yes, all NGINX, Docker, and PM2 deployment steps can be packaged into Infrastructure as Code (IaC) playbooks.

Need Help Implementing This?

Partner with Inteliny's principal architects to audit your stack, automate CI/CD, and accelerate deployment.

How to Deploy a Full-Stack MERN Application Using Docker, Nginx & GitHub Actions | Inteliny Knowledge Base