Skip to main content

Django (Version 4.1.1) – Ubuntu 22.04

Overview

Category: Frameworks
Supported OS: Ubuntu 22.04

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, Django streamlines web development by handling much of the routine work, so you can focus on building your application. Django is free and open source.


Software Included

  • Django: 4.1.1
  • Nginx: 1.18.0
  • Postgres: 14.6
  • Postfix: 3.4.13
  • Certbot: 1.21.0
  • Gunicorn: 20.1.0

Getting Started After Deployment

1. Firewall Configuration

Allow only SSH (port 22, rate limited), HTTP (port 80), and HTTPS (port 443) access for security and web access.


2. Deployment Message

During deployment, you will be logged out and see the following message:

Please wait until the installation is completed.... Connection to $IPADDRESS closed.

Do not log in for at least 2 minutes after the instance is created to allow the setup to complete.


3. First Login Steps

  • Log in as the ubuntu user, using your set password or SSH key.

  • Switch to the root user without a password:

    sudo su -

  • View your Django project immediately by visiting your instance’s IP address in a web browser.

  • The Postgres user password is stored under:

    /root/.postgresql_user_passwords

  • Login to Postgres using:

    sudo -u postgres psql

  • The Django database information is stored under:

    /root/.postgresql_passwords

  • Django Admin login credentials are stored under:

    /root/.adminpanel_passwords

  • Django shell user login credentials are stored under:

    /root/.shell_user_passwords

  • Django home directory:

    /home/django/django_project/django_project/


4. Managing Your Django Application

  • Use the provided project at /home/django/django_project or start fresh. If needed, edit Gunicorn’s configuration at /etc/systemd/system/gunicorn.service to point to your project location.

  • You can also change how Nginx routes traffic by editing /etc/nginx/sites-enabled/default.

  • To install Python dependencies for your Django project:

    pip install -r requirements.txt

  • After making code changes, reload Gunicorn:

    PID=$(systemctl show --value -p MainPID gunicorn.service) && kill -HUP $PID

  • The Django admin area is accessible at /admin. Login details are stored in /root/.adminpanel_passwords (use cat /root/.adminpanel_passwords).


5. Connecting PostgreSQL Remotely

Refer to the section "Once the PostgreSQL is deployed" for instructions on configuring remote access.


6. Setting up Postfix

  • Set a valid hostname:

    hostnamectl set-hostname $HOSTNAME

    Replace $HOSTNAME with your chosen hostname. Ensure the hostname has a valid A record and the instance IP has a PTR record.

  • Configure the hostname in /etc/postfix/main.cf:

    myhostname = $hostname

  • Restart Postfix:

    systemctl restart postfix

  • Test outgoing email:

    echo "Postfix test" | mail -s "Subject" test@gmail.com

Note: Initially, emails may be delivered to the SPAM folder. As your server’s reputation builds over time, emails should reach recipients’ inboxes.