How To Configure Django With Apache In CentOS 7

Django is a high-level and powerful Python Web framework. In fact, this tool will help to make rapid development and concrete design. It is free and open source application that can help you to have your python application and website rapidly. In this article, we will give you the procedure how to configure Django with Apache in CentOS 7.

We will start our article by giving the historic of Django and listing its features.

What Is Django?

Django was created at the end of 2003 by Adrian Holovaty and Simon Willison, two developers at the Lawrence Journal-World newspaper. It was released under the BSD license.

Regarding its features, Django:

  • Helps developers to finish their applications rapidly.
  • Is a secure tool and developers work in a safe environment.
  • Takes care of user authentication, RSS feeds, content administration and many others.
  • Could be used to develop social networks, scientific computing platforms and content management.

Install Django in CentOS 7

First steps:

Before starting the configuration, it is required to configure your CentOS 7 server instance with “sudo” privileges. The installation of Django it will be with a Python virtual environment. Later the “mod_wsgi” Apache module will be needed to configure the Apache with Django application.

Installing CentOS’s packages:

Different items need to be installed at the beginning. So, we will need to have the Apache web server, “mod_wsgi”, “pip” and Python package manager which could be used to download Python related tools.

So let’s start, to have “pip” it is required to enable the EPEL repository by typing:

sudo yum install epel-release

After enabling EPEL repository, the needed tools will be installed by using:

sudo yum install python-pip httpd mod_wsgi

Configure the Python virtual environment:

Before starting our Django project, it is also required to create the Python virtual environment. So, we need to install “virtualenv” to create our environment by using:

sudo pip install virtualenv

Now after installing the “virtualenv” we will create our project directory using:

mkdir ~/myprojectcd ~/myproject

After creating our directory, we are able to create our Python virtual environment using:

virtualenv myprojectenv

So to summarize, within our project directory “myproject” we will have our environment called “myprojectenv”. And automatically, the “pip” and the local version of Python are installed in our directory which could be used for the installation and configuration of a Python environment. Later, we will enable the virtual environment using:

source myprojectenv/bin/activate

Now after following us, you will work with your Python virtual environment which is checked if you have something like:

(myprojectenv)user@host:~/myproject$

As we finished the all required steps, we can install the Django using:

pip install django

So it is time now to start creating our Django project.

Create the Django project:

Now after installing Django in our directory, we will use it to install files in the same folder. Then a second directory will be created where we will make the management script. We will use the following command:

django-admin.py startproject myproject .

Don’t forget the “.” It is very important to create the files in this directory. Now, it is important to adjust the settings of our project. So firstly you have to open the settings file using:

nano myproject/settings.py

SQlite database will be used now. After opening the settings file, at the botton of it, a specific command will be added to configure our made directory.

So at the bottom of the settings file, add:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

Then you can save and close this file. Now we will transfer the database of our project to the SQLite database using:

cd ~/myproject./manage.py makemigrations./manage.py migrate

And use the following command to create an administrator user while you will be asked to select a username, give an e-mail address and enter your password.

./manage.py createsuperuser

At the end, you will be able to test your Django project by using:

./manage.py runserver 0.0.0.0:8000

Then you can visit your server’s domain name using:

http://server_domain_or_IP:8000

If you use the “/admin” at the end of the URL, you will be asked to enter your administrative username and password you have already created. Later, you can check your Django admin interface.

To leave the virtual environment you just have to type:

deactivate

Configure Django With Apache

After checking your rDjango project, it is time to configure Apache. All the clients requests are translated to WSGI format which is by default used with Django with “mod_wsgi”. You have to create a new configuration file in order to define WSGI pass. You just have to use:

sudo nano /etc/httpd/conf.d/django.conf

Where the file were called “django.conf ”. Then the following command will be used in order to set up the needed alias to map the requests started by “/static” to the static folder.

Alias /static /home/user/myproject/static

Next, we will have access to the “wsgi” file within the second level project directory where the Django code is stored.

Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>   
 Require all granted
</Directory> 
<Directory /home/user/myproject/myproject>    
<Files wsgi.py>        
Require all granted    
</Files></Directory>

Now it is recommended to use the daemon mode to run the WSGI process. The “WSGIDaemonProcess” will be used for that. You have to specify the name for the process because an arbitrary one will be used. Two paths will be used. One for our project and other, “lib/pythonx.x/site-packages”. In order to find all the needed Python code bu Apache to run our project.

Then, the script alias which will help to transfer the requests from the root folder to the “wsgi.py” file will be configured using:

Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>    
Require all granted</Directory> 
<Directory /home/user/myproject/myproject>  
  <Files wsgi.py>        
Require all granted 
   </Files>
</Directory> 
WSGIDaemonProcessmyproject python-
path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject 
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py

Then save and close the file. Now we will describe how we can fix some permissions sets in order to give Apache possibility to get access to our files. Generally, each user’s home directory will be locked down with the CentOS. So we will need the “apache” user in our user’s group in order have access to such files. You can make this by using:

sudo usermod -a -Guser apache

Now the different member of the user group will have permissions to use the concerned home directory. If the SQLike database is used so several steps need to be followed to allow the Apache process to have access to the database file. Firstly, the group owner of the database need to have permissions to read and write. The file is called “db.sqlite3” generally and must be transferred to your base project directory:

chmod 664 ~/myproject/db.sqlite3

Secondly, the Apache group need to have a group ownership of the file:

sudo chown :apache ~/myproject/db.sqlite3

Finally, the Apache group have to get the ownership of the database’s parent directory using:

sudo chown :apache ~/myproject

Now Apache can be used by this command:

sudo systemctl start httpd

After this configuration the Django site may be checked within the server’s domain name or IP address without need to specify the port. And then you can enable the Apache server to run using:

sudo systemctl enable httpd

Conclusion

The Django project was installed in a virtual environment and the Apache was configured with “mod_wsgi” in this article.  We hope that it is a helpful article for you.