1

I know that the question has already been asked here. I've read tons of answers, but nothing helped.

I'm using virtualenv (/root/eb-virt); my Django project is called mediar. All public files are stored in /var/www/html/xxx.com/public_html/

From error log:

[Sun Oct 16 18:40:11.737747 2016] [:error] [pid 27659] [client 128.75.240.205:60486] mod_wsgi (pid=27659): Target WSGI script '/var/www/html/xxx.com/django.wsgi' cannot be loaded as Python module.
[Sun Oct 16 18:40:11.737808 2016] [:error] [pid 27659] [client 128.75.240.205:60486] mod_wsgi (pid=27659): Exception occurred processing WSGI script '/var/www/html/xxx.com/django.wsgi'.
[Sun Oct 16 18:40:11.745983 2016] [:error] [pid 27659] [client 128.75.240.205:60486] Traceback (most recent call last):
[Sun Oct 16 18:40:11.746153 2016] [:error] [pid 27659] [client 128.75.240.205:60486]   File "/var/www/html/xxx.com/django.wsgi", line 9, in <module>
[Sun Oct 16 18:40:11.746165 2016] [:error] [pid 27659] [client 128.75.240.205:60486]     import django.core.handlers.wsgi
[Sun Oct 16 18:40:11.746191 2016] [:error] [pid 27659] [client 128.75.240.205:60486] ImportError: No module named 'django'

My apache2.conf:

Mutex file:${APACHE_LOCK_DIR} default

PidFile ${APACHE_PID_FILE}

Timeout 300

KeepAlive Off

MaxKeepAliveRequests 100

KeepAliveTimeout 5

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

#<Directory /srv/>
#   Options Indexes FollowSymLinks
#   AllowOverride None
#   Require all granted
#</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

<IfModule mpm_prefork_module>
    StartServers 4
    MinSpareServers 20
    MaxSpareServers 40
    MaxClients 200
    MaxRequestsPerChild 4500
</IfModule>


LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

WSGIScriptAlias / /var/www/html/xxx.com/public_html/mediar/mediar/wsgi.py
WSGIPythonPath /var/www/html/xxx.com/public_html:/root/eb-virt/lib/python3.4/site-packages/

<Directory /var/www/html/xxx.com/public_html/mediar/mediar/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

My xxx.com.conf:

<VirtualHost *:80>
  ServerAdmin xxx@xxx.com
  ServerName  xxx.com
  ServerAlias www.xxx.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/xxx.com/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/xxx.com/log/error.log
  CustomLog /var/www/html/xxx.com/log/access.log combined
WSGIScriptAlias / /var/www/html/xxx.com/django.wsgi
</VirtualHost>

And, finally, my django.wsgi:

#!/usr/bin/python 
import os, sys 

PROJECT_ROOT = '/var/www/html/xxx.com/' 
sys.path.append(PROJECT_ROOT) 

os.environ['DJANGO_SETTINGS_MODULE'] = 'mediar.settings' 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler()

I've tried

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3

, it didn't help.

What should I do?

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Sophie Panic
  • 23
  • 1
  • 3
  • maybe in `django.wsgi` use `print(sys.version, sys.__file__)` to see which Python is used by server. Probably it doesn't use `virtualenv` – furas Oct 16 '16 at 19:07
  • The ``/root`` directory generally can only be read by the ``root`` user. The user that Apache runs as would not be able to see inside of that directory. Do not put application code or virtual environments under ``/root``. – Graham Dumpleton Oct 16 '16 at 20:47
  • so did you solve the problem? i'm facing similar issue – chrizonline Nov 09 '16 at 18:21

1 Answers1

0

I would first try using the right python shebang in your wsgi file. Shouldn’t it be something like the following?

#!/root/eb-virt/bin/python

And then the second thing would be to explicitly add the virtualenv to the sys.path append in the wsgi file

sys.path.append('/root/eb-virt/lib/python3.4/site-packages')
2ps
  • 15,099
  • 2
  • 27
  • 47