Django is a popular high-level web framework built using Python that encourages rapid development and clean, pragmatic design. In this tutorial, we will walk you through the process of how to install Django on Rocky Linux, a community-based enterprise Linux distribution.
Prerequisites
To follow this tutorial, you will need:
- A Rocky Linux system.
- A user account with sudo privileges.
How to Install Django on Rocky Linux
Update Your System
Before installing Django, ensure that your Rocky Linux system is up-to-date:
sudo dnf update -y
Install Python and Pip on Rocky Linux
Django is built on Python, so we’ll first need to install Python and its package manager, Pip. Rocky Linux comes with Python pre-installed, but it’s a good idea to verify its version:
python3 --version
Next, install Pip on Rocky Linux:
sudo dnf install python3-pip -y
Install Django on Rocky Linux
Now that Python and Pip are installed, we can proceed to install Django using Pip:
pip3 install django
After the installation, verify Django is installed successfully:
django-admin --version
Create a Django Project on Rocky Linux
Once Django is installed, create a new Django project using the following command:
django-admin startproject myproject
Replace myproject
with the desired name for your project.
Configure the Django Project on Rocky Linux
Navigate to your Django project directory:
cd myproject
Edit the settings.py
file to configure the Django project. For this tutorial, we will focus on configuring the ALLOWED_HOSTS
and DATABASES
settings.
Open settings.py
using a text editor, such as nano:
nano myproject/settings.py
Locate the ALLOWED_HOSTS
setting and add your server’s IP address or domain name to the list:
ALLOWED_HOSTS = ['your_server_ip_or_domain']
Configure the DATABASES
setting according to your desired database system. By default, Django uses SQLite, which may be suitable for development purposes. For production environments, consider using PostgreSQL, MySQL, or MariaDB. You can follow our guides on setting up PostgreSQL on Rocky Linux, MySQL on Rocky Linux, or MariaDB on Rocky Linux.
After configuring your settings, save the file and exit the text editor.
Create and Configure a Virtual Environment
To isolate your Django project’s dependencies, create a virtual environment using the following command:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
When the virtual environment is active, your prompt should change, indicating the active environment.
Install Django in the virtual environment:
pip install django
Set up a Web Server on Rocky Linux
For this tutorial, we will use the lightweight Gunicorn web server. Install Gunicorn using Pip:
pip install gunicorn
Test Gunicorn by running the following command:
gunicorn myproject.wsgi
Your Django project should now be running on http://localhost:8000
. Press CTRL+C
to stop Gunicorn.
Configure Systemd for Gunicorn on Rocky Linux
To ensure that your Django application runs continuously, even after a server reboot or crash, it’s a good idea to configure a Systemd service for Gunicorn. Systemd will manage the Gunicorn process and restart it when necessary.
- Create a new service file for Gunicorn:
sudo nano /etc/systemd/system/gunicorn.service
Add the following content to the service file and replace {your_user}
with your system username, and {your_project_directory}
with your Django project directory:
[Unit]
Description=Gunicorn instance to serve Django project
After=network.target
[Service]
User={your_user}
Group=nginx
WorkingDirectory={your_project_directory}
ExecStart={your_project_directory}/venv/bin/gunicorn --workers 3 --bind unix:{your_project_directory}/django.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
Save the file and exit the editor.
Enable and start the Gunicorn service on Rocky Linux
sudo systemctl enable gunicorn sudo systemctl start gunicorn
Configure Nginx as a Reverse Proxy on Rocky Linux
Finally, you need to set up Nginx to proxy requests to the Gunicorn server. This will allow you to serve your Django application using Nginx, which is more suitable for production environments due to its better performance and additional features.
- Install Nginx:
sudo dnf install -y nginx
Create a new Nginx configuration file for your Django project:
sudo nano /etc/nginx/conf.d/django_project.conf
Add the following content to the configuration file, replacing {your_project_directory}
with your Django project directory and {your_domain}
with your domain name:
arduino
server {
listen 80;
server_name {your_domain};
location / {
include proxy_params;
proxy_pass http://unix:{your_project_directory}/django.sock;
}
}
Save the file and exit the editor.
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the configuration test is successful, restart Nginx:
sudo systemctl restart nginx
If you have SELinux enabled, you may need to allow Nginx to access the Gunicorn socket:
sudo setsebool -P httpd_can_network_connect 1
Your Django project should now be accessible at http://{your_domain}
. Congratulations, you have successfully set up Django on Rocky Linux!
Conclusion
In this tutorial, you have learned how to install Django and Gunicorn on Rocky Linux, configure PostgreSQL as the database for your Django project, and set up Nginx as a reverse proxy to serve your application. For more tutorials on how to set up and manage your Linux environment, check out our LinuxBoost blog. Don’t forget to explore other useful articles, such as setting up Apache Tomcat, configuring Fail2Ban.