How to install Django, MariaDB on Debian 11

In this tutorial, I will show you how to install Django, MariaDB on Debian 11 (codenamed Bullseye) from official repository. This tutorial also applies to all Debian-based distribution, for example, Ubuntu. We will use Debian 11 because it is one of the most popular GNU/Linux distributions and is a well supported operating system.

Installing Python and MariaDB on Linux

So, let’s begin. First you need to update your package lists. Then install Python 3, Pip and MariaDB.

sudo apt update
sudo apt install python3-{pip,venv} mariadb-{server,client} libmariadb-dev

In command above we used Bash expansion feature. You could write that as follows:

sudo apt install python3-pip python3-venv mariadb-server mariadb-client libmariadb-dev

Configuring MariaDB/MySQL

After installing all required software you should configure it. Let’s begin with MySQL (MariaDB). Run initial configuration process:

sudo mysql_secure_installation

During the process, choose to change root account, remove test DB and anonymous account and allow connections from localhost only. After initial configuration, create a database for your website and a non-root account for that DB and grant it all privileges for website database. Next, “flush” privileges to make changes take effect.

Login to MySQL as root: mysql -u root -p. In the MariaDB (MySQL) shell, run the following queries:

CREATE DATABASE website CHARACTER SET utf8mb4;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'your-secure-password';
GRANT ALL PRIVILEGES ON website.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

Remember to replace “your-secure-password” with your password.

Set up Python virtual environment

Make a directory for your website. There you can create and activate Python Virtual Environment (venv) for your website:

mkdir ~/website
cd ~/website
python3 -m venv venv
source venv/bin/activate

If it throws an error, install the venv module: sudo apt install python3-venv. Next, run commands above.

If you see (venv) in the beginning of your shell prompt, this means you have activated the virtual environment. You could use dot . instead of source command — a default Bash alias.

Get the C and C++ compiler toolchain and Python C headers by installing build-essential metapackage and python3-dev:

sudo apt install build-essential python3-dev

Now you can install Python Django and MySQLclient via Pip:

pip install wheel django mysqlclient

The wheel package is required to install mysqlclient properly. At this point, the virtual environment is configured!

Create and configure a Django project

Now you can create a Django project:

django-admin startproject website .

Next, open the file website/settings.py in the editor. Change ALLOWED_HOSTS to contain the hostname of your website or localhost (127.0.0.1):

ALLOWED_HOSTS = ['localhost']

Then go to the DATABASES dict in the settings.py file. Replace SQLite with MySQL by changing DATABASES variable values:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'website',
        'USER': 'user',
        'PASSWORD': 'your-secure-password',
        'HOST': '/run/mysqld/mysqld.sock',
    }
}

Note that we used MySQL Unix domain socket instead of localhost:3306. This can give a little performance increase. Alternatively, you could use 'HOST': 'localhost', 'PORT': 3306.

Optionally, you can create config.py with DB_PASSWORD variable in it and import config.py from settings.py and use config.DB_PASSWORD instead. This will increase security, as there is no need to keep settings.py secret anymore.

Then “migrate” your Django models (from INSTALLED_APPS) to MySQL website database by running this command:

python3 manage.py migrate

Now you can run your server: python3 manage.py runserver 0.0.0.0:8000. If you go to http://localhost:8000/ on your web browser, you should see this:

Default Django page

The install worked successfully! Congratulations!

You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs.

If you did everything correctly, the default Django page will appear. The next step is to create a superuser for admin panel:

python3 manage.py createsuperuser

Then restart the server and go to http://localhost:8000/admin and login. You should see the admin panel.

From now on you can start developing your Django application! Remember to set DEBUG to False when you deploy your project in production environment.

Summary

To make Django work with MariaDB MySQL database, you do the following things:

  • Install Python, MariaDB.
  • Configure MariaDB/MySQL — secure the installation, create the database and user for a website.
  • Set up virtual environment for Python, install Django.
  • Create a Django project, configure the settings.py.
  • Done! Check your installation with runserver command.

Leave a Comment