How to Serve Django Applications with Apache on CentOS 7

How to Serve Django Applications with Apache on CentOS 7

<meta name=”title” content=”Django Applications with Apache on CentOS 7″>

<meta name=”keywords” content=” Django, CentOS 7, python web server, web server, mysql-server, mysql”> 

Introduction                                                                            

Django is a web framework which let you get your website quickly. Django has a server for testing your code.

We are going to demonstrate that how to use Django with a Python virtual environment on CentOS 7. After that we’ll set up Apache, before that our application with the objective that it can manage client requests particularly on before passing sales which requires application method of reasoning to the Django application.

<meta name=”description” content=” Django is a high-level and effective python web framework. In truth, this device will help to make speedy improvement and urban layout. It’s far unfastened and open supply software that allows you to have your python utility and internet site hastily. In this article, we can give you the manner a way to configure django with apache in centos 7.”>

Pre-Requirements

We need a CentOS 7 server with a non-root customer of “sudo” benefits planned.

We will discuss Django in a Python virtual environment. Bringing Django into a circumstance specific to your endeavor will allow your assignments and their essentials to be dealt with autonomously.

When we have our application up and running, we will outline Apache to interface with the Django application. It will do this with the “mod_wsgi” Apache module, which can make a translation of HTTP requesting into a foreseen application position portrayed by a determination called WSGI. You can find more about WSGI by scrutinizing the associated territory on this helper.

Installation Packages

We need the Apache web server, the “mod_wsgi” module used to interface with our Django application and pip, the Python group boss that can be used to download our Python-related instruments. To get pip, we’ll need to engage the EPEL store, which as some additional groups. You can do it easily as below:

With EPEL enabled, we need to type:

sudo yum install python-pip httpdmod_wsgi

Configure a Python VE

We can get virtualenv using this pip:

sudo pip install virtualenv

Now create a directory where you can keep your project and move into the directory:

mkdir ~/myproject

cd ~/myproject

Create a Python VE using the below command:

virtualenvmyprojectenv

We need to activate the virtual environment using the below command:

sourcemyprojectenv/bin/activate 

Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look a little like this: (myprojectenv)[email protected]:~/myproject$.

Now install Django using the below command:

pip install django 

Create a New Django Project

You can create a new Django Project using the below command:

django-admin.pystartprojectmyproject 

Adjust the Settings

Open the settings file by your text editor:

nano myproject/settings.py

We will use the default SQLite database as a piece of this helper for ease’s motivation, so we don’t generally need to change too much. We will focus on delineation the static records registry, where Django will put static archives, so that the web server can serve these easily.

At the base of the archive, we will add a line to organize this list. Django uses the STATIC_ROOT setting to choose the registry where these reports should go. We’ll use a touch of Python to educate it to use a file called “static” in our endeavor’s principal registry:

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

Now save the file when you are finished. 

Complete Initial Project Setup

Now, we can switch the initial database schema to our SQLite database using the management script:

cd ~/myproject

./manage.py makemigrations

./manage.py migrate 

Now create a user for the project by typing:

./manage.py createsuperuser 

You need to choose a username, email address and a password.

We will gather the static content into the directory location using the below command:

./manage.py collectstatic

Now, you can test the project by start upDjango  server with:

./manage.py runserver 0.0.0.0:8000

Now visit

http://server_domain_or_IP:8000

Configure Apache

To configure the WSGI pass, we have to create the new configuration file which defines the WSGI pass. Create and open a file with sudo privileges within the /etc/httpd/conf.d directory. We should call this file django.conf:

sudonano /etc/httpd/conf.d/django.conf

We will set up the alias and then the grant access to the directory in question with a directory block:

Alias /static /home/user/myproject/static

<Directory /home/user/myproject/static>

    Require all granted

</Directory> 

Now, we’ll give access to the wsgi.py record inside the second level endeavor registry where the Django code is secured. To do this, we’ll use an inventory section with a record region inside. We will permit access to the record inside this settled create:

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>

We can use the WSGIDaemonProcess request to set this up.

A brief timeframe later, we set up the Python way where Apache can find most of the sections that may be required.

A brief span later, we need to decide the technique group. This should demonstrate the same name we decided for the WSGIDaemonProcess request (myproject for our circumstance). Finally, we need to set the script false name, because Apache will pass requests for the root territory to the wsgi.py record:

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

WSGIProcessGroupmyproject

WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py

When you have completed these changes, save and close the file.

The Django was introduced in a virtual situation and the Apache was designed with “mod_wsgi” in this article. We trust that it will be very useful article for you.

 

LEAVE A COMMENT:

This site uses Akismet to reduce spam. Learn how your comment data is processed.

POPULAR POST

iOS 9.3.2 Jailbreak Has Claimed iOS 9 has proved to be а little on the buggy side, with some quite serious issues affecting some users. […][..]
Most Popular Page