Ok, after 5-6 hours of trying, I give up. I have searched the web, tried all solutions suggested, but nothing is solving my problem.
Goal: Set up Django on my Ubuntu 12.04 VPS.
Problem: Exception occurred processing WSGI script [...] ImportError: No module named django.core.handlers.wsgi in /etc/log/apache2/error.log.
Solutions tried: 1) Appending the site-packages directory to the sys path in Djangos' wsgi.py file, 2) re-installing mod_wsgi, 3) making sure mod_wsgi is compiled for the same Python version as Django is installed with, 4) chmod 777 for the site-packages directory.
Environment: Ubuntu 12.04 VPS, Django installation in virtualenv, Python version 2.7.3, Django version 1.6.1, mod_wsgi built from mod_wsgi-3.4.tar.gz.
Full error message:
mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
mod_wsgi (pid=23691): Target WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
Conf file from sites-available:
<VirtualHost *:80>
ServerAdmin admin#mysite.com
ServerName mysite.com
DocumentRoot /var/www/mysite.com/djangoSite
WSGIDaemonProcess djangoSite python-path=/var/www/mysite.com/djangoSite:~/Envs/myEnv/lib/python2.7/site-packages
WSGIProcessGroup djangoSite
WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/
<Directory /var/www/mysite.com/djangoSite/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/mysite.com/djangoSite/djangoSite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
wsgi.py:
import os
import sys
sys.path.append('/var/www/mysite.com/djangoSite/')
sys.path.append('/var/www/mysite.com/djangoSite/djangoSite/')
activate_this = '/root/Envs/myenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoSite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Site structure
/var/www/
-- mysite.com
-- djangoSite
-- manage.py
-- djangoSite
-- settings.py, wsgi.py etc.
In your wsgi.py, try adding this to activate virtual env:
import os
import sys
sys.path.append('/Path_To/Virtual_Env/Project_Dir/')
#This is important if multiple apps are running (instead of setdefault)
os.environ["DJANGO_SETTINGS_MODULE"] = "app_name.settings"
activate_this = '/Path_To/Virtual_Env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
In you apache config, you mainly need only(No Deamon Process required):
WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/
See from the Create Virtual Host section of this link
Make sure you are using absolute paths in your Apache2 config file. I assume you have been able to run python manage.py shell to get a working Django shell? I would tackle the Apache2 config file; I'm sure your issue is there.
~/Envs/myEnv/lib/python2.7/site-packages
I think that's your problem right there: the ~ is, if it is expanded at all, expanded to the home of the www-data user. You can validate that by doing something like this:
import sys
print >> sys.stderr, sys.path
in your wsgi file; then check your error log. If it doesn't end up in the main error log, add an ErrorLog to your vhost to get per-vhost error logging.
Also, /root/ is not readable for the www-data user, you need to move your virtual env somewhere accessible.
Related
I'm trying to deploy a django app in an Apache Server (Wamp) using a virtual environtment, but getting that error. Everything is going well, the problem seems to be happen in the wsgi.py file.
The wsgi.py never start the venv so this never start the app.
Here is my httpd-vhost.conf:
ServerName my.app.name
ServerAdmin myadminname#localhost.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIPassAuthorization On
Alias /static C:/wamp/apache2/htdocs/<myappname>/frontend/build/static/
<Directory "C:/wamp/apache2/htdocs/<myappname>/frontend/build/static/">
Allow from all
Require all granted
</Directory>
<Directory "C:/wamp/apache2/htdocs/<myappname>/<mysetting's django folder>">
<Files wsgi.py>
Allow from all
Require all granted
</Files>
</Directory>
#WSGIDaemonProcess <my.app.group> python-path="C:/wamp/apache2/htdocs/<app.name>/env/Lib/site-packages"
#WSGIProcessGroup <my.app.group>
WSGIScriptAlias / "C:/wamp/apache2/htdocs/<app.name>/<settings folder>/wsgi.py"
</VirtualHost>
Here is my wsgi.py file:
import os
import sys
# Add the virtual environment path to the system path
sys.path.append('C:/wamp/apache2/htdocs/<app.name>/env/Lib/site-packages')
# activate_this = 'C:/wamp/apache2/htdocs/<app.name>/env/Scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
# exec(open(activate_this).read(),dict(__file__=activate_this))
# Activate the virtual environment
activate_env = 'C:/wamp/apache2/htdocs/<app.name>/env/Scripts/python'
exec(open(activate_env, 'rb').read(), {'__file__': activate_env})
# Set the DJANGO_SETTINGS_MODULE environment variable
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
# Import the Django application from the Django project
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
In the wsgi.py file there are two ways I found for activate the venv. The venv don't have the activate_this.py file but there was an answer I found answer-here that say you simply copying it from the virtualenv package solve the problem. I tried and worked (in Windows 10). But then I tried in a lower Windows version and got that error. Then I found the other solution without the activate_this.py file but still don't work.
I tried to set up a Django app with Apache and mod_wsgi, but ran into a problem that I have no ideas where is the cause. The app works fine with the command "python manage.py runserver", but when I tried to run it with Apache, I got the following errors in the Apache error log file.
Current thread 0x00007fb4880ad940 (most recent call first):
<no Python frame>
Python path configuration:
PYTHONHOME = '/data/anaconda3/envs/partsdb'
PYTHONPATH = (not set)
program name = 'python3'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = '/usr/bin/python3'
sys.base_prefix = '/data/anaconda3/envs/partsdb'
sys.base_exec_prefix = '/data/anaconda3/envs/partsdb'
sys.platlibdir = 'lib64'
sys.executable = '/usr/bin/python3'
sys.prefix = '/data/anaconda3/envs/partsdb'
sys.exec_prefix = '/data/anaconda3/envs/partsdb'
sys.path = [
'/data/anaconda3/envs/partsdb/lib64/python38.zip',
'/data/anaconda3/envs/partsdb/lib64/python3.8',
'/data/anaconda3/envs/partsdb/lib64/python3.8/lib-dynload',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
I have the following lines in an Apache conf file.
WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
WSGIScriptAlias / /data/partsdb/partsdb/wsgi.py
<Directory "/data/partsdb/partsdb">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
I also replaced the following two lines in the Apache conf file
WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
with the following two lines, but got the same errors.
WSGIDaemonProcess partsdb python-path=/data/partsdb/partsdb python-home=/data/anaconda3/envs/hla3db_venv
WSGIProcessGroup partsdb
The file /data/partsdb/partsdb/wsgi.py just contains the following lines of codes.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'partsdb.settings')
application = get_wsgi_application()
Upon a brief debugging, I found out that the errors were from this line in wsgi.py.
from django.core.wsgi import get_wsgi_application
My machine's OS is redhat 8, and the Apache version is 2.4.37. Thanks for any info/hints.
Try the following code:
WSGIDaemonProcess partsdb python-path=/data/partsdb/partsdb python-home=/data/anaconda3/envs/hla3db_venv
WSGIProcessGroup partsdb
WSGIApplicationGroup %{GLOBAL}
WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
WSGIScriptAlias / /data/partsdb/partsdb/wsgi.py
<Directory "/data/partsdb/partsdb">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static /path/to/directory/defined/as/STATIC_ROOT/
# The first part is defined by STATIC_URL in settings.py
<Directory /path/to/directory/defined/as/STATIC_ROOT>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName mysite.app
ServerAdmin webmaster#localhost
# You do not need to specify DocumentRoot
# I have seen advice not to do so
ErrorLog ${APACHE_LOG_DIR}error.log
CustomLog ${APACE_LOG_DIR}access.log combined
</VirtualHost>
Please note I used this for a single app running in daemon mode as suggested in the documentation, on a Ubuntu server. Check the permissions on the folders, in my case I had to change permissions to allow apache to read and write to the folders the site was in.
You can also look at the answer Graham Dumpleton gave a few years back here:
Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'
Ubuntu16(x64) + Python3.6(installed by anaconda) + Django1.11.3(installed by conda) + Apache2.4.18.
With this configuration__ect/apache2/site-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mblog
WSGIDaemonProcess mblog python-path=/var/www/mblog
WSGIProcessGroup mblog
WSGIScriptAlias / /var/www/mblog/mblog/wsgi.py
<Directory /var/www/mblog/mblog>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
</VirtualHost>
and the apache www directory for python project as below:
enter code here/var/www/mblog/(755)
|-- manage.py(755)
|
|-- mblog(dir 755)
| `|-- wsgi.py(644)
| `-- urls.py(644)
|-- templates(dir)
| `-- *.html(644)
The wsgi.py is configured as below (the files is granted):
import os, sys
sys.path.append('/var/www/mblog/mblog')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mblog.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
After all of these config, the browser display:"500 Internal Server Error"
So I look for the error of /var/log/apache2/error.log as below:
File "/var/www/mblog/mblog/wsgi.py", line 31, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
I am confused about all of the explain of website explanation but all of these cannot solve this question.
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/
Could you please give me a hand to solve this problem?
Thank you!
You either haven't told mod_wsgi where the Python virtual environment is that you are using and which Django is installed in, or your mod_wsgi is compiled for a different version of Python than you want to use and which Python is installed in.
Check what mod_wsgi is compiled for by doing what is described in:
http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-installation-in-use
http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-shared-library
For how to set up a Python virtual environment (which is recommended be used), see:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
i have a problem when run django on apache:
htdocs/blog/apps/homepage/urls.py:
url(r'^$', 'index', name="h_index"),
url(r'^about/$', 'about', name="h_about"),
url(r'^contact/$', 'contact', name="h_contact"),
url(r'^archive/$', 'archive', name="h_archive"),
htdocs/blog/urls.py
(r'^', include('apps.homepage.urls')),
django.wsgi:
import os
import os.path
import sys
sys.path.append('D:/Coding/xampp/htdocs')
sys.path.append('D:/Coding/xampp/htdocs/blog')
os.environ['DJANGO_SETTINGS_MODULE'] = 'blog.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
httpd.conf:
Alias /static/ "D:/Coding/xampp/htdocs/blog/static/"
WSGIScriptAlias /blog/ "D:/Coding/xampp/htdocs/blog/django.wsgi"
when i run "localhost/blog", it's working. But run "localhost/blog/about/" or other, it's error:
[error] [client ::1] Target WSGI script not found or unable to stat: .../htdocs/blog/django.wsgiabout, referer: http://localhost/blog/
Please notice how your apache configuration doesn't match the documented syntax for mod_wsgi's WSGIScriptAlias.
WSGIScriptAlias /blog/ "D:/Coding/xampp/htdocs/blog/django.wsgi"
should be:
WSGIScriptAlias /blog "D:/Coding/xampp/htdocs/blog/django.wsgi"
(notice no trailing slash after the 2nd token, "/blog")
I just resolved the same issue just now and found this thread Googling. Hope this helps you and future users.
For more information:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
I would suspect you are not loading the wsgi module. If you look closely at your error message Apache is treating it like a file and adding about to the name of the wsgi script file it looks for - thus it doesn't find it.
I found a similar question here, but didn't help me.
I basically have apache setup on my OS X. I also have Django installed.
However, when i try to load the page through the browser locally, i get:
Forbidden
You don't have permission to access / on this server.
I have an original httpd.conf with the only modification of enabling vhosts in which I have:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/Dropbox/project/www/"
ServerName touch.loc
# ServerAlias www.dummy-host.example.com
ErrorLog "/Dropbox/project/www/log/error.log"
CustomLog "/Dropbox/project/www/log/access.log" common
</VirtualHost>
And the error.log file gives:
[Tue May 03 20:22:56 2011] [error] [client 127.0.0.1] Directory index forbidden by Options directive: /Dropbox/project/www/
I read around and it looks like i have to add the following to the httpd.conf:
<Directory /Dropbox/project/www >
Order deny,allow
Allow from all
</Directory>
In which case i get:
[Tue May 03 20:27:55 2011] [error] [client 127.0.0.1] client denied by server configuration: /Dropbox/project/www/
Can someone help me fix this annoyance? How can I further Investigate it?
Does it have to do with users/groups?
UPDATE:
I then added the Options +Indexes and the permissions opened. However when i try to load the page, file structure appears instead of the wsgi file to pickup and load the website. What would be the reason for that?
here is my .wsgi file:
import os
import sys
sys.stdout = sys.stderr
# Add the virtual Python environment site-packages directory to the path
import site
site.addsitedir('/usr/lib/python2.6/dist-packages')
#If your project is not on your PYTHONPATH by default you can add the following
sys.path.append('/Dropbox/project/www/')
sys.path.append('/Dropbox/project/www/project')
# Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages
os.environ['PYTHON_EGG_CACHE'] = '/Dropbox/project/www/mod_wsgi/egg-cache'
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
You seem to be missing anything in your Apache configuration that actually serves the Django application. You need to read the Django mod_wsgi documentation - the first line of code there is what you're missing.
Note that your code should not live under the DocumentRoot.
The problem I see is in Dropbox folder
I have made so:
chmod o+x /home/your_name/Dropbox
That resolves permissions