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:
Target System Architecture Diagram
Interactive Execution Checklist
Step-by-Step Implementation Guide
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:
docker build -t my-react-app .
docker build -t my-node-server .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:
version: '3'
services:
react-app:
build: ./react-app
ports:
- '3000:3000'
node-server:
build: ./node-server
ports:
- '3001:3001'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:
sudo apt-get update
sudo apt-get install nginxStep 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:
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:latestStep 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:
sudo certbot certonly --nginx -d example.comStep 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:
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;
}
}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:
sudo ufw allow http
sudo ufw allow httpsStep 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:
sudo mongodump --uri mongodb://localhost:27017/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:
sudo pm2 start app.jsStep 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:
sudo docker logs -f my-react-appPro Tips & Optimizations
Common Pitfalls to Avoid
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.