Step-by-Step Guide to Deploying a Django Project on a Server

Step-by-Step Guide to Deploying a Django Project on a Server


1. Prepare Your Django Project

  1. Collect Static Files: Run the following command to gather all static files into the STATIC_ROOT directory:


python manage.py collectstatic
  1. Set DEBUG to False: In your settings.py, ensure DEBUG is set to False for production:
DEBUG = False
  1. Set Allowed Hosts: Update the ALLOWED_HOSTS setting with your server's domain or IP:
ALLOWED_HOSTS = ['yourdomain.com', 'server_ip']
  1. Secure the Secret Key: Use environment variables to store sensitive data like SECRET_KEY.


2. Set Up the Server

Install Necessary Software

  1. Update the Server:
sudo apt update && sudo apt upgrade -y
  1. Install Required Packages: Install Python, pip, and virtualenv:
sudo apt install python3 python3-pip python3-venv -y
  1. Install a Web Server and WSGI Server: Install nginx and gunicorn (or uWSGI):
sudo apt install nginx
pip install gunicorn


3. Configure the Django Application

  1. Set Up a Virtual Environment:
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt
  1. Test Gunicorn: Run the following command to ensure Gunicorn works:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi


4. Configure Nginx

  1. Create an Nginx Configuration File:
sudo nano /etc/nginx/sites-available/myproject
  1. Add the following configuration:
server {
listen 80;
server_name yourdomain.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /path/to/your/project;
}

location / {
include proxy_params;
proxy_pass http://unix:/path/to/your/project/myproject.sock;
}
}
  1. Enable the Configuration:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx


5. Configure Gunicorn

  1. Create a Gunicorn Service:
sudo nano /etc/systemd/system/gunicorn.service
  1. Add the following:
[Unit]
Description=gunicorn daemon for Django project
After=network.target

[Service]
User=username
Group=groupname
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/virtualenv/bin/gunicorn --workers 3 --bind unix:/path/to/your/project/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target
  1. Start and Enable Gunicorn:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn


6. Secure the Server

  1. Set Up HTTPS: Install and configure Certbot for a free SSL certificate:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
  1. Renew Certificates Automatically: Add the following to crontab:
0 3 * * * certbot renew --quiet


7. Test Your Deployment

  1. Visit your domain or IP in a browser to ensure the site is working.
  2. Check logs for any errors:
  3. Nginx logs: /var/log/nginx/error.log
  4. Gunicorn logs: Depends on your service file.

Let me know if you need assistance with any of these steps!

  • Tags:
  • No tags

Comments (0)

Leave a Reply

Log in to post a comment.