1

I would like to install a Django website on my Ubuntu VPS with Apache and mod_wsgi. I use a virtualenv. But when I go the mysite.com I get a 500 Internal Server Error and the error.log tells me this:

[Sun Jan 25 18:42:36 2015] [error] [client xx.xx.xx.xx]   File "/usr/local/lib/python2.7/dist-packages/django/core/cache/backends/memcached.py", line 155, in __init__
[Sun Jan 25 18:42:36 2015] [error] [client xx.xx.xx.xx]     import memcache
[Sun Jan 25 18:42:36 2015] [error] [client xx.xx.xx.xx] ImproperlyConfigured: Error importing module django.contrib.sessions.middleware: "No module named memcache"

It told me that memcached is not installed, but it seems that it looks at the wrong python/site-packages path, not the one of my virtualenv. I have installed memcached in my virtualenv.

Here is my VirtualHost for "mysite"

<VirtualHost *:80>

    ServerName www.mysite.com
    ServerAlias mysite.com
    ServerAdmin [email protected]

    Alias /uploads/ /var/www/mysite/uploads/
    Alias /static/ /var/www/mysite/myapp/static/

    WSGIDaemonProcess mysite  python-path=/var/www/mysite:/root/.virtualenvs/mysite_env/lib/python3.2/site-packages
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py

    <Directory /var/www/mysite/myapp/static>
      Order allow,deny
      Allow from all
    </Directory>

    <Directory /var/www/mysite/uploads>
      Order allow,deny
      Allow from all
    </Directory>


    <Directory /var/www/mysite/mysite>
      <Files wsgi.py>
      Order allow,deny
      Allow from all
      </Files>
    </Directory>


</VirtualHost>

I don't know if that can help but I would precise that I currently run other PHP websites in this VPS, and each website has its own VirtualHost file.

Any help would be greatly appreciated!

2 Answers 2

1

Your apache.conf looks reasonable... here is one of mine as a comparison

WSGIScriptAlias / ${SRV}/www/example/wsgi.py
WSGIProcessGroup www.example.no
WSGIDaemonProcess www.example.no \
    display-name=example \
    threads=50 \
    maximum-requests=10000 \
    umask=0002 \
    inactivity-timeout=3600 \
    home=${SRV}/www/example \
    python-path=${SRV}/www:${SRV}/src:${SRV}/venv/dev/lib/python2.7/site-packages \
    python-eggs=${SRV}/.python-eggs

the ${SRV} comes from mod_define which I've manually compiled for Apache 2.2, but should be included in 2.4(?)

You should follow the steps listed here: https://code.google.com/p/modwsgi/wiki/VirtualEnvironments. It is important to note, however, that even when using a virtualenv you need to use the same Python version as mod_wsgi was built with (and it looks like you're trying to use Py3 on a Py2 configuration).

I've modified the wsgi.py file to read config values from site.ini:

from ConfigParser import ConfigParser
import sys, os

# server variables
SITE_ROOT = os.path.split(__file__)[0]
WWW = os.path.split(SITE_ROOT)[0]

config = ConfigParser()
config.read([os.path.join(DK_SITE_ROOT, 'site.ini')])

# site properties
SITE_NAME = config.get('site', 'sitename')
DNS = config.get('site', 'dns')
SRV = config.get('server', 'srv')
# virtualenv variables
VENV_NAME = "dev"   # only

# derived settings
DJANGO_SETTINGS_MODULE = "%s.settings" % SITE_NAME
VIRTUAL_ENV = "%s/venv/%s" % (SRV, VENV_NAME)

# activate virtualenv
# (warning: https://code.google.com/p/modwsgi/wiki/CheckingYourInstallation)
_activate = "%s/%s/activate_this.py" % (
    VIRTUAL_ENV,
    'Scripts' if sys.platform == 'win32' else 'bin'
)
if sys.version_info >= (3, 0):
    exec(compile(open(_activate, 'rb').read(), _activate, 'exec'))
else:
    execfile(_activate, dict(__file__=_activate))

# make site directory importable
sys.path.insert(0, WWWDIR)

# set all GLOBAL vars as environment variables
for _varname in [k for k in globals().keys() if k == k.upper()]:
    _val = globals()[_varname]
    if type(_val) == str:
        os.environ[_varname] = _val

# This application object is used by the development server
# as well as any WSGI server configured to use this file. 
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

(it's been a while since I tested this with Py3, but it works great with Py2).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Actually my mod_wsgi had not been compiled against python3. I solved it by doing : apt-get install libapache2-mod-wsgi-py3.
1

I normally configure the virtualenv import in my wsgi.py file and it works. Just by adding the lines:

site.addsitedir('/home/{my_user}/.virtualenvs/{virtualenv}/local/lib/python2.7/site-packages')
activate_env=os.path.expanduser("/home/{my_user}/.virtualenvs/{virtualenv}/bin/activate_this.py")
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except:
    pass

And that's the configuration for my virtualhost:

<VirtualHost yourhost>
     ServerName server.name.com
     WSGIScriptAlias / /path/to/wsgi.py
     Alias /static/ /path/to/static/
     Alias /media/ /path/to/media/
     DocumentRoot /path/to/project

     ErrorLog /var/log/apache2/project_error.log
</VirtualHost>

Something more detailed here.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.