Deploy Django Project


Step 1: Set Up a Virtual Private Server (VPS)

  • Choose a VPS provider and create a server instance.
  • Configure the server's operating system, ensuring it meets the requirements for running your Django project.


Step 2: Connect to the VPS

  • Connect to your VPS using SSH or any other remote access method provided by your VPS provider.
  • You should have the necessary login credentials to access the VPS.


Step 3: Install Dependencies

  • Update the package manager on your VPS by running the appropriate command. For example, on Ubuntu, you can use:
  • sudo apt update


  • Install the required dependencies, including Python and any other packages or libraries your Django project relies on. The specific commands may vary based on your VPS provider and operating system.


Step 4: Set Up Project Directory

  • Create a directory structure for your Django project on the VPS. For example:
  • sudo mkdir /opt/django-profile
  • Navigate to the project directory:
  • cd /opt/django-profile


Step 5: Set Up a Virtual Environment (venv)

  • Create a virtual environment to isolate the dependencies of your Django project:
  • python3 -m venv venv
  • Activate the virtual environment:
  • source venv/bin/activate


Step 6: Clone the Project

  • Clone your Django project repository or transfer the project files to the VPS.
  • Make sure the project files are placed within the project directory (/opt/django-profile in our example).


Step 7: Install Project Dependencies

  • Navigate to the project directory:
  • cd /opt/django-profile
  • Install the project dependencies using pip:
  • pip install -r requirements.txt


Step 8: Configure Gunicorn

  • Create a Gunicorn configuration file (e.g., gunicorn_config.py) within your project directory (/opt/django-profile).
  • Customize the configuration based on your project's requirements. Specify the number of workers, network interface, and other settings. Refer to Gunicorn's documentation for more details.


gunicorn_config.py:

command = '/opt/django-profile/venv/bin/gunicorn'

pythonpath = '/opt/django-profile/base'

bind = '127.0.0.1:8000'

workers = 3

#user = nobody


Step 9: Install Configure Nginx

  • Install Nginx:
  • sudo apt install nginx


  • Create a new Nginx server block configuration file for your Django project. You can use a text editor to create the file:


sudo nano /etc/nginx/sites-available/myproject


  • Replace "myproject" with a suitable name for your project.


In the editor, add the following configuration to the file:


server {

  listen 80;

  server_name your_domain_or_ip; 1.2.3.4 www.abcd.com


  location /static/ {

    root /opt/myproject/;

  }


  location / {

    proxy_pass http://localhost:8000;

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

}


  • Replace "your_domain_or_ip" with your actual domain name or IP address.
  • Save the file and exit the text editor


Step 10: Enable the Nginx Server Block

  • Create a symbolic link from the site's available directory to the sites-enabled directory:
  • sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/


Step 11: Test Nginx Configuration

  • Check the Nginx configuration for any syntax errors:
  • sudo nginx -t


Step 12: Restart Nginx

  • Restart Nginx to apply the configuration changes:
  • sudo service nginx restart


Step 13: Start Gunicorn

  • Start Gunicorn as we discussed earlier using the appropriate command:
  • gunicorn -c /opt/django-profile/gunicorn_config.py your_project_name.wsgi


Step 14: Create Service

  • create service and use command of step 13 in service content file.



Note: That's it! Nginx should now be configured as a reverse proxy for your Django project. Requests coming to Nginx will be forwarded to Gunicorn, which will then serve your Django application. Make sure to replace "your_project_name" with the actual name of your Django project.


Note: If you're using HTTPS/SSL, additional configuration steps are required to enable SSL certificates and redirect HTTP requests to HTTPS. It's recommended to refer to Nginx documentation or online tutorials for more advanced Nginx configurations.


date:June 12, 2024