Deployment
When you're ready to deploy your Dracory application to a production environment, there are several approaches you can take. This guide will walk you through the process of preparing your application for deployment and the various deployment options available.
Preparing for Deployment
Before deploying your Dracory application, you should take the following steps to ensure it's ready for production:
- Update Environment Variables - Make sure your .env file is configured correctly for production:
APP_ENV=production
APP_DEBUG=false
APP_URL=https://your-production-domain.com
- Optimize for Production - Run the following commands to optimize your application:
go build -ldflags="-s -w" -o app main.go
- Set Up a Production Database - Configure your production database and update your database configuration accordingly.
- Configure Web Server - Set up a proper web server like Nginx or Apache to serve your application.
- Set Up SSL - Secure your application with SSL certificates, preferably using Let's Encrypt.
Deployment Options
Traditional Server Deployment
You can deploy your Dracory application on a traditional server (VPS or dedicated server) by following these steps:
- Build your application for the target platform:
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o app main.go
- Transfer the compiled binary and necessary files to your server:
scp app .env public/* user@your-server:/path/to/application
- Set up a systemd service to run your application:
[Unit]
Description=Dracory Application
After=network.target
[Service]
User=www-data
WorkingDirectory=/path/to/application
ExecStart=/path/to/application/app
Restart=always
[Install]
WantedBy=multi-user.target
- Configure Nginx as a reverse proxy:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
- Set up SSL with Let's Encrypt:
sudo certbot --nginx -d your-domain.com
Docker Deployment
Docker provides a consistent environment for your application. Here's how to deploy your Dracory application using Docker:
- Create a Dockerfile in your project root:
FROM golang:1.17-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -ldflags="-s -w" -o app main.go
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/app .
COPY --from=builder /app/.env .
COPY --from=builder /app/public ./public
EXPOSE 8080
CMD ["./app"]
- Build and tag your Docker image:
docker build -t your-username/dracory-app:latest .
- Run your Docker container:
docker run -p 8080:8080 your-username/dracory-app:latest
Cloud Deployment
Dracory applications can be deployed to various cloud platforms:
Google Cloud Run
Cloud Run is a fully managed platform that automatically scales your stateless containers:
# Build and tag your Docker image
docker build -t gcr.io/your-project/dracory-app .
# Push to Google Container Registry
docker push gcr.io/your-project/dracory-app
# Deploy to Cloud Run
gcloud run deploy dracory-app --image gcr.io/your-project/dracory-app --platform managed
AWS Elastic Beanstalk
Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications:
# Install the EB CLI
pip install awsebcli
# Initialize your EB project
eb init
# Create an environment and deploy
eb create dracory-production
Heroku
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud:
# Create a Procfile
echo "web: ./app" > Procfile
# Create a new Heroku app
heroku create
# Set buildpack
heroku buildpacks:set heroku/go
# Deploy to Heroku
git push heroku master
Continuous Integration and Deployment
Setting up CI/CD pipelines can automate your deployment process. Here are examples for popular CI/CD platforms:
GitHub Actions
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Build
run: go build -v -o app main.go
- name: Deploy to Production
run: |
# Your deployment script here
GitLab CI/CD
# .gitlab-ci.yml
stages:
- build
- test
- deploy
build:
stage: build
image: golang:1.17
script:
- go build -o app main.go
artifacts:
paths:
- app
test:
stage: test
image: golang:1.17
script:
- go test ./...
deploy:
stage: deploy
script:
- apt-get update -qy
- apt-get install -y openssh-client
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $SERVER_IP >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- scp app $SERVER_USER@$SERVER_IP:/path/to/application/
- ssh $SERVER_USER@$SERVER_IP "systemctl restart dracory-app"
only:
- main
Monitoring and Logging
Once your application is deployed, it's important to set up monitoring and logging to ensure it's running smoothly:
- Prometheus and Grafana - For metrics collection and visualization
- ELK Stack - For centralized logging (Elasticsearch, Logstash, Kibana)
- New Relic or Datadog - For application performance monitoring
Deployment Checklist
Before going live with your production deployment, make sure to check the following:
- SSL certificates are properly configured
- Database migrations have been applied
- Environment variables are correctly set for production
- Static assets are properly served
- Error logging and monitoring are set up
- Backups are configured
- Load testing has been performed