Search Posts on Binpipe Blog

Django Installation on Centos 7 with Apache mod-wsgi & MariaDB

## Install the Components from the CentOS and EPEL Repositories

yum install epel-release
yum install python-pip httpd mod_wsgi
pip install --upgrade pip
yum install python-pip python-devel gcc mariadb-server mariadb-devel
sudo systemctl start mariadb
sudo systemctl enable mariadb
mysql_secure_installation


## Create a Database and Database User

mysql -u root -p

CREATE DATABASE noviceproj CHARACTER SET UTF8;
CREATE USER novice@localhost IDENTIFIED BY 'redhat';
GRANT ALL PRIVILEGES ON noviceproj.* TO novice@localhost;
FLUSH PRIVILEGES;

## Install Django within a Virtual Environment & Connect DB


pip install virtualenv
mkdir noviceproj
cd noviceproj/
virtualenv noviceenv
source noviceenv/bin/activate
pip install django mysqlclient



Open the main Django project settings file located within the child project directory:


vi ~/noviceproj/noviceproj/settings.py
Towards the bottom of the file, you will see a DATABASES section that looks like this:


. . .


DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


. . .
This is currently configured to use SQLite as a database. We need to change this so that our MariaDB database is used instead. To do that comment the above and append the below lines:



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'noviceproj',
'USER': 'novice',
'PASSWORD': 'redhat',
'HOST': 'localhost',
'PORT': '3306',
}
}


## Migrate the Database and Test your Project

cd ~/noviceproj/
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser


vi noviceproj/settings.py

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

./manage.py collectstatic

(set user/passwd for admin)


## Start Development Server

python manage.py runserver 0.0.0.0:8000

Quit the server with CONTROL-C.

To leave virtualenv type:

deactivate

## Setting up Apache Server with Mod_wsgi

vi /etc/httpd/conf.d/django.conf

#
Alias /static /root/noviceproj/static
<Directory /root/noviceproj/static >

Require all granted
</Directory>
<Directory /root/noviceproj/noviceproj>

<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess noviceproj python-path=/root/noviceproj:/root/noviceproj/noviceenv/lib/python2.7/site-packages

WSGIProcessGroup noviceproj
WSGIScriptAlias / /root/noviceproj/noviceproj/wsgi.py
#

usermod -a -G root apache
chmod 710 /home/user (not required)
chown :apache ~/noviceproj

systemctl restart httpd