Day 22 – Django Deployment with Shell Scripting & DevOps

1️⃣ Introduction to Django Deployment
Django deployment refers to moving your project from a local development environment to a live production server where real users can access it. This process isn’t limited to just uploading code—it involves optimizing performance, securing the application, configuring servers, and automating repetitive tasks. Even a small mistake during deployment can break your app or make it vulnerable. That’s why having a deep understanding of the deployment process is essential.
2️⃣ Common Issues Faced During Deployment
When deploying a Django project, some common errors often occur. For example, if the ALLOWED_HOSTS setting doesn’t include your server’s IP or domain, a “DisallowedHost” error will occur. Static files won’t display if the collectstatic command hasn’t been run. If the Gunicorn service is down, you may face a “502 Bad Gateway” error. Database errors may occur due to credential mismatches or misconfigured ports. These problems can be avoided by including proper setup in your deployment script from the start.
3️⃣ Role of Web Server and WSGI
Web servers like Nginx and WSGI servers like Gunicorn play an essential role in Django deployment. Gunicorn runs your Django code at the Python level, while Nginx receives HTTP requests and forwards them to Gunicorn. Nginx also serves static files, reducing the load on Gunicorn. Proper configuration of both ensures a fast and reliable application experience for users.
4️⃣ Introduction to Shell Scripting in DevOps
Shell scripting is a critical skill in DevOps. It lets you automate repetitive server tasks like pulling code from Git, activating the virtual environment, installing dependencies, handling migrations, managing static files, and restarting services. Using a single shell script, the entire deployment can be done with one command, reducing human errors and speeding up the process. Every developer should learn to write deployment shell scripts.
5️⃣ Basic Server Setup for Django
The first step in deploying Django is setting up an Ubuntu server. On the server, you need to install Python, pip, virtualenv, Git, and Nginx. Then you clone your project from a Git repository, create a virtual environment, and install required Python packages. Once done, you apply migrations and collect static files. These initial manual steps can later be bundled into a shell script for quick reuse.
6️⃣ Environment Variables and Secrets
Handling secrets securely is crucial in production. Environment variables are stored in a .env file and accessed in Django using libraries like python-decouple or django-environ. Sensitive data like SECRET_KEY, database passwords, and settings like DEBUG=False should never be hardcoded. This makes your application safer and easier to manage in different environments (like development, staging, and production).
7️⃣ Setting up Gunicorn and Nginx
Gunicorn is used to serve Django in production and can be started with a command like gunicorn projectname.wsgi:application. To keep it running permanently, it should be managed using systemd services. Nginx is configured to act as a reverse proxy, forwarding HTTP requests to Gunicorn and directly serving static files. Proper Nginx configuration includes directives like proxy_pass, location, and alias, which must be set correctly for smooth operation.
8️⃣ Automating Deployment with Shell Script
A well-written shell script can automate the entire deployment process. It can include steps like pulling updates with Git, installing packages via pip, running migrations, collecting static files, and restarting Gunicorn and Nginx. With such a script, deploying your project becomes as easy as running one command. This is especially helpful in team environments or when frequent updates are made.
9️⃣ Best Practices for Django Deployment
Certain best practices should always be followed in Django deployment. These include setting DEBUG=False, enabling HTTPS using SSL (via Let's Encrypt), blocking external database access, monitoring logs, and using a firewall like UFW. You should always use a virtual environment and keep secrets in an .env file. It’s also a good habit to implement testing and monitoring tools after deployment to ensure everything runs smoothly.
🔟 Conclusion: Why DevOps Matters
This session made it clear that coding is only part of the job—deploying code in a secure, scalable, and maintainable way is just as important. The DevOps mindset helps developers become complete engineers who understand automation, security, monitoring, and performance. Shell scripting simplifies deployment and makes it repeatable. As you grow in your career, mastering deployment will save you time, reduce errors, and improve collaboration across teams.



