Stuck with deploying django with apache + mod_wsgi - django

I get an 500 internal server error and in the log files it writes:
[Thu Jun 14 16:30:22 2012] [error] [client 127.0.0.1] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
here is my httpd.conf:
ServerName localhost
<VirtualHost *:80>
ServerAdmin ttt#mysite.com
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/mysite/
LogLevel warn
WSGIDaemonProcess processes=2 maximum-requests=500 threads=1
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /media /var/www/mysite/mysite/static/media/
</VirtualHost>
wsgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

This problem is covered in both the mod-wsgi documentation http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango and the Django deployment documentation https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/ which note that the project must be on your Python path. You can use the WSGIPythonPath directive or set the python-path in your WSGIDaemonProcess directive from the Django documentation. Or you can add it to the sys.path in your wsgi file as the mod-wsgi docs state.

Related

Django + Apache(httpd): Error loading MySQLdb module

I have tried to figure this out for a day now and cannot seem to make any headway.
I'm getting the following Apache error:
[wsgi:error] django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
[wsgi:error] Did you install mysqlclient?
[wsgi:error] [remote 10.10.10.90:35990] mod_wsgi (pid=14165): Target WSGI script '/var/www/vhosts/project_vision/web/web/wsgi.py' does not contain WSGI application 'application'.
pip3 freeze states mysql-connector and mysql-connector-python is installed. The mysql-connector-python versions matches mysql-community rpm's installed.
File structure is this (web is the name of the Django project within the larger Project_vision project):
/var/www/vhosts/project_vision
|- venv/
|- web/
|- static
|- templates
|- vision_web
|- models/
|- migrations/
|- ...
|- web
|- settings.py
|- wsgi.py
|- ...
/var/www/vhosts/project_vision/web/web/wsgi.py
import os
import signal
import sys
import time
import traceback
from django.core.wsgi import get_wsgi_application
sys.path.append('/var/www/vhosts/project_vision')
sys.path.append('/var/www/vhosts/project_vision/web')
sys.path.append('/var/www/vhosts/project_vision/venv')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'web.settings')
try:
application = get_wsgi_application()
except Exception:
# Error loading applications
if 'mod_wsgi' in sys.modules:
traceback.print_exc()
os.kill(os.getpid(), signal.SIGINT)
time.sleep(2.5)
/etc/httpd/conf.d/vision.conf
<VirtualHost *:80>
ServerName my_project_vision.com
DocumentRoot /var/www/vhosts/project_vision/web
Alias /static/ /var/www/vhosts/project_vision/web/static/
<Directory /var/www/vhosts/project_vision/web/static>
Require all granted
</Directory>
WSGIDaemonProcess my_project_vision.com \
processes=2 threads=15 display-name=%{GROUP} \
python-home=/var/www/vhosts/project_vision/venv \
python-path=/var/www/vhosts/project_vision/web
WSGIProcessGroup my_project_vision.com
WSGIApplicationGroup %{GLOBAL}
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/project_vision/web/web/wsgi.py
<Directory /var/www/vhosts/project_vision>
AllowOverride all
Require all granted
Options FollowSymlinks
</Directory>
</VirtualHost>
Where in the world am I going wrong? It must be something minor but important...

WSGI Django 403 Forbidden

When I run
sudo service apache2 restart
I get an error
unable to resolve host hostname
When I look at the error.txt it says it's my WSGI configuration.
Here is my WSGI script.
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
sys.executable = '/usr/lib/python2.7/dist-packages'
application = WSGIHandler()
django.setup()
EDIT1:
Here is my apache2 file. I did change the WSGI file to /user/app/app_settings/wsgi.py
<VirtualHost website:80>
ServerName website.com
ServerAlias www.website.com
ServerAdmin email#website.com
DocumentRoot /home/user/app/static
WSGIScriptAlias / /home/user/app/app_settings/wsgi.py
<Directory /home/user/app/>
Order allow,deny
Allow from all
Require all granted
</Directory>
#Alias /robots.txt /var/www/user/app/robots.txt
#Alias /favicon.ico /var/www/user/app/favicon.ico
#Alias /images /home/user/app/static/images
#Alias /static /home/user/app/static/
ErrorLog /var/www/website/error.log
#CustomLog /var/www/website/access.log combined
</VirtualHost>

Unable to run my Django Site (Version 1.6) on Apache Server

I am running a site that was previously running on django 1.4, but now I shifted it on django 1.6.
I am able to run my site on both version using python manage.py shell.
I am running it nicely on Apache when i keep django 1.4 virtual environment python path in Apache site-enabled, but it does not work when i keep django 1.6 virtual environment python path there.
For django 1.6 it throws error:
ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
What can be problem? Any changes required in wsgi.py file for django 1.6? Please help.
my virtualhost file is :
<VirtualHost *:80>
ServerAdmin vidya.sagar0911#gmail.com
DocumentRoot /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/
ServerName www.vidblog.com
<Directory /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo>
#Order deny,allow
#Allow from all
Options FollowSymLinks
AllowOverride None
</Directory>
Alias /media /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/vidyamedia/
Alias /static /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/static/
WSGIDaemonProcess www.demo.com user=www-data group=www-data processes=8 threads=75\
python-path=/home/vidya/workspace/djnago1.6/lib/python2.7/site-packages
WSGIProcessGroup www.vidblog.com
WSGIScriptAlias / /home/vidya/workspace/vidya/vidya_rose/cms/trunk/demo/demo/wsgi.py
</VirtualHost>
This is the issue when you are migrating your old projects from django-1.x(<1.6) to django-1.6 you need to define DJANGO_SETTINGS_MODULE before you call get_wsgi_application
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

django 1.4 with apache virtualhost - path problems

I'm trying to upgrade my application from django 1.2 to 1.4, which I have tested successfully with the inbuilt webserver.
However, I am having problems with deploying it as a VirtualHost with Apache (on Ubuntu).
my sites-available/default contains:
<VirtualHost *:80>
ServerName myapplication
WSGIScriptAlias / /usr/share/myapplication/wsgi.py
WSGIDaemonProcess myapplication python-path=/usr/share/myapplication:/usr/lib/python2.6/dist-packages
<Directory /usr/share/myapplication>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
the file /usr/share/myapplication/wsgi.py contains the standard:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapplication.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Apache will start ok, but when I go to http://myapplication I get '500: Internal Server Error' and the apache logs show:
ImportError: Could not import settings 'WCReporter.settings' (Is it on sys.path?): No module named WCReporter.settings
Am I using WSGIDaemonProcess correctly? The django docs aren't clear.
Thanks
Solved this with the following, from http://rc98.net/django_wsgi.
sites-available/default:
<VirtualHost *:80>
ServerName myapplication
WSGIDaemonProcess myapplication
WSGIProcessGroup myapplication
WSGIScriptAlias / /usr/share/myapplication/wsgi.py
<Directory /usr/share/myapplication>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
and wsgi.py:
import os,sys
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path not in sys.path:
sys.path.append(path)
path = '/usr/share/myapplication'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapplication.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Could you print your path at runtime:
#init.py
import sys
print sys.path
have you got an init file in the project base dir?
does it work if you import settings instead of myapp.settings?

Problem with Django setup using mod_wsgi and apache on centos 5.4

I am trying to setup apache with mod_wsgi to serve Django through a nginx proxy on CentOS 5.4. I want to start by configuring Apache with Wsgi to serve on a local port, but I get a 403 error when I run curl localhost:8080
# Httpd config:
Listen 127.0.0.1:8080
NameVirtualHost 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
ServerName staging.example.com
LogLevel warn
ErrorLog /home/django/project_name/log/apache_error.log
CustomLog /home/django/project_name/log/apache_access.log combined
DocumentRoot /home/django/project_name/django_project_dir/
WSGIDaemonProcess staging.example.com user=apache group=apache threads=25
WSGIProcessGroup staging.example.com
WSGIScriptAlias / /home/django/project_name/django_project_dir/django.wsgi
<Directory /home/django/project_name/django_project_dir>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
And my wsgi script:
#/home/django/project_name/django_project_dir/django.wsgi
ALLDIRS = ['/home/django/project_dir/virtualenv/lib/python2.4/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
for directory in ALLDIRS:
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
sys.path.append('/home/django/project_name/')
os.environ['PYTHON_EGG_CACHE'] = '/home/django/.python-eggs'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
What is the problem with this configuration?