Django Server setup with Apache with mod_wsgi in Centos 9
Description
Python application needs to be configured with apache webserver to publish the host.
The reason for mod_wsgi is the middleware which talks between python application and Apache
Overall Flow
installing the httpd service and modwsgi
sudo yum install httpd mod-wsgi
sudo systemctl start httpd
sudo systemctl status httpd
sudo systemctl enable httpd
httpd -v
sudo systemctl restart httpd
systemctl status httpd.service
Adding the user
[root@localhost ~]# adduser dj_adm
[root@localhost ~]# passwd dj_adm
Changing password for user dj_adm.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#
Create the virtual environment using python3
[dj_adm@localhost ~]$ virtualenv -p python3 ~/virtualenvs/dj-env
created virtual environment CPython3.9.10.final.0-64 in 696ms
creator CPython3Posix(dest=/home/dj_adm/virtualenvs/dj-env, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/dj_adm/.local/share/virtualenv)
added seed packages: pip==22.0.4, setuptools==61.0.0, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
[dj_adm@localhost ~]$
Activating Virtual environment and installing django
[dj_adm@localhost ~]$ source ~/virtualenvs/dj-env/bin/activate
(dj-env) [dj_adm@localhost ~]$ pip install django
Collecting django
Downloading Django-4.0.3-py3-none-any.whl (8.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.0/8.0 MB 7.6 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.3/42.3 KB 2.1 MB/s eta 0:00:00
Collecting asgiref<4,>=3.4.1
Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.5.0 django-4.0.3 sqlparse-0.4.2
cloning the git repo
(dj-env) [dj_adm@localhost ~]$ git clone https://github.com/velmuruganpon/dj-practice.git
Cloning into 'dj-practice'...
Username for 'https://github.com': vel----@gmail.com
Password for 'https://vel-----@gmail.com@github.com':
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
(dj-env) [dj_adm@localhost ~]$ cd dj-practice/
(dj-env) [dj_adm@localhost dj-practice]$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
(dj-env) [dj_adm@localhost dj-practice]$ tree
.
└── README.md
0 directories, 1 file
(dj-env) [dj_adm@localhost dj-practice]$
Creating the first project called "mysite"
(dj-env) [dj_adm@localhost dj-practice]$ django-admin startproject mysite
(dj-env) [dj_adm@localhost dj-practice]$ tree
.
├── mysite
│ ├── manage.py
│ └── mysite
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── README.md
2 directories, 7 files
changing the wsgi file
dj-env) [dj_adm@localhost dj-practice]$ cat mysite/mysite/wsgi.py
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""
python_home = '/home/dj_adm/virtualenvs/dj-env'
activate_this = python_home + '/bin/activate_this.py'
with open(activate_this) as f:
code = compile(f.read(), activate_this, 'exec')
exec(code, dict(__file__=activate_this))
import os
import sys
from django.core.wsgi import get_wsgi_application
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
permission changes
chmod -R o+x /home/dj_adm/selinux changes using root
chcon -R system_u:object_r:httpd_sys_script_exec_t:s0 /home/dj_adm/virtualenvs/dj-env
sudo chcon -R system_u:system_r:httpd_sys_script_exec_t:s0 /home/dj_adm/dj-practice/mysite
create the django.conf under /etc/httpd/conf.d/
[root@localhost opt]# cat /etc/httpd/conf.d/django.conf
<VirtualHost *:80>
<Directory /home/dj_adm/dj-practice/mysite/mysite>
<files wsgi.py>
Require all granted
</files>
</Directory>
WSGIDaemonProcess mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /home/dj_adm/dj-practice/mysite/mysite/wsgi.py process-group=mysite
</VirtualHost>
restart the httpd service
[root@localhost opt]# sudo systemctl restart httpd
Permanent Selinux Changes using root
semanage fcontext -a -t httpd_sys_script_exec_t /home/dj_adm/virtualenvs/dj-env
semanage fcontext -a -t httpd_sys_script_exec_t /home/dj_adm/dj-practice/mysite
To verify the selinux changes
[root@localhost opt]# cat /etc/selinux/targeted/contexts/files/file_contexts.local
# This file is auto-generated by libsemanage
# Do not edit directly.
/home/dj_adm/virtualenvs/dj-env system_u:object_r:httpd_sys_script_exec_t:s0
/home/dj_adm/dj-practice/mysite system_u:object_r:httpd_sys_script_exec_t:s0
[root@localhost opt]#
Comments
Post a Comment