WebFaction
Community site: login faq

I am trying to get a Django application to run in a virtualenv. There are probably hundreds of tutorials like this, but they are all different, and I assume many are out of date. In the end none of them helped me. I am not trying to achieve anything specific right now. I just want to see the welcome page of a Django 2 app that runs in a virtualenv in a Webfaction mod_wsgi application.

My basic problem is that that the default app has htdocs/index.py (Welcome to your mod_wsgi website...), but I don't know if the Django app has an equivalent to index.py. So I would not know how to replace the values of DirectoryIndex and DocumentRoot. (Almost none of the tutorials use these parameters. And the one I found has this weird from main import app, where I again don't know the Django equivalent.)

These are the places in /home/watchduck/webapps/foo that httpd.conf should probably know about:

  • proj/proj/wsgi.py
  • venv
  • the location of Python in venv

I am not quite sure about the location of Python. which python gives me venv/bin/python (a link to python3 in the same folder). But there is also venv/lib/python3.5. I added the latter to python-path, as suggested here.

Following the rather terse documentation led me to an httpd.conf that looks like this:

ServerRoot "/home/watchduck/webapps/foo/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/watchduck/logs/user/access_foo.log combined
ErrorLog /home/watchduck/logs/user/error_foo.log

Listen 23993
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess foo processes=2 threads=12 python-home=/home/watchduck/webapps/foo/venv python-path=/home/watchduck/webapps/foo/venv/lib/python3.5:/home/watchduck/webapps/foo/venv/lib/python3.5/site-packages:/home/watchduck/webapps/foo:/home/watchduck/webapps/foo/proj
WSGIProcessGroup foo
WSGIRestrictEmbedded On
WSGILazyInitialization On

WSGIScriptAlias / /home/watchduck/webapps/foo/proj/proj/wsgi.py

Here are the paths in WSGIDaemonProcess in human readable form:

python-home
    /home/watchduck/webapps/foo/venv 
python-path
    /home/watchduck/webapps/foo/venv/lib/python3.5
    /home/watchduck/webapps/foo/venv/lib/python3.5/site-packages
    /home/watchduck/webapps/foo
    /home/watchduck/webapps/foo/proj

As mentioned above, I did not know how to modify these two lines, so I just removed them:

DirectoryIndex index.py
DocumentRoot /home/watchduck/webapps/foo/htdocs

Same for this:

< Directory /home/watchduck/webapps/foo/htdocs >
    Options +ExecCGI
    AddHandler wsgi-script .py
< / Directory >

With the WSGIScriptAlias in the last line I get a Gateway Timeout. Without it just a Not Found.

In any case I get these errors in the log file:

Current thread 0x00007f0c09c6d780 (most recent call first):
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f0c09c6d780 (most recent call first):
[Wed Nov 21 22:45:39.047412 2018] [core:notice] [pid 1880:tid 139689680361344] AH00052: child pid 20557 exit signal Aborted (6)
[Wed Nov 21 22:45:39.047899 2018] [core:notice] [pid 1880:tid 139689680361344] AH00052: child pid 20558 exit signal Aborted (6)
Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f0c09c6d780 (most recent call first):
Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

The comments I found about this problem suggest that something is wrong with the Python path.
Can anyone tell me what?

Am I right to assume, that httpd.conf is basically the only place where I have to make these changes? In this case, could someone please just share one that works? If it is more complicated, it would be great to have a basic example project on GitHub.

asked 21 Nov, 23:05

watchduck
133
accept rate: 0%

edited 22 Nov, 14:58


If you have just created a mod_wsgi app with python 3.7, named wsgi, then you can do the following:

cd ~/webapss/wsgi
rm -rf lib htdocs
python3.7 -m venv venv
source venv/bin/activate
pip install django
django-admin startproject myproject

Now edit the httpd.conf file and remove DirectoryIndex, DocumentRoot and the 4 lines of the <Directory> section. Then, locate the python-path part of the WSGIDaemonProcess line and replace it with the location of your project and add the python-home=/home/your-username/webapps/wsgi/venv Finally, add a line pointing to your wsgi.py file that looks like WSGIScriptAlias / /home/your-username/webapps/wsgi/myproject/myproject/wsgi.py

In the end, it should look like:

ServerRoot "/home/your-username/webapps/wsgi/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/your-username/logs/user/access_wsgi.log combined
ErrorLog /home/your-username/logs/user/error_wsgi.log

Listen 15296
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess wsgi processes=2 threads=12 python-path=/home/your-username/webapps/wsgi/myproject python-home=/home/your-username/webapps/wsgi/venv
WSGIProcessGroup wsgi
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/your-username/webapps/wsgi/myproject/myproject/wsgi.py
permanent link

answered 22 Nov, 14:13

iliasr ♦♦
2.0k14
accept rate: 36%

That httpd.conf is basically the same as the one I have shown above. The only difference is, that you have reduced python-path to foo/proj (your wsgi/myproject). This change did not make the ModuleNotFoundError go away. But I have reinstalled the virtual environment with your command python3.7 -m venv venv, and that did the trick. Before I had used virtualenv -p python3 venv --no-site-packages. What was wrong with that? virtualenv -p python3 venv is what I use locally. --no-site-packages is deprecated and should have no effect (as I just learned).

(22 Nov, 14:56) watchduck

There shouldn't be a difference using python's built-in way to create virtual environments and the 3rd-party tool. The result should be the similar.

We can't say why this wasn't working for you just by this description. We would need to check the configuration as it was and reproduce this problem to find the issue.

(22 Nov, 15:06) iliasr ♦♦

It was in a fresh mod_wsgi 4.6.5/Python3.7 app, created in the control panel. So it should be easy to reproduce, if that's what you want to do. Maybe the venv created with the third-party-tool really lacks the encodings package for some reason.

(22 Nov, 15:28) watchduck

The only thing you have to do for a virtualenv is to specify its module path on the --python-path flag of the WSGIDaemonProcess directive. Anything else is not required. You would modify that line by using a colon ':' to separate the paths, adding the new path for your virtual env's module path.

permanent link

answered 22 Nov, 00:52

johns ♦♦
5.3k212
accept rate: 23%

By "virtual env's mudule path" you mean /home/watchduck/webapps/foo/env, right? The documentation suggested to add that as python-home, but I also added it to python-path now. But the ModuleNotFoundError does not go away.

(22 Nov, 12:40) watchduck
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×902
×55
×55
×28

question asked: 21 Nov, 23:05

question was seen: 174 times

last updated: 22 Nov, 15:36

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM