DebianDebian Based

How To Install Django on Debian 12

Install Django on Debian 12

In this tutorial, we will show you how to install Django on Debian 12. For those of you who didn’t know, Django, a powerful Python web framework, empowers developers to build dynamic and sophisticated web applications with ease.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of Django on a Debian 12 (Bookworm).

Prerequisites

  • A server running one of the following operating systems: Debian 12 (Bookworm).
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Django.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Django on Debian 12 Bookworm

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

sudo apt update
sudo apt install apt-transport-https lsb-release ca-certificates curl dirmngr gnupg

This command will refresh the repository, allowing you to install the latest versions of software packages.

Step 2. Installing Python and Pip.

Django is a Python-based web framework, so you need to install Python and Pip on your system. You can install them by running the following command:

sudo apt install python3-pip python3-venv

Virtual environments are essential for isolating Django’s dependencies and avoiding conflicts with other Python projects. Let’s create and activate a new virtual environment:

python3 -m venv mydjangoenv
source mydjangoenv/bin/activate

Step 3. Installing Django on Debian 12.

With the virtual environment active, it’s time to install Django using the pip package manager:

pip install django

To verify that Django is successfully installed, use the following command:

django-admin --version

Step 4. Configuring Django.

Let’s create a new Django project and explore its structure:

django-admin startproject myproject
cd myproject

Now, configure the database settings in settings.py:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

To handle static files, add the following to settings.py:

STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

Step 5. Example Command Line Interface (CLI).

With Django installed and configured, we can create a new app within the project:

python manage.py startapp myapp

Next, let’s apply migrations to set up the database tables:

python manage.py migrate

Now, run the development server and preview your app in the browser:

python manage.py runserver

Step 6. Deploying Django on Apache with mod_wsgi.

To deploy Django on Apache, first, install the required packages:

sudo apt install apache2 libapache2-mod-wsgi-py3

Create a new virtual host configuration file for your Django project:

sudo nano /etc/apache2/sites-available/myproject.conf

Add the following configuration:

<VirtualHost *:80>
ServerName your_domain.com

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

WSGIDaemonProcess myproject python-home=/path/to/mydjangoenv python-path=/path/to/myproject
WSGIProcessGroup myproject
WSGIScriptAlias / /path/to/myproject/wsgi.py

<Directory /path/to/myproject>
Require all granted
</Directory>
</VirtualHost>

Save and close, then enable the virtual host and restart Apache:

sudo a2ensite myproject.conf
sudo systemctl restart apache2

Step 7. Tips for Production Deployment.

  • For a secure production environment, ensure your Django application uses HTTPS. Obtain an SSL certificate and configure Apache accordingly.
  • To optimize Django for production settings, use Gunicorn or uWSGI instead of the development server.
  • Implement security measures, such as user authentication, input validation, and protection against common web vulnerabilities.

Step 8. Troubleshooting Common Issues.

  • If you encounter “ModuleNotFoundError: No module named ‘django’,” verify that your virtual environment is activated.
  • For “Internal Server Error (500)” issues, check Apache’s error logs for potential causes.
  • In case of database-related problems, ensure the database is correctly configured and migrated.

Congratulations! You have successfully installed Django. Thanks for using this tutorial for installing the latest version of Django on Debian 12 Bookworm. For additional help or useful information, we recommend you check the official Django website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button