Setup Django with MySQL on CentOS 7

Django is popular Web framework for writing web applications. With django you can built faster and scalable applications, without writing the code from scratch (without reinventing the wheel). Web framework were design to aid programmer to create applications. These web framework are the core and takes care of functionalities like user session managemet, database connectivity etc.

CentOS 7 still have Python 2.7 as the default tool. EPEL repository comes up with Python 3. Thanks to EPEL repo. Here’s the way to setup Python 3 on CentOS environment.

Install python34:

[root@wace ~]# yum install python34 python34-devel python34-setuptools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * epel: epel.excellmedia.net
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
Resolving Dependencies
--> Running transaction check
---> Package python34.x86_64 0:3.4.5-3.el7 will be installed
--> Processing Dependency: python34-libs(x86-64) = 3.4.5-3.el7 for package: python34-3.4.5-3.el7.x86_64
--> Processing Dependency: libpython3.4m.so.1.0()(64bit) for package: python34-3.4.5-3.el7.x86_64
---> Package python34-devel.x86_64 0:3.4.5-3.el7 will be installed
--> Processing Dependency: python3-rpm-macros for package: python34-devel-3.4.5-3.el7.x86_64
--> Processing Dependency: python-rpm-macros for package: python34-devel-3.4.5-3.el7.x86_64
---> Package python34-setuptools.noarch 0:19.2-3.el7 will be installed
--> Running transaction check
---> Package python-rpm-macros.noarch 0:3-10.el7 will be installed
--> Processing Dependency: python-srpm-macros for package: python-rpm-macros-3-10.el7.noarch
---> Package python3-rpm-macros.noarch 0:3-10.el7 will be installed
---> Package python34-libs.x86_64 0:3.4.5-3.el7 will be installed
--> Running transaction check
---> Package python-srpm-macros.noarch 0:3-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
===========================================================================================================================================================
 Package                                       Arch                             Version                               Repository                      Size
===========================================================================================================================================================
Installing:
 python34                                      x86_64                           3.4.5-3.el7                           epel                            50 k
 python34-devel                                x86_64                           3.4.5-3.el7                           epel                           184 k
 python34-setuptools                           noarch                           19.2-3.el7                            epel                           373 k
Installing for dependencies:
 python-rpm-macros                             noarch                           3-10.el7                              epel                           5.0 k
 python-srpm-macros                            noarch                           3-10.el7                              epel                           4.4 k
 python3-rpm-macros                            noarch                           3-10.el7                              epel                           4.5 k
 python34-libs                                 x86_64                           3.4.5-3.el7                           epel                           6.7 M

Transaction Summary
===========================================================================================================================================================
Install  3 Packages (+4 Dependent packages)

Total download size: 7.3 M
Installed size: 29 M
Is this ok [y/d/N]: y
Downloading packages:
(1/7): python-rpm-macros-3-10.el7.noarch.rpm                                                                                        | 5.0 kB  00:00:00
(2/7): python-srpm-macros-3-10.el7.noarch.rpm                                                                                       | 4.4 kB  00:00:00
(3/7): python3-rpm-macros-3-10.el7.noarch.rpm                                                                                       | 4.5 kB  00:00:01
(4/7): python34-devel-3.4.5-3.el7.x86_64.rpm                                                                                        | 184 kB  00:00:01
(5/7): python34-3.4.5-3.el7.x86_64.rpm                                                                                              |  50 kB  00:00:01
(6/7): python34-setuptools-19.2-3.el7.noarch.rpm                                                                                    | 373 kB  00:00:03
(7/7): python34-libs-3.4.5-3.el7.x86_64.rpm                                                                                         | 6.7 MB  00:00:22
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                      329 kB/s | 7.3 MB  00:00:22
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python34-3.4.5-3.el7.x86_64                                                                                                             1/7
  Installing : python34-libs-3.4.5-3.el7.x86_64                                                                                                        2/7
  Installing : python3-rpm-macros-3-10.el7.noarch                                                                                                      3/7
  Installing : python-srpm-macros-3-10.el7.noarch                                                                                                      4/7
  Installing : python-rpm-macros-3-10.el7.noarch                                                                                                       5/7
  Installing : python34-devel-3.4.5-3.el7.x86_64                                                                                                       6/7
  Installing : python34-setuptools-19.2-3.el7.noarch                                                                                                   7/7
  Verifying  : python34-devel-3.4.5-3.el7.x86_64                                                                                                       1/7
  Verifying  : python34-setuptools-19.2-3.el7.noarch                                                                                                   2/7
  Verifying  : python-srpm-macros-3-10.el7.noarch                                                                                                      3/7
  Verifying  : python34-libs-3.4.5-3.el7.x86_64                                                                                                        4/7
  Verifying  : python-rpm-macros-3-10.el7.noarch                                                                                                       5/7
  Verifying  : python3-rpm-macros-3-10.el7.noarch                                                                                                      6/7
  Verifying  : python34-3.4.5-3.el7.x86_64                                                                                                             7/7

Installed:
  python34.x86_64 0:3.4.5-3.el7                 python34-devel.x86_64 0:3.4.5-3.el7                 python34-setuptools.noarch 0:19.2-3.el7

Dependency Installed:
  python-rpm-macros.noarch 0:3-10.el7   python-srpm-macros.noarch 0:3-10.el7   python3-rpm-macros.noarch 0:3-10.el7   python34-libs.x86_64 0:3.4.5-3.el7

Complete!
[root@wace ~]#
[root@wace ~]# easy_install-3.4 pip
Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 9.0.1
Downloading https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
Processing pip-9.0.1.tar.gz
Writing /tmp/easy_install-emaejyks/pip-9.0.1/setup.cfg
Running pip-9.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-emaejyks/pip-9.0.1/egg-dist-tmp-6i9gci5o
/usr/lib64/python3.4/distutils/dist.py:260: UserWarning: Unknown distribution option: 'python_requires'
  warnings.warn(msg)
warning: no previously-included files found matching '.coveragerc'
warning: no previously-included files found matching '.mailmap'
warning: no previously-included files found matching '.travis.yml'
warning: no previously-included files found matching '.landscape.yml'
warning: no previously-included files found matching 'pip/_vendor/Makefile'
warning: no previously-included files found matching 'tox.ini'
warning: no previously-included files found matching 'dev-requirements.txt'
warning: no previously-included files found matching 'appveyor.yml'
no previously-included directories found matching '.github'
no previously-included directories found matching '.travis'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
creating /usr/lib/python3.4/site-packages/pip-9.0.1-py3.4.egg
Extracting pip-9.0.1-py3.4.egg to /usr/lib/python3.4/site-packages
Adding pip 9.0.1 to easy-install.pth file
Installing pip script to /usr/bin
Installing pip3.4 script to /usr/bin
Installing pip3 script to /usr/bin

Installed /usr/lib/python3.4/site-packages/pip-9.0.1-py3.4.egg
Processing dependencies for pip
Finished processing dependencies for pip
[root@wace ~]#
[root@wace ~]# python -V
Python 2.7.5
[root@wace ~]# python3 -V
Python 3.4.5
[root@wace ~]# pip3 -V
pip 9.0.1 from /usr/lib/python3.4/site-packages/pip-9.0.1-py3.4.egg (python 3.4)
[root@wace ~]#

Successfully, installed Python3 with pip3.

It is recommended to install development tools.

 [root@wace ~]# yum groupinstall 'Development Tools'

Install MySQL Server:

MySQL is the most popular database management system used for powering web applications. MariaDB becomes the default database and MySQL is no longer available in CentOS 7. MariaDB is a drop in replacement for MySQL. In order to install MySQL, we have to install MySQL repository from mysql.com site.

Adding MySQL 5.7 repo to CentOS.

[root@wace ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
--2016-12-10 12:39:18--  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm [following]
--2016-12-10 12:39:19--  http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.35.82.159
Connecting to repo.mysql.com (repo.mysql.com)|23.35.82.159|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9224 (9.0K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-9.noarch.rpm’

100%[=================================================================================================================>] 9,224       --.-K/s   in 0.1s

2016-12-10 12:39:19 (63.5 KB/s) - ‘mysql57-community-release-el7-9.noarch.rpm’ saved [9224/9224]
[root@wace ~]#
[root@wace ~]# yum install mysql57-community-release-el7-9.noarch.rpm
Loaded plugins: fastestmirror
Examining mysql57-community-release-el7-9.noarch.rpm: mysql57-community-release-el7-9.noarch
Marking mysql57-community-release-el7-9.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
===========================================================================================================================================================
 Package                                    Arch                    Version                 Repository                                                Size
===========================================================================================================================================================
Installing:
 mysql57-community-release                  noarch                  el7-9                   /mysql57-community-release-el7-9.noarch                  8.6 k
Transaction Summary
===========================================================================================================================================================
Install  1 Package

Total size: 8.6 k
Installed size: 8.6 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-9.noarch                                                                                                  1/1
  Verifying  : mysql57-community-release-el7-9.noarch                                                                                                  1/1

Installed:
  mysql57-community-release.noarch 0:el7-9

Complete!
[root@wace ~]#

Install MySQL

[root@wace ~]# yum install mysql-community-server mysql-community-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * epel: epel.excellmedia.net
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-devel.x86_64 0:5.7.16-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-devel-5.7.16-1.el7.x86_64
--> Processing Dependency: libmysqlclient.so.20()(64bit) for package: mysql-community-devel-5.7.16-1.el7.x86_64
---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.16-1.el7 for package: mysql-community-server-5.7.16-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.16-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.44-2.el7.centos will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-client.x86_64 0:5.7.16-1.el7 will be installed
---> Package mysql-community-common.x86_64 0:5.7.16-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.16-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================
 Package                                         Arch                       Version                            Repository                             Size
===========================================================================================================================================================
Installing:
 mysql-community-devel                           x86_64                     5.7.16-1.el7                       mysql57-community                     3.6 M
 mysql-community-libs                            x86_64                     5.7.16-1.el7                       mysql57-community                     2.1 M
     replacing  mariadb-libs.x86_64 1:5.5.44-2.el7.centos
 mysql-community-libs-compat                     x86_64                     5.7.16-1.el7                       mysql57-community                     2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.44-2.el7.centos
 mysql-community-server                          x86_64                     5.7.16-1.el7                       mysql57-community                     152 M
Installing for dependencies:
 mysql-community-client                          x86_64                     5.7.16-1.el7                       mysql57-community                      24 M
 mysql-community-common                          x86_64                     5.7.16-1.el7                       mysql57-community                     271 k

Transaction Summary
===========================================================================================================================================================
Install  4 Packages (+2 Dependent packages)

Total download size: 184 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.16-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.7.16-1.el7.x86_64.rpm is not installed
(1/6): mysql-community-common-5.7.16-1.el7.x86_64.rpm                                                                               | 271 kB  00:00:00
(2/6): mysql-community-devel-5.7.16-1.el7.x86_64.rpm                                                                                | 3.6 MB  00:00:01
(3/6): mysql-community-libs-5.7.16-1.el7.x86_64.rpm                                                                                 | 2.1 MB  00:00:01
(4/6): mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm                                                                          | 2.0 MB  00:00:00
(5/6): mysql-community-client-5.7.16-1.el7.x86_64.rpm                                                                               |  24 MB  00:00:12
(6/6): mysql-community-server-5.7.16-1.el7.x86_64.rpm                                                                               | 152 MB  00:01:02
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                      2.8 MB/s | 184 MB  00:01:06
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-9.noarch (@/mysql57-community-release-el7-9.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.16-1.el7.x86_64                                                                                              1/7
  Installing : mysql-community-libs-5.7.16-1.el7.x86_64                                                                                                2/7
  Installing : mysql-community-client-5.7.16-1.el7.x86_64                                                                                              3/7
  Installing : mysql-community-server-5.7.16-1.el7.x86_64                                                                                              4/7
  Installing : mysql-community-libs-compat-5.7.16-1.el7.x86_64                                                                                         5/7
  Installing : mysql-community-devel-5.7.16-1.el7.x86_64                                                                                               6/7
  Erasing    : 1:mariadb-libs-5.5.44-2.el7.centos.x86_64                                                                                               7/7
  Verifying  : mysql-community-libs-5.7.16-1.el7.x86_64                                                                                                1/7
  Verifying  : mysql-community-libs-compat-5.7.16-1.el7.x86_64                                                                                         2/7
  Verifying  : mysql-community-client-5.7.16-1.el7.x86_64                                                                                              3/7
  Verifying  : mysql-community-devel-5.7.16-1.el7.x86_64                                                                                               4/7
  Verifying  : mysql-community-common-5.7.16-1.el7.x86_64                                                                                              5/7
  Verifying  : mysql-community-server-5.7.16-1.el7.x86_64                                                                                              6/7
  Verifying  : 1:mariadb-libs-5.5.44-2.el7.centos.x86_64                                                                                               7/7

Installed:
  mysql-community-devel.x86_64 0:5.7.16-1.el7       mysql-community-libs.x86_64 0:5.7.16-1.el7      mysql-community-libs-compat.x86_64 0:5.7.16-1.el7
  mysql-community-server.x86_64 0:5.7.16-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.16-1.el7                                 mysql-community-common.x86_64 0:5.7.16-1.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.44-2.el7.centos

Complete!
[root@wace ~]#

Create a mysql user account for python to access.

Install Django

You can use pip to install the virtual environment and verify it using pip list.

[root@wace ~]# pip install virtualenv
Collecting virtualenv
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 305kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
[root@wace ~]#
[root@wace ~]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (19.2)
virtualenv (15.1.0)
[root@wace ~]#

Create virtual environment using virtualenv command. This command will create a directory and setup all the files.

[root@wace ~]# virtualenv env_mysite
Using base prefix '/usr'
New python executable in /root/env_mysite/bin/python3.4
Also creating executable in /root/env_mysite/bin/python
Installing setuptools, pip, wheel...done.
[root@wace ~]#

Now, you can activate the virtual environment. The ssh prompt will change as below. After that you can verify the Python version.

[root@wace ~]# cd env_mysite/
[root@wace env_mysite]# source bin/activate
(env_mysite) [root@wace env_mysite]# python
Python 3.4.5 (default, Nov  9 2016, 16:24:59)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
(env_mysite) [root@wace env_mysite]#

Let’s install django into the virtual environemt.

(env_mysite) [root@wace env_mysite]# pip install mysqlclient
Collecting mysqlclient
  Downloading mysqlclient-1.3.9.tar.gz (81kB)
    100% |████████████████████████████████| 81kB 84kB/s
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... done
  Stored in directory: /root/.cache/pip/wheels/97/65/8b/64875621ce64dc57bb8bf6d40931392b1e54497d8e952ecc37
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.9
(env_mysite) [root@wace env_mysite]#
(env_mysite) [root@wace env_mysite]# pip install django
Collecting django
  Downloading Django-1.10.4-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 186kB/s
Installing collected packages: django
Successfully installed django-1.10.4
(env_mysite) [root@wace env_mysite]#
(env_mysite) [root@wace env_mysite]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Django (1.10.4)
mysqlclient (1.3.9)
pip (9.0.1)
setuptools (30.4.0)
wheel (0.29.0)
(env_mysite) [root@wace env_mysite]#

Django framework has been installed, now you start a new project. This command will create a new directory in the working directory.

(env_mysite) [root@wace env_mysite]# django-admin startproject mysite
(env_mysite) [root@wace env_mysite]#
(env_mysite) [root@wace env_mysite]# tree mysite
mysite
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files
(env_mysite) [root@wace env_mysite]#

Start the application using the below command and migrate, create admin user.

(env_mysite) [root@wace mysite]# python manage.py migrate
Operations to perform: