1

I am trying to publish the site created by Django using apache. The settings of the server are as follows.

・ CentOS 7.2
・ Python 3.6
・ Django 2.0
・ apache 2.4

I am preparing a virtual environment using pyenv as follows.

git clone https://github.com/yyuu/pyenv.git ~/.pyenv
…
pyenv install anaconda3-5.1.0
pyenv rehash
pyenv global anaconda3-5.1.0
…
yum install httpd httpd-devel
systemctl start httpd
systemctl enable httpd
….
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.14.tar.gz
tar -zxvf 4.5.14.tar.gz
cd mod_wsgi-4.5.14/
./configure --with-python=/home/username/.pyenv/versions/anaconda3-5.1.0/bin/python
make
sudo make install
…

■ httpd.conf

NameVirtualHost *:80
LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>

ServerName xxx.com
DocumentRoot /var/www/html

WSGIScriptReloading On
WSGIDaemonProcess xxx python-path=/home/username/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages python-home=/home/username/.pyenv/versions/anaconda3-5.1.0
WSGIProcessGroup xxx
WSGIScriptAlias / /var/www/html/xxx/xxx/wsgi.py

<Directory "/xxx/">
Order deny,allow
</Directory>
</VirtualHost>

The following error will occur with this setting.

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Setting chmod should be fine. What other reasons can be considered?

2 Answers 2

0

Version 4.5.14 of mod_wsgi is old, use the latest version.

Preferable to install it using pip install mod_wsgi to your Python installation of virtual environment as that ensures correct Python shared library is picked up, which when it doesn't occur, can cause this problem.

Then run mod_wsgi-express module-config to show the config lines to add to Apache to load mod_wsgi. This may include a LoadFile line as well to further enforce correct Python library is used, which for Anaconda Python is important.

Then use the config:

# I really hope you haven't thrown away your complete Apache
# config file. This should already be defined as part of the
# default Apache config file.

NameVirtualHost *:80

# Put here the output of running mod_wsgi-express module-config.

# Also turn of embedded mode using the following since using
# daemon mode.

WSGIRestrictEmbedded on


# This may not actually be required. Only needed on some RHEL
# and CentOS systems.

WSGISocketPrefix /var/run/wsgi

<VirtualHost *:80>

ServerName xxx.com
DocumentRoot /var/www/html

WSGIScriptReloading On

# The python-path wasn't needed. BTW, don't recommend using pyenv
# if using PSF Python as their default formula for building Python
# from source code doesn't create a shared library so it will not
# work with mod_wsgi. Anaconda Python is probably okay as it would
# use binary distro.

# Ensure python-home is what is value of sys.prefix for Python.

WSGIDaemonProcess xxx python-home=/home/username/.pyenv/versions/anaconda3-5.1.0

WSGIProcessGroup xxx

# Also force main interpreter if have only one app.

WSGIApplicationGroup %{GLOBAL}

WSGIScriptAlias / /var/www/html/xxx/xxx/wsgi.py

# Apache 2.4 should use 'Require all granted'.

# The ``/xxx`` path should match full absolute directory of WSGI
# script file used in ``WSGIScriptAlias``, so not right here.

<Directory "/xxx/">
Require all granted
</Directory>
</VirtualHost>

BTW, bad idea to put your Python source code under the same directory as DocumentRoot has. If you comment out WSGIScriptAlias, people can download your code.

1
  • BTW, you can also drop WSGIScriptReloading On as that is the default anyway. May 23, 2018 at 20:20
0

You should add

WSGIPythonHome /path/to/virtualenv/

to httpd.conf

https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.