Last active
May 28, 2025 20:34
-
-
Save sh1mu7/01f73f3762fc1bb11ce218a6403c661f to your computer and use it in GitHub Desktop.
Django and React Deployment Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: sh1mu7 | |
# Date: 2024-02-06 | |
# Description: Django deployment automation. | |
# Prompt for service name | |
# shellcheck disable=SC2162 | |
#!/bin/bash | |
#!/bin/bash | |
echo "=== Deployment Automation Script ===" | |
echo "Choose deployment type:" | |
echo "1) React" | |
echo "2) Django" | |
read -p "Enter 1 or 2: " DEPLOY_TYPE | |
if [ "$DEPLOY_TYPE" == "1" ]; then | |
# React deployment | |
echo "=== React App Nginx Deployment Script ===" | |
read -p "Enter your domain (e.g., example.com): " DOMAIN | |
read -p "Enter absolute path to React dist/build folder: " REACT_PATH | |
if [ ! -d "$REACT_PATH" ]; then | |
echo "β Error: Directory does not exist: $REACT_PATH" | |
exit 1 | |
fi | |
NGINX_CONF="/etc/nginx/sites-available/$DOMAIN" | |
sudo bash -c "cat > $NGINX_CONF" <<EOL | |
server { | |
listen 80; | |
server_name $DOMAIN; | |
root $REACT_PATH; | |
index index.html; | |
location / { | |
try_files \$uri /index.html; | |
} | |
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|otf|eot)$ { | |
expires 1y; | |
access_log off; | |
add_header Cache-Control "public"; | |
} | |
error_page 404 /index.html; | |
} | |
EOL | |
sudo ln -sf "$NGINX_CONF" "/etc/nginx/sites-enabled/" | |
echo "π§ Testing Nginx config..." | |
sudo nginx -t | |
if [ $? -eq 0 ]; then | |
echo "β Reloading Nginx..." | |
sudo systemctl reload nginx | |
else | |
echo "β Nginx config test failed. Fix the issues and try again." | |
exit 1 | |
fi | |
# Install SSL with Certbot | |
echo "π Installing Let's Encrypt SSL for $DOMAIN" | |
sudo certbot --nginx -d "$DOMAIN" | |
echo "π React site deployed at https://$DOMAIN" | |
elif [ "$DEPLOY_TYPE" == "2" ]; then | |
# Django deployment | |
echo "=== Django App Deployment Script ===" | |
read -p "Enter service name: " NAME | |
read -p "Enter domain name: " DOMAIN | |
read -p "Enter project directory: " PROJECT_DIR | |
if [ -z "$NAME" ] || [ -z "$DOMAIN" ] || [ -z "$PROJECT_DIR" ]; then | |
echo "β Missing required fields." | |
exit 1 | |
fi | |
[ -f "/etc/systemd/system/$NAME.service" ] && rm "/etc/systemd/system/$NAME.service" | |
[ -f "/etc/systemd/system/$NAME.socket" ] && rm "/etc/systemd/system/$NAME.socket" | |
[ -f "/etc/nginx/sites-available/$DOMAIN" ] && rm "/etc/nginx/sites-available/$DOMAIN" | |
systemctl daemon-reload | |
cat <<EOL > "/etc/systemd/system/$NAME.service" | |
[Unit] | |
Description=$NAME daemon | |
Requires=${NAME}.socket | |
After=network.target | |
[Service] | |
User=root | |
Group=www-data | |
WorkingDirectory=$PROJECT_DIR | |
ExecStart=$PROJECT_DIR/venv/bin/gunicorn Config.wsgi:application -w 2 --bind unix:/run/${NAME}.sock --access-logfile - | |
[Install] | |
WantedBy=multi-user.target | |
EOL | |
cat <<EOL > "/etc/systemd/system/$NAME.socket" | |
[Unit] | |
Description=$NAME socket | |
[Socket] | |
ListenStream=/run/${NAME}.sock | |
[Install] | |
WantedBy=sockets.target | |
EOL | |
cat <<EOL > "/etc/nginx/sites-available/$DOMAIN" | |
server { | |
listen 80; | |
server_name $DOMAIN; | |
location = /favicon.ico { | |
access_log off; | |
log_not_found off; | |
} | |
location /static/ { | |
root $PROJECT_DIR; | |
} | |
location /media/ { | |
root $PROJECT_DIR; | |
} | |
location / { | |
include proxy_params; | |
proxy_pass http://unix:/run/${NAME}.sock; | |
} | |
client_max_body_size 20M; | |
gzip on; | |
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml; | |
} | |
EOL | |
[ ! -L "/etc/nginx/sites-enabled/$DOMAIN" ] && ln -s "/etc/nginx/sites-available/$DOMAIN" "/etc/nginx/sites-enabled/" | |
systemctl daemon-reload | |
systemctl restart "$NAME" | |
systemctl reload nginx | |
sudo certbot --nginx -d "$DOMAIN" | |
systemctl restart "$NAME" | |
systemctl reload nginx | |
echo "β Django project deployed and secured at https://$DOMAIN" | |
else | |
echo "β Invalid selection. Please choose 1 (React) or 2 (Django)." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment