Django 2.1 deployment on centos 7 with apache, mod_wsgi, python3 venv

8,007

I got everything to work. Here are the full steps:

yum install epel-release centos-release-scl 
yum install python36 python36-devel httpd httpd-devel rh-python36-mod_wsgi`  

Check if rh-python36-mod_wsgi places everything in correct dirs

rpm -ql rh-python36-mod_wsgi

If found in:

/opt/rh/httpd24/root/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf  
/opt/rh/httpd24/root/usr/lib64/httpd/modules/mod_rh-python36-wsgi.so  

Move them to:

/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf  
/etc/httpd/modules/mod_rh-python36-wsgi.so  

Create python3 venv. if you're moving from somewhere, not creating in, check SEL to make sure apache can use it

cd /var/www
python36 -m venv django-venv
source /var/www/django-venv/activate

Configure virtual host at /etc/httpd/conf.d/django.conf

<VirtualHost *:80>

ServerName mysite.com
ServerAlias www.mysite.com


WSGIDaemonProcess mysite python-home=/var/www/django-venv python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py

Alias /static /var/www/mysite/static
<Directory /var/www/mysite/static>
    Require all granted
</Directory>

<Directory /var/www/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

</VirtualHost>

If project is moved to /var/www, restore correct SEL labels:

restorecon -Rv /var/www

Make project dir owned by apache:

chown -R apache:apache /var/www/mysite

If SQLite is used, allow SEL to permit apache access to it (improvement needed):

semanage boolean -p http_unified on
Share:
8,007

Related videos on Youtube

J D
Author by

J D

Updated on September 18, 2022

Comments

  • J D
    J D over 1 year

    I've stumbled with this seemingly most relevant deployment option because guides seem to either reference mod_wsgi with python2, or deployment on deb based systems where expected paths are different.
    So I'm following these steps:

    #repos for python3.6, wsgi for python3.6
    yum install epel-release centos-release-scl
    
    #base packages
    yum install python36 python36-devel httpd httpd-devel rh-python36-mod_wsgi
    
    #python3.6 venv
    cd /var/www; 
    python36 -m venv django-venv
    source django-venv/bin/activate
    pip3 install django
    
    #apache config to support wsgi
    edit /etc/httpd/conf/httpd.conf to include
    LoadModule wsgi_module modules/mod_wsgi.so
    

    apache config to serve django content located at /var/www/mysite

    <VirtualHost *:80>
     ServerAdmin [email protected]
     ServerName mysite.com
     ServerAlias www.mysite.com
    
     WSGIDaemonProcess mysite python-home=/var/www/django-venv/ python-path=/var/www/django-venv/lib/python3.6/site-packages
     WSGIProcessGroup mysite
     WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
    
     Alias /static /var/www/mysite/static
     <Directory /var/www/mysite/mysite/static>
      Require all granted
     </Directory>
    
     <Directory /var/www/mysite/mysite>
      <Files wsgi.py>
      Require all granted
      </Files>
     </Directory>
    
    </VirtualHost>
    

    SEL changes:
    chown apache:apache -R /var/www/mysite chown apache:apache -R /var/www/django-venv

    httpd starts successfully but I keep getting the following in the error log:

    Current thread 0x00007f5b5a486880 (most recent call first):
    [Fri Nov 23 14:29:02.019635 2018] [core:notice] [pid 4837] AH00052: child pid 5159 exit signal Aborted (6)
    Fatal Python error: Py_Initialize: Unable to get the locale encoding
    ModuleNotFoundError: No module named 'encodings'
    

    Could you guys confirm what am I missing in the steps so far for this setup?

    Some other info:
    ll /etc/httpd/modules/*wsgi* -rwxr-xr-x. 1 root root 966K Nov 23 09:13 /etc/httpd/modules/mod_wsgi.so

    systemctl status -l httpd
    ● httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    Active: active (running) since Fri 2018-11-23 14:26:16 EET; 13min ago
    Docs: man:httpd(8)
    man:apachectl(8)
    Main PID: 4837 (httpd)
    Status: "Total requests: 3; Current requests/sec: 0; Current traffic:   0 B/sec"
    CGroup: /system.slice/httpd.service
    ├─4837 /usr/sbin/httpd -DFOREGROUND
    ├─4839 /usr/sbin/httpd -DFOREGROUND
    ├─4840 /usr/sbin/httpd -DFOREGROUND
    ├─4841 /usr/sbin/httpd -DFOREGROUND
    ├─4842 /usr/sbin/httpd -DFOREGROUND
    ├─4843 /usr/sbin/httpd -DFOREGROUND
    └─4850 /usr/sbin/httpd -DFOREGROUND
    Nov 23 14:26:16 www1 systemd[1]: Starting The Apache HTTP Server...
    Nov 23 14:26:16 www1 httpd[4837]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::a7a7:b61c:5ffc:b91a. Set the 'ServerName' directive globally to suppress this message
    Nov 23 14:26:16 www1 systemd[1]: Started The Apache HTTP Server.