-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·77 lines (62 loc) · 1.95 KB
/
deploy.sh
File metadata and controls
executable file
·77 lines (62 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
set -euo pipefail
source "$(dirname "$0")/lib/logger.sh"
if [ -z "${1:-}" ]; then
log_info "Usage: ./deploy.sh <droplet-name>"
exit 1
fi
DROPLET=$1
IP=$(doctl compute droplet get "$DROPLET" -o json | jq -r '.[0].networks.v4[0].ip_address' 2>/dev/null || log_info "IP not found")
log_info "🚀 Deploying to $DROPLET ($IP)..."
# Create app directory on server if missing
ssh -o StrictHostKeyChecking=no deploy@"$IP" '
sudo mkdir -p /var/www/myapp
sudo chown deploy:deploy /var/www/myapp
'
# Sync the app
rsync -avz --delete app/ deploy@"$IP":/var/www/myapp/
ssh -o StrictHostKeyChecking=no deploy@"$IP" '
cd /var/www/myapp
python3 -m venv venv
venv/bin/pip install -r requirements.txt
venv/bin/pip install gunicorn
# Systemd service
sudo tee /etc/systemd/system/gunicorn.service > /dev/null <<EOG
[Unit]
Description=Gunicorn instance for myapp
After=network.target
[Service]
User=deploy
Group=www-data
WorkingDirectory=/var/www/myapp
Environment="PATH=/var/www/myapp/venv/bin"
ExecStart=/var/www/myapp/venv/bin/gunicorn --workers 3 --bind unix:/var/www/myapp/myapp.sock app:app
[Install]
WantedBy=multi-user.target
EOG
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
sudo systemctl enable gunicorn
# Nginx
sudo tee /etc/nginx/sites-available/myapp > /dev/null <<EON
server {
listen 80;
server_name _;
location / {
proxy_pass http://unix:/var/www/myapp/myapp.sock;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location /static/ {
alias /var/www/myapp/static/;
}
}
EON
sudo ln -sf /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl restart nginx
'
log_info "✅ Deployment complete!"
log_info "🌐 Your app is live at http://$IP"