How to Install Python 3.6 and Django with Apache (mod_wsgi)

Submitted by aknnn on Wed, 10/10/2018 - 00:43

Today we're going to Install Python 3.6, Django and MariaDB on CentOS 7 and make Apache to serve our Django site using mod_wsgi.
We are setting Apache to use virtual environment so that you can serve multiple sites with your single Apache instance.


First we need to install Python 3.6.
As you might know. CentOS 7 doesn't officially support Python 3 versions, so we need a little workaround.

In this tutorial we are using the popular IUS repository to install Python and some relevant packages.

First you need to install their repository:
# sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm

Next install python and python development module from:
# sudo yum -y install python36u python36u-devel

Install pip, you will need it to install django and other python packages (this also comes from IUS repo).
# sudo yum -y install python36u-pip

Additionally; edit your ~/.bashrc file and make a couple of aliases by adding these lines:
# alias python3="python3.6"
# alias pip3="pip3.6"

From now on you can (I will) just use "python3 <arguments>" instead of "python3.6 <arguments>" to run python3.6 in your terminal.

Now we will install mod_wsgi. It is the module that helps apache to serve our python applications.
# sudo yum install python36u-mod_wsgi


For a database, I recommend using MariaDB over MySQL. MariaDB is a drop-in replacement to MySQL, so there's no difference in usage.
It's guaranteed to stay free and open source, in the case of Oracle making MySQL all corporate.
# sudo yum install mariadb-server

Log in to MariaDB and enter a new your password for your user. (Using MySQL commands of course):
# mysql -u root -p

Create a database:
# CREATE DATABASE database_name;

Create a user for your database (replace username with a new username)
# CREATE USER 'username'@'localhost';

Add privileges to your new user (replace secure_password with a secure password):
# GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost' IDENTIFIED BY 'secure_password';

Remember the username, password and database as we are going to use them later on in this tutorial.


Alright! Now we have Python and MariaDB set up, let's start a Django project!

First I would recommend you to use a separate virtual environment for each of your projects. That way you can install packages to your projects without them affecting global environment.

It's easy, only thing you need to do is create a environment directory somewhere (it doesn't matter where you put it or how you name it, but it's convenient to store all environments in one place, usually a folder in your home directory)

# python3 -m venv YOUR_NEW_ENVIRONMENT_NAME

This will create a directory with your project name to the directory you are in.
You can activate the environment from everywhere by entering this command:

# source /path/to/YOUR_NEW_ENVIRONMENT/bin/activate

Your terminal should now show your environment name before your username in commandline (something like "(env) [user@host]$")
Every package installed with pip(3) will now stay in this environment and will affect nothing globally.

Deactivating this environment is as simple as:
# deactivate

But let's not deactivate it yet. Let's install django to this environment and start a project.

# pip3 install django

cd into directory you wan't to make the new project in.
Start a django project by executing:

# django-admin startproject my_new_site


Edit the my_new_site/my_new_site/settings.py file to contain information for the database we made earlier.
Look for DATABASES dictionary, and edit it as shown below:

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.mysql',
#         'NAME': 'database_name',
#         'USER': 'username',
#         'PASSWORD': 'secure_password',
#         'HOST': 'localhost',
#         'PORT': '',
#     }
# }

Edit the ALLOWED_HOSTS list too to contain your domain:
# ALLOWED_HOSTS = ['www.example.com', 'example.com']

Add these lines to mysite/mysite/__init__py file:
# import pymysql
# pymysql.install_as_MySQLdb()

We can deactivate our environment for now.
# deactivate


Apache needs to be configured to serve our site:

Edit /etc/httpd/conf.d/vhost.conf (If you don't have it, make it).
Add these lines (modify as needed):

# <VirtualHost *:80>
#
#     # Modify these
#     ServerAdmin your.email@example.com
#     ServerName www.example.com"
#     ServerAlias example.com
#     DocumentRoot /path/to/your_new_site
#
#     WSGIProcessGroup www.example.com
#
#     # You need to modify this to correspond the wsgi.py location of your project
#     WSGIScriptAlias / /path/to/your_new_site/your_new_site/wsgi.py
#
#     # You need to modify python-home path to correspond your virtual environment folder
#     # which was made when you created the environmet
#     WSGIDaemonProcess www.example.com processes=2 threads=15 display-name=%{GROUP} python-home=/path/to/YOUR_NEW_ENVIRONMENT
#
#       # Modify this path too
#     <Directory /path/to/your_new_site>
#         Options FollowSymlinks
#         require all granted
#         AllowOverRide All
#     </Directory>
#
# </VirtualHost>

You should be covered. Start and stop httpd service (only restart might not work in this case) and visit your server address.
# systemctl stop httpd
# systemctl start httpd

You should see a Django test page.

If not, check the /var/log/httpd/error_log

You may see a line:
# Fatal Python error: Py_Initialize: Unable to get the locale encoding
It means that wsgi.py can't find the correct Path to your python environment.

You can correct this by inserting two lines in your projects wsgi.py file:
# import sys
# sys.path.append("/path/to/your_new_site")

Comments

Submitted by GerardCrose (not verified) on Thu, 01/24/2019 - 20:10

Permalink

cialis without a doctor's prescription
buy cialis without prescription cialis without a doctor's prescription cialis without prescription mayavanrosendaal.com

Submitted by GerardCrose (not verified) on Fri, 01/25/2019 - 17:30

Permalink

buy cialis without a doctor's prescription
cialis without a doctor's prescription buy cialis without a doctor's prescription buy cialis without a doctor's prescription buy cialis without a doctor's prescription

Submitted by GerardCrose (not verified) on Sat, 01/26/2019 - 04:32

Permalink

cialis without prescription
buy cialis without prescription mayavanrosendaal.com cialis without prescription cialis without prescription

Submitted by Hootievier (not verified) on Fri, 02/15/2019 - 21:48

Permalink

I like rock bands! I really do! And my favourite yadrock band is Hootie & Blowfish! All band members has re-united to give more than 50 concerts to their fans in 2019! To know more about Hootie and The Blowfish in 2019 visit website Hootie and the Blowfish tour Washington. You won't miss any performance in 2019 if you click on the link!

Submitted by ulukliseh (not verified) on Mon, 02/18/2019 - 14:00

Permalink

Submitted by ohlevasevtjao (not verified) on Mon, 02/18/2019 - 14:45

Permalink

Submitted by ibwijaqu (not verified) on Mon, 02/18/2019 - 14:57

Permalink

Submitted by oqzakoigiwak (not verified) on Mon, 02/18/2019 - 14:58

Permalink

Submitted by qawuxoqguxiqu (not verified) on Mon, 02/18/2019 - 15:01

Permalink

Submitted by isuffamub (not verified) on Mon, 02/18/2019 - 15:01

Permalink

Submitted by iagsolequj (not verified) on Mon, 02/18/2019 - 15:16

Permalink

Submitted by epuzyunuunal (not verified) on Mon, 02/18/2019 - 15:27

Permalink

Submitted by auyulef (not verified) on Mon, 02/18/2019 - 15:33

Permalink

Submitted by oseyajajo (not verified) on Mon, 02/18/2019 - 15:49

Permalink

Submitted by ifugoliho (not verified) on Mon, 02/18/2019 - 16:20

Permalink

Submitted by iwiayefedta (not verified) on Mon, 02/18/2019 - 16:35

Permalink

Submitted by axipsita (not verified) on Mon, 02/18/2019 - 16:56

Permalink

Submitted by ecuxosk (not verified) on Mon, 02/18/2019 - 17:07

Permalink

Submitted by egopuhusibim (not verified) on Mon, 02/18/2019 - 17:25

Permalink

Submitted by aqavaxipa (not verified) on Mon, 02/18/2019 - 17:49

Permalink

Submitted by icatsampfi (not verified) on Mon, 02/18/2019 - 17:55

Permalink

Submitted by ipuzqolarasod (not verified) on Mon, 02/18/2019 - 17:56

Permalink

Submitted by itiokahiba (not verified) on Mon, 02/18/2019 - 18:11

Permalink

Submitted by eewurefujapie (not verified) on Mon, 02/18/2019 - 18:11

Permalink

Submitted by ixdaciboparun (not verified) on Mon, 02/18/2019 - 18:19

Permalink

Submitted by ihijedemaki (not verified) on Mon, 02/18/2019 - 18:24

Permalink

Submitted by elaziviuezux (not verified) on Mon, 02/18/2019 - 18:27

Permalink

Submitted by axamupdu (not verified) on Mon, 02/18/2019 - 18:42

Permalink

Submitted by patawupad (not verified) on Mon, 02/18/2019 - 18:48

Permalink

Submitted by iyicabgal (not verified) on Mon, 02/18/2019 - 18:58

Permalink

Complete nrq.xcnl.aknnn.org.ans.kn serotonin scaly, hamartomas [URL=http://sci-ed.org/buy-fluconazole/ - buy fluconazole[/URL - fluconazole for online http://sci-ed.org/buy-fluconazole/#fluconazole-for-online diflucan [URL=http://gocyclingcolombia.com/buy-cialis/ - lowest price on generic cialis[/URL - cialis http://gocyclingcolombia.com/buy-cialis/#buycialisonlinecanada.org buy cialis cialis and diarrhea [URL=http://scoverage.org/buy-cytotec/ - buy cytotec online[/URL - cytotec http://scoverage.org/buy-cytotec/#where-to-buy-cytotec-online cytotec [URL=http://columbia-electrochem-lab.org/cialis/ - cialis 20 mg price[/URL - cialis http://columbia-electrochem-lab.org/cialis/#cialis-generic cialis 20mg price at walmart buy cialis online [URL=http://fitnesscabbage.com/levitra-online/ - levitra how to use[/URL - buying levitra online http://fitnesscabbage.com/levitra-online/#levitra-20mg-information buy levitra cheap [URL=http://gocyclingcolombia.com/buy-lasix/ - lasix[/URL - buy lasix http://gocyclingcolombia.com/buy-lasix/#lasix buy furosemide sentence pharyngitis, incorrect.

Submitted by osvaqideiv (not verified) on Mon, 02/18/2019 - 18:58

Permalink

Submitted by aqcuvasiimf (not verified) on Mon, 02/18/2019 - 19:14

Permalink

Submitted by ixautifu (not verified) on Mon, 02/18/2019 - 19:46

Permalink

Submitted by ezuceriki (not verified) on Mon, 02/18/2019 - 20:01

Permalink

Submitted by oraxisejoja (not verified) on Mon, 02/18/2019 - 20:32

Permalink

Submitted by woekonupet (not verified) on Mon, 02/18/2019 - 20:32

Permalink

Submitted by udiipuvaaqipe (not verified) on Mon, 02/18/2019 - 20:48

Permalink

Submitted by apacedeq (not verified) on Mon, 02/18/2019 - 21:03

Permalink

Submitted by ilvifenasucef (not verified) on Mon, 02/18/2019 - 21:19

Permalink

Submitted by ehuaqik (not verified) on Mon, 02/18/2019 - 21:35

Permalink

Submitted by uxikoviyava (not verified) on Mon, 02/18/2019 - 21:51

Permalink

Submitted by ovuleha (not verified) on Mon, 02/18/2019 - 22:22

Permalink

Submitted by akibevom (not verified) on Mon, 02/18/2019 - 22:22

Permalink

Submitted by esoibuce (not verified) on Mon, 02/18/2019 - 22:38

Permalink

Submitted by ekmnoweniripu (not verified) on Mon, 02/18/2019 - 22:38

Permalink

Add new comment