Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Created August 4, 2023 20:19
Show Gist options
  • Save ultim8k/1fee46f6e1ca125812e3a693ae2de043 to your computer and use it in GitHub Desktop.
Save ultim8k/1fee46f6e1ca125812e3a693ae2de043 to your computer and use it in GitHub Desktop.
Next.js VPS hosting with docker and nginx
#!/bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/path/to/your/backup/$TIMESTAMP"
POSTGRES_CONTAINER="your_postgres_container_name"
DB_NAME="your_db_name"
DB_USER="your_db_user"
mkdir -p "$BACKUP_DIR"
# Backup database
docker exec "$POSTGRES_CONTAINER" pg_dump -U "$DB_USER" "$DB_NAME" > "$BACKUP_DIR/db.sql"
# Backup application files
tar -zcvf "$BACKUP_DIR/app.tar.gz" /path/to/your/app
# Optionally, copy to a remote server
# scp -r "$BACKUP_DIR" user@your_remote_server:/path/to/remote/backup
#!/bin/bash
# Define ports for blue and green environments
BLUE_PORT=5000
GREEN_PORT=6000
# Define nginx configuration files for blue and green
BLUE_CONF=/etc/nginx/conf.d/blue.conf
GREEN_CONF=/etc/nginx/conf.d/green.conf
# Define directories for blue and green
BLUE_DIR=/opt/blue
GREEN_DIR=/opt/green
# Define application repository
REPO=git@github.com:username/repo.git
# Check which environment is currently active
if [ $(readlink /etc/nginx/conf.d/current.conf) == $BLUE_CONF ]; then
IDLE=$GREEN_DIR
IDLE_PORT=$GREEN_PORT
ACTIVE=$BLUE_DIR
ACTIVE_PORT=$BLUE_PORT
else
IDLE=$BLUE_DIR
IDLE_PORT=$BLUE_PORT
ACTIVE=$GREEN_DIR
ACTIVE_PORT=$GREEN_PORT
fi
# Clone new version of the application
rm -rf $IDLE
git clone $REPO $IDLE
# Start new version of the application
cd $IDLE
docker-compose up -d --build -p $IDLE_PORT
# Run health check
sleep 10 # Wait for application to start
if ! curl -f http://localhost:$IDLE_PORT/health_check; then
echo "New version failed health check"
docker-compose down
exit 1
fi
# Switch nginx to the new version
ln -sf /etc/nginx/conf.d/idle.conf /etc/nginx/conf.d/current.conf
service nginx reload
# Stop and remove old version of the application
cd $ACTIVE
docker-compose down
# Remove versions older than the last 2
ls -td /opt/*/ | tail -n +3 | xargs rm -rf
version: '3'
services:
app:
container_name: nextjs-app
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/usr/src/app
ports:
- 3000:3000
depends_on:
- db
environment:
- DATABASE_URL=postgres://postgres:password@db:5432/postgres
db:
image: postgres:13-alpine
container_name: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=postgres
services:
app:
logging:
driver: syslog
options:
syslog-address: "tcp://logs.papertrailapp.com:12345"
tag: "service_name"
# Use an official Node runtime as the base image
FROM node:14-alpine
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Bundle app source
COPY . .
# Build app
RUN npm run build
# Expose port
EXPOSE 3000
# Start app
CMD ["npm", "start"]
# .github/workflows/main.yml
name: Node.js CI
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://app: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;
}
}
{
"name": "nextjs-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment