1

I am having some issues getting a django project of mine to be properly served via mod_wsgi.

This is the first time I have tried something like this so please forgive any noob mistakes.

The error I am currently getting is as follows:

[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153] mod_wsgi (pid=20418): Exception occurred processing WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py'.
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153] Traceback (most recent call last):
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153]   File "/opt/badgr/code/apps/mainsite/wsgi.py", line 14, in <module>
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153]     from django.core.wsgi import get_wsgi_application
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153] ImportError: No module named wsgi

The main project directory is here:

/opt/badgr/

The project has its own virtualenv located here:

/opt/badgr/env/lib/python2.7/site-packages

My wsgi.py file:

"""
WSGI config for badgr 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/1.7/howto/deployment/wsgi/
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mainsite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

I also have a django.conf file located in /etc/httpd/conf.d/:

Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
</Directory>


<Directory /opt/badgr/code/apps/mainsite/>
    <Files wsgi.py>
    </Files>
</Directory>

WSGISocketPrefix /var/run/wsgi/wsgi
WSGIDaemonProcess badgr python-path=/opt/badgr:/opt/badgr/env/lib/python2.7/site-packages/
WSGIProcessGroup badgr
WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py

As far as I can tell mod_wsgi is installed correctly in the virtualenv so I am not sure why I am seeing this error when trying to connect via http to the site.

Here is my httpd.conf:

   ServerTokens OS
 /mod/mpm_common.html#lockfile>);
    ServerRoot "/etc/httpd"
    PidFile run/httpd.pid
    Timeout 60
    KeepAlive Off
    MaxKeepAliveRequests 100
    KeepAliveTimeout 15
    <IfModule prefork.c>
    StartServers       8
    MinSpareServers    5
    MaxSpareServers   20
    ServerLimit      256
    MaxClients       256
    MaxRequestsPerChild  4000
    </IfModule>
    <IfModule worker.c>
    StartServers         4
    MaxClients         300
    MinSpareThreads     25
    MaxSpareThreads     75 
    ThreadsPerChild     25
    MaxRequestsPerChild  0
    </IfModule>
    Listen 80
    LoadModule auth_basic_module modules/mod_auth_basic.so
    LoadModule auth_digest_module modules/mod_auth_digest.so
    LoadModule authn_file_module modules/mod_authn_file.so
    LoadModule authn_alias_module modules/mod_authn_alias.so
    LoadModule authn_anon_module modules/mod_authn_anon.so
    LoadModule authn_dbm_module modules/mod_authn_dbm.so
    LoadModule authn_default_module modules/mod_authn_default.so
    LoadModule authz_host_module modules/mod_authz_host.so
    LoadModule authz_user_module modules/mod_authz_user.so
    LoadModule authz_owner_module modules/mod_authz_owner.so
    LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
    LoadModule authz_dbm_module modules/mod_authz_dbm.so
    LoadModule authz_default_module modules/mod_authz_default.so
    LoadModule ldap_module modules/mod_ldap.so
    LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
    LoadModule include_module modules/mod_include.so
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule logio_module modules/mod_logio.so
    LoadModule env_module modules/mod_env.so
    LoadModule ext_filter_module modules/mod_ext_filter.so
    LoadModule mime_magic_module modules/mod_mime_magic.so
    LoadModule expires_module modules/mod_expires.so
    LoadModule deflate_module modules/mod_deflate.so
    LoadModule headers_module modules/mod_headers.so
    LoadModule usertrack_module modules/mod_usertrack.so
    LoadModule setenvif_module modules/mod_setenvif.so
    LoadModule mime_module modules/mod_mime.so
    LoadModule dav_module modules/mod_dav.so
    LoadModule status_module modules/mod_status.so
    LoadModule autoindex_module modules/mod_autoindex.so
    LoadModule info_module modules/mod_info.so
    LoadModule dav_fs_module modules/mod_dav_fs.so
    LoadModule vhost_alias_module modules/mod_vhost_alias.so
    LoadModule negotiation_module modules/mod_negotiation.so
    LoadModule dir_module modules/mod_dir.so
    LoadModule actions_module modules/mod_actions.so
    LoadModule speling_module modules/mod_speling.so
    LoadModule userdir_module modules/mod_userdir.so
    LoadModule alias_module modules/mod_alias.so
    LoadModule substitute_module modules/mod_substitute.so
    LoadModule rewrite_module modules/mod_rewrite.so
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule cache_module modules/mod_cache.so
    LoadModule suexec_module modules/mod_suexec.so
    LoadModule disk_cache_module modules/mod_disk_cache.so
    LoadModule cgi_module modules/mod_cgi.so
    LoadModule version_module modules/mod_version.so
    Include conf.d/*.conf
    User apache
    Group apache
    ServerAdmin root@localhost
    UseCanonicalName Off
    DocumentRoot "/var/www/html"

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory "/var/www/html">

        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all

    </Directory>


    <IfModule mod_userdir.c>

        UserDir disabled

    </IfModule>




    DirectoryIndex index.html index.html.var


    AccessFileName .htaccess


    <Files ~ "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy All
    </Files>

    TypesConfig /etc/mime.types

    DefaultType text/plain

    <IfModule mod_mime_magic.c>
        MIMEMagicFile conf/magic
    </IfModule>

    HostnameLookups Off

    ErrorLog logs/error_log

    LogLevel warn

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent


    CustomLog logs/access_log combined

    ServerSignature On


    Alias /icons/ "/var/www/icons/"

    <Directory "/var/www/icons">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>


    <IfModule mod_dav_fs.c>
        # Location of the WebDAV lock database.
        DAVLockDB /var/lib/dav/lockdb
    </IfModule>

    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

    <Directory "/var/www/cgi-bin">
        AllowOverride None
        Options None
        Order allow,deny
        Allow from all
    </Directory>


    IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable Charset=UTF-8

    AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip

    AddIconByType (TXT,/icons/text.gif) text/*
    AddIconByType (IMG,/icons/image2.gif) image/*
    AddIconByType (SND,/icons/sound2.gif) audio/*
    AddIconByType (VID,/icons/movie.gif) video/*

    AddIcon /icons/binary.gif .bin .exe
    AddIcon /icons/binhex.gif .hqx
    AddIcon /icons/tar.gif .tar
    AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
    AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
    AddIcon /icons/a.gif .ps .ai .eps
    AddIcon /icons/layout.gif .html .shtml .htm .pdf
    AddIcon /icons/text.gif .txt
    AddIcon /icons/c.gif .c
    AddIcon /icons/p.gif .pl .py
    AddIcon /icons/f.gif .for
    AddIcon /icons/dvi.gif .dvi
    AddIcon /icons/uuencoded.gif .uu
    AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
    AddIcon /icons/tex.gif .tex
    AddIcon /icons/bomb.gif /core

    AddIcon /icons/back.gif ..
    AddIcon /icons/hand.right.gif README
    AddIcon /icons/folder.gif ^^DIRECTORY^^
    AddIcon /icons/blank.gif ^^BLANKICON^^


    DefaultIcon /icons/unknown.gif

    ReadmeName README.html
    HeaderName HEADER.html

    IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t

    AddLanguage ca .ca
    AddLanguage cs .cz .cs
    AddLanguage da .dk
    AddLanguage de .de
    AddLanguage el .el
    AddLanguage en .en
    AddLanguage eo .eo
    AddLanguage es .es
    AddLanguage et .et
    AddLanguage fr .fr
    AddLanguage he .he
    AddLanguage hr .hr
    AddLanguage it .it
    AddLanguage ja .ja
    AddLanguage ko .ko
    AddLanguage ltz .ltz
    AddLanguage nl .nl
    AddLanguage nn .nn
    AddLanguage no .no
    AddLanguage pl .po
    AddLanguage pt .pt
    AddLanguage pt-BR .pt-br
    AddLanguage ru .ru
    AddLanguage sv .sv
    AddLanguage zh-CN .zh-cn
    AddLanguage zh-TW .zh-tw

    LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW


    ForceLanguagePriority Prefer Fallback

    AddDefaultCharset UTF-8

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddType application/x-x509-ca-cert .crt
    AddType application/x-pkcs7-crl    .crl

    AddHandler type-map var
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml


    Alias /error/ "/var/www/error/"

    <IfModule mod_negotiation.c>
    <IfModule mod_include.c>
        <Directory "/var/www/error">
            AllowOverride None
            Options IncludesNoExec
            AddOutputFilter Includes html
            AddHandler type-map var
            Order allow,deny
            Allow from all
            LanguagePriority en es de fr
            ForceLanguagePriority Prefer Fallback
        </Directory>


    </IfModule>
    </IfModule>


    BrowserMatch "Mozilla/2" nokeepalive
    BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
    BrowserMatch "RealPlayer 4\.0" force-response-1.0
    BrowserMatch "Java/1\.0" force-response-1.0
    BrowserMatch "JDK/1\.0" force-response-1.0


    BrowserMatch "Microsoft Data Access Internet Publishing Provider" redirect-carefully
    BrowserMatch "MS FrontPage" redirect-carefully
    BrowserMatch "^WebDrive" redirect-carefully
    BrowserMatch "^WebDAVFS/1.[0123]" redirect-carefully
    BrowserMatch "^gnome-vfs/1.0" redirect-carefully
    BrowserMatch "^XML Spy" redirect-carefully
    BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully

WSGI is loaded via a file in conf.d:

LoadModule wsgi_module modules/mod_wsgi.so

I haven't really edited much in the HTTPD.conf file besides just removing the comments so I could fit it on here.

I also am not sure why python seems to be running 2.6.# for httpd instead of 2.7.

Any thoughts or additional information appreciated. Also let me know if I should provide anything else.

3 Answers3

0

That error about django.core.wsgi is because you have a very old Django version installed, older than the version that your project was set up for.

This is likely going to be because your mod_wsgi is compiled for Python 2.6 and you have an older Django version installed in that Python installation.

If you want to use Python 2.7 you must reinstall mod_wsgi with one which is compiled for Python 2.7. You cannot force mod_wsgi compiled for Python 2.6 to use a virtual environment for Python 2.7.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Thanks for the reply Graham, – Sesshoumaru Oct 20 '16 at 18:35
  • How would one go about re-installing it. Is there some kind of flag I need to use when installing via pip or do I need to completely compile mod_wsgi from scratch? – Sesshoumaru Oct 20 '16 at 18:35
  • Ah, I just need to install python27-mod_wsgi with yum instead I believe. Will give that shot and post here. – Sesshoumaru Oct 20 '16 at 18:38
  • Okay so after doing the above I am still getting the same error. I am pretty much positive the module is getting loaded too. Should I try out Greg's suggestion? – Sesshoumaru Oct 20 '16 at 19:20
  • `[Thu Oct 20 14:16:55.576488 2016] [lbmethod_heartbeat:notice] [pid 23095] AH02282: No slotmem from mod_heartmonitor [Thu Oct 20 14:16:55.576555 2016] [:warn] [pid 23095] mod_wsgi: Compiled for Python/2.7.5. [Thu Oct 20 14:16:55.576564 2016] [:warn] [pid 23095] mod_wsgi: Runtime using Python/2.7.8. [Thu Oct 20 14:16:55.580256 2016] [mpm_prefork:notice] [pid 23095] AH00163: Apache/2.4.18 (Red Hat) mod_wsgi/3.4 Python/2.7.8 configured -- resuming normal operations [Thu Oct 20 14:16:55.580281 2016] [core:notice] [pid 23095] AH00094: Command line: '/opt/rh/httpd24/root/usr/sbin/httpd'` – Sesshoumaru Oct 20 '16 at 19:24
  • How many Python 2.7 installations do you have on your system? Was the virtual environment actually created from the system Python, or was it created from a separate Python installation such as Anaconda Python 2.7? – Graham Dumpleton Oct 20 '16 at 20:07
  • The virtual environment was created with 2.7. The system python is 2.6.6 I believe but the 2.7 is installed under /opt/rh/.. – Sesshoumaru Oct 25 '16 at 17:34
  • Any additional insight on this? The Project I am creating specifically requests Django 1.7.1. I have upgraded to 1.10.3 and am still getting the same error about django.core.wsgi. Does it have anything to do with this warning?: `[Tue Nov 01 11:43:11.827249 2016] [:warn] [pid 22163] mod_wsgi: Compiled for Python/2.7.5. [Tue Nov 01 11:43:11.827261 2016] [:warn] [pid 22163] mod_wsgi: Runtime using Python/2.7.8. ` – Sesshoumaru Nov 01 '16 at 16:48
  • I think my issue is I am not specifying the correct django settings module and not sure how to find it / specify it. – Sesshoumaru Nov 01 '16 at 19:15
  • I do think I figured out how to get the virtual environment side working though – Sesshoumaru Nov 01 '16 at 19:16
0

Note: I provided this answer that uses the mod_wsgi in pip because I had to do this myself recently to get it running in FreeBSD using Python 3. It isn't commonly done this way, but it should work.

If you want to install mod_wsgi from pip then you should give Apache the right path to mod_wsgi or install the module pip it creates into the Apache installation. If you are running it on your system using apt-get or pkg or BSD ports, then listen to the wise advice of Graham.

There are other things that could simultaneously be wrong, however I believe the proximate error is that your application cannot import wsgi. You should go into your environment by doing:

cd /opt/badgr
. env/bin/activate

If that fails try source env/bin/activate

And then installing mod_wsgi:

pip install mod_wsgi

This will install a script called mod_wsgi-express which will allow you to run Apache/mod_wsgi as a development server from the command line. The script can also help you to install the module it compiles into your Apache.

First we have to deal with fact that Apache already has mod_wsgi module installed from system packages. The best thing to do is uninstall the system package for mod_wsgi. This will likely remove that file in conf.d with the LoadModule line for wsgi_module.

Next run:

sudo mod_wsgi-express install-module

This should copy the module into the Apache modules directory and output two lines with a LoadModule and WSGIPythonHome directive. Add a new wsgi.conf file under conf.d directory and add those two lines to it. Restart Apache.

Doing it from pip in your virtualenv ensures it creates the right version of mod_wsgi with respect to the version of python you are running.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Greg Schmit
  • 4,275
  • 2
  • 21
  • 36
  • 1
    Using ``pip install mod_wsgi`` will not help in this case as they are not using the ``mod_wsgi-express`` method of using ``mod_wsgi``. It is a completely different way of deploying ``mod_wsgi`` and you don't mix them. – Graham Dumpleton Oct 19 '16 at 23:07
  • Ah, then I will update to reflect that. Thanks, I am a less-experienced contributor. – Greg Schmit Oct 19 '16 at 23:09
  • Graham, they would just have to point to the new .so file in the apache config, right? I want to make sure I am not misleading anyone. – Greg Schmit Oct 19 '16 at 23:27
  • Strictly speaking you aren't misleading anyone. The mod_wsgi-express script has a way of it telling you where the module is, and for copying it into the Apache installation but in general I find it confusing to people when mix the two different ways. I will add to your answer and elaborate on the best way if want to go that path. – Graham Dumpleton Oct 20 '16 at 00:21
  • Thanks Graham for adding a whole lot of clarity. – Greg Schmit Oct 20 '16 at 01:02
  • After adding WSGIPythonHome /opt/badgr/env I seen to be getting this error now: – Sesshoumaru Oct 20 '16 at 19:49
  • ImportError: No module named site (Sorry I keep hitting enter and forgetting that it submits the comment) – Sesshoumaru Oct 20 '16 at 19:50
  • Ok, so do you have an app named 'site', and are you importing it somewhere? Also, you should be able to edit your comments if you forget and hit enter. I just did a wsgi setup and I didn't use WSGIPythonHome, rather I used WSGIPythonPath where you specify your project directory and the location of site-packages in virtualenv (it would look like "/path/of/project:/path/of/project-env/lib/python3.5/site-packages", and then I also used "WSGIScriptAlias / /path/to/project/project/wsgi.py" inside my virtualhost. – Greg Schmit Oct 20 '16 at 20:58
  • Okay so after adding the python path I get this: `[Wed Oct 26 15:39:49.705334 2016] [:error] [pid 9533] [remote 140.225.0.153:244] Traceback (most recent call last): [Wed Oct 26 15:39:49.705366 2016] [:error] [pid 9533] [remote 140.225.0.153:244] File "/opt/badgr/code/apps/mainsite/wsgi.py", line 14, in [Wed Oct 26 15:39:49.705420 2016] [:error] [pid 9533] [remote 140.225.0.153:244] from django.core.wsgi import get_wsgi_application [Wed Oct 26 15:39:49.705449 2016] [:error] [pid 9533] [remote 140.225.0.153:244] ImportError: No module named django.core.wsgi ` – Sesshoumaru Oct 26 '16 at 20:40
  • I seemed to only be getting the 'site' errors when I had wsgipythonhome configured. I don't seem to be getting that error without it. – Sesshoumaru Oct 26 '16 at 20:41
  • That error is the error that Graham answered in his post. You should install django in your environment and ensure you're on the latest version. `code`pip install django`code` (or `code`pip3`code`) while in your virtualenv should work. – Greg Schmit Oct 26 '16 at 22:31
  • Tried upgrading to 1.10.3 and still no luck. Thanks for the continued assistance in this matter also. I normally just do sys-admin stuff so Python is pretty alien to me. – Sesshoumaru Nov 01 '16 at 17:02
  • Also when I have activated my virtual environment and run python and run this command: from django.core.wsgi import get_wsgi_application I don't get any errors. I feel like somehow my environment isn't getting used/started by httpd? – Sesshoumaru Nov 01 '16 at 18:04
  • I think my issue is I am not specifying the correct django settings module and not sure how to find it / specify it. I do think I figured out how to get the virtual environment side working though – Sesshoumaru Nov 01 '16 at 19:16
0

Atleast for this problem I think it was that I didn't grant enough rights to the site-packages folder and its contents. I made some progress after doing that but am now running into other issues that I have opened in another thread.

I gave the site-packages folder rwx recursively for group apache.

Here is the next part of my issues:

Issues getting Python 2.7 to work with my django project

Community
  • 1
  • 1