Installing a Django application on a Debian 10 server is a hot topic for both experienced developers and beginners finding their way in the world of web development. This complete guide provides step-by-step instructions for installing a Django application on Debian 10 using Apache and WSGI.
Django is one of the most popular web frameworks for building modern, scalable web applications. Debian 10, known for its stability and security, offers a solid foundation for hosting. When combined with Apache, one of the most widely-used web servers, and WSGI to bridge the gap between Python applications and web servers, you get a robust and reliable stack for your web projects.
Whether you are an experienced Django developer seeking a reliable way to host your application or a beginner just starting with Django and in search of a clear and concise guide, this tutorial will walk you through the entire process.
Read on for detailed instructions, including the configuration of virtual hosts, directory permissions, and more. Learn how to install and configure your Django application on a Debian 10 system running Apache and WSGI, and get your project online today!
Step 1: Preparation of the system
Make sure your Debian 10 system is up to date:
sudo apt update
sudo apt upgrade
Step 2: Installation of Apache and required modules
Install Apache and the mod_wsgi module:
sudo apt install apache2
sudo apt install libapache2-mod-wsgi-py3
sudo a2enmod wsgi
Step 3: Installation of Python 3.7
Check your current Python version:
python3 --version
If necessary, install Python 3.7:
sudo apt install python3.7
Step 4: Creating and setting up a virtual environment
Install and create packages for a virtual environment:
sudo apt install python3-venv
python3 -m venv /var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/myenv
Step 5: Installation of Django
Activate the virtual environment and install Django:
source /var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/myenv/bin/activate
pip3 install django
Step 6: Virtual Host Configuration (vhost).
1. Directory Permissions.
Configure access rights:
<Directory /var/www/djangoapplicatie.nl>
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
2. Virtual Host Block
Define the beginning of the configuration block:
<VirtualHost *:80>
...
</VirtualHost>
3. DocumentRoot, ServerName, and ServerAlias.
Set paths, domain name, and aliases:
DocumentRoot /var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/src
ServerName djangoapplicatie.nl
ServerAlias www.djangoapplicatie.nl
4. ErrorLog and CustomLog
Define log locations:
ErrorLog ${APACHE_LOG_DIR}/djangoapplicatie.nl.error.log
CustomLog ${APACHE_LOG_DIR}/djangoapplicatie.nl.access.log combined
5. Static and Media Aliases
Make static and media files accessible.
6. WSGI Configuration
Connect the Django application to Apache via WSGI:
WSGIDaemonProcess pythoneditortool python-path=/var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/src python-home=/var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/myenv
WSGIProcessGroup pythoneditortool
WSGIScriptAlias / /var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/src/wsgi.py
7. PHP-FPM and Suexec Configuration.
Configure PHP scripts and user rights.
8. DAV Configuration
Set up WebDAV.
Save file and restart Apache
Save, enable, and restart Apache:
sudo a2ensite djangoapplicatie.nl.conf
sudo systemctl restart apache2
Example entire vhost file
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/src
ServerName djangoapplicatie.nl
ServerAlias www.djangoapplicatie.nl
<Directory /var/www/djangoapplicatie.nl>
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
Alias /static/ /var/www/djangoapplicatie.nl/static/
Alias /media/ /var/www/djangoapplicatie.nl/media/
<Directory /var/www/djangoapplicatie.nl/static>
Require all granted
</Directory>
<Directory /var/www/djangoapplicatie.nl/media>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/djangoapplicatie.nl.error.log
CustomLog ${APACHE_LOG_DIR}/djangoapplicatie.nl.access.log combined
WSGIDaemonProcess pythoneditortool python-path=/var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/src python-home=/var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/myenv
WSGIProcessGroup pythoneditortool
WSGIScriptAlias / /var/www/djangoapplicatie.nl/web/srv/webapps/pythoneditortool/src/wsgi.py
# Suexec en PHP-FPM (if needed)
# ...
# DAV Configuration (if needed)
# ...
</VirtualHost>
Step 7: Check your installation
Open your domain to check that everything is working.
Conclusion
Setting up a Django application with Apache on Debian 10 requires a few steps, including installing Apache, Python, and the necessary modules, setting up a virtual environment, and configuring a virtual host in Apache. Your configuration file serves as an excellent example of the proper settings. If everything is configured correctly, your Django application should now be reachable through your domain.
Learn more: Setting Up Django Application From Scratch
If you are just starting out with Django and want to build an application from scratch, you can benefit from the extensive and detailed documentation provided by the Django project itself.
The officialDjango documentation provides a tutorial series that guides you in creating a Django application from scratch. From installing Django to defining models, creating views and customizing templates, these tutorials provide detailed explanations and code examples to help you successfully build your first Django application.
Suitable for developers of all levels, the tutorial is an excellent resource to familiarize you with key concepts and practices within Django. Whether you are a complete beginner or want to refresh your knowledge, the manual provides practical instructions and best practices straight from the experts behind Django itself.