-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
113 lines (91 loc) · 3.3 KB
/
deploy.sh
File metadata and controls
113 lines (91 loc) · 3.3 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Exit on any error
set -e
# 1. Update the system and install dependencies
echo "Updating system and installing dependencies..."
sudo apt update -y
sudo apt upgrade -y
sudo apt install -y python3-pip python3-dev libpq-dev nginx
# 2. Navigate to the project folder (the repo is already cloned)
# cd /path/to/your/django/project
# 3. Install Python dependencies globally (no virtualenv)
echo "Installing project dependencies..."
sudo pip3 install -r requirements.txt
# 4. Set up Django settings for production
echo "Configuring Django settings..."
# You need to manually edit your `settings.py` file to include your domain or IP in `ALLOWED_HOSTS`
# Make sure DEBUG is set to False and that your DATABASES configuration is correct.
# Example:
# ALLOWED_HOSTS = ['your_domain.com', 'server_ip']
# DEBUG = False
# Ensure that DATABASES is configured correctly for SQLite
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# 5. Collect static files for production
echo "Collecting static files..."
python3 manage.py collectstatic --noinput
# 6. Set up Gunicorn to run the app
echo "Setting up Gunicorn..."
# Gunicorn is installed via the requirements.txt
# Here we will run Gunicorn with the desired number of workers
gunicorn --workers 3 --bind unix:/path/to/your/project/myproject.sock myproject.wsgi:application
# 7. Configure Gunicorn as a systemd service
echo "Configuring Gunicorn as a service..."
cat <<EOL | sudo tee /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon for Django project
After=network.target
[Service]
User=your_user
Group=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/path/to/your/project/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
EOL
# Enable and start the Gunicorn service
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
# 8. Configure NGINX to proxy requests to Gunicorn
echo "Configuring NGINX..."
cat <<EOL | sudo tee /etc/nginx/sites-available/myproject
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://unix:/path/to/your/project/myproject.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 /path/to/your/project/static/;
}
location /media/ {
alias /path/to/your/project/media/;
}
}
EOL
# Enable the site in NGINX and restart the service
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo systemctl restart nginx
# 9. Install and configure SSL (optional, with Let's Encrypt)
echo "Setting up SSL with Let's Encrypt..."
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com
# 10. Restart NGINX to apply SSL
sudo systemctl restart nginx
# 11. Run Django migrations (even for SQLite, just in case)
echo "Running migrations..."
python3 manage.py migrate
# 12. Check that everything is running
echo "Checking status of services..."
sudo systemctl status gunicorn
sudo systemctl status nginx
echo "Deployment completed successfully!"