Files
my-docs/Web Development & Frameworks/nginx/django.md
2024-11-07 16:06:11 +03:30

4.6 KiB
Executable File

Installing Packages from Ubuntu Repositories

  1. Update your Ubuntu system:

    sudo apt update
    
  2. Install necessary packages:

    sudo apt install python3-venv python3-dev libpq-dev postgresql postgresql-contrib nginx curl
    

Creating the PostgreSQL Database and User

  1. Log into an interactive Postgres session:

    sudo -u postgres psql
    
  2. Inside the PostgreSQL prompt, create a database for your project:

    CREATE DATABASE myproject;
    
  3. Create a database user for your project with a secure password:

    CREATE USER myprojectuser WITH PASSWORD 'password';
    
  4. Modify connection parameters for the user:

    ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
    ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
    ALTER ROLE myprojectuser SET timezone TO 'UTC';
    
  5. Grant the new user access to administer the new database:

    GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
    
  6. Exit the PostgreSQL prompt:

    \q
    

Creating a Python Virtual Environment for Your Project

  1. Create a directory for your project files:

    mkdir ~/myprojectdir
    cd ~/myprojectdir
    
  2. Create a Python virtual environment:

    python3 -m venv myprojectenv
    
  3. Activate the virtual environment:

    source myprojectenv/bin/activate
    
  4. Install Django, Gunicorn, and psycopg2:

    pip install django gunicorn psycopg2-binary
    

Creating and Configuring a New Django Project

  1. Create a new Django project with a defined directory:

    django-admin startproject myproject ~/myprojectdir
    
  2. Adjust settings in the settings.py file:

    • Set ALLOWED_HOSTS to ['*']
    • Configure DATABASES with PostgreSQL details
  3. Add static root configuration to settings.py:

    import os
    STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
    
  4. Migrate initial database schema:

    ~/myprojectdir/manage.py makemigrations
    ~/myprojectdir/manage.py migrate
    
  5. Create an administrative user:

    ~/myprojectdir/manage.py createsuperuser
    
  6. Collect static content:

    ~/myprojectdir/manage.py collectstatic
    
  7. Allow port 8000:

    sudo ufw allow 8000
    
  8. Start Django development server:

    ~/myprojectdir/manage.py runserver 0.0.0.0:8000
    
  9. Access your application in a web browser.

  10. Stop Apache2 service:

    sudo /etc/init.d/apache2 restart
    

Testing Gunicorn

  1. Test Gunicorn to ensure it can serve the application:

    cd ~/myprojectdir
    gunicorn --bind 0.0.0.0:8000 myproject.wsgi
    
  2. Stop Gunicorn:

    • Press CTRL-C

Creating systemd Socket and Service Files for Gunicorn

  1. Create a systemd socket file for Gunicorn:

    sudo nano /etc/systemd/system/gunicorn.socket
    
  2. Create and open a systemd service file for Gunicorn:

    sudo nano /etc/systemd/system/gunicorn.service
    
  3. Configure the service file with appropriate details.

Checking for the Gunicorn Socket File

  1. Check the status of the Gunicorn socket:

    sudo systemctl status gunicorn.socket
    
  2. Check for the existence of the Gunicorn socket file:

    file /run/gunicorn.sock
    
  3. Check Gunicorn socket logs:

    sudo journalctl -u gunicorn.socket
    

Testing Socket Activation

  1. Test the socket activation mechanism:

    sudo systemctl status gunicorn
    
  2. Send a connection to the socket through curl:

    curl --unix-socket /run/gunicorn.sock localhost
    

Configuring Nginx to Proxy Pass to Gunicorn

  1. Create and open a new server block in Nginx's sites-available directory:

    sudo nano /etc/nginx/sites-available/myproject
    
  2. Configure the server block with appropriate settings.

  3. Enable the server block:

    sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
    
  4. Test Nginx configuration for syntax errors:

    sudo nginx -t
    
  5. Restart Nginx:

    sudo systemctl restart nginx
    
  6. Open firewall to normal traffic on port 80:

    sudo ufw delete allow 8000
    sudo ufw allow 'Nginx Full'
    

Conclusion

You should now be able to access your Django application via your server's domain or IP address.