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

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()

Related

404 error when deploying django project with apache2

I tried to create a new django project on apache2, but I got 404 error.
Here are what I did.
-In /home/ubuntu/django directory, type
django-admin startproject proj
-Edit proj/proj/wsgi.py as follows.
import os
import sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
sys.path.append('/home/ubuntu/django/proj');
sys.path.append('/home/ubuntu/django/proj/proj');
application = get_wsgi_application()
-Edit ALLOWED_HOSTS in proj/proj/settings.py as follows.
ALLOWED_HOSTS = ['example.com']
-Edit /etc/apache2/conf-available/wsgi.conf as follows.
WSGIScriptAlias /proj /home/ubuntu/django/proj/proj/wsgi.py
WSGIPythonPath /home/ubuntu/django/proj
<Directory /home/ubuntu>
Require all granted
</Directory>
<Directory /home/ubuntu/proj/proj>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
-Restart apache2 as
/etc/init.d/apache2 restart
-Access to http://example.com/proj, and then got 404 error as follows.
Page not found (404)
Request Method: GET
Request URL: http://example.com/proj/
Using the URLconf defined in proj.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn't match any of these.
My enviroment is as follows.
Ubuntu 18.04 LTS (AWS)
Apache/2.4.29
Python 3.6.9
Django 3.0.5
How can I solve this 404 error?
Do you have '/admin' in your urls.py file?
You may also need to change the apache2 settings from /home/ubuntu/proj/proj to /home/ubuntu/django/proj/proj

ImportError: No module named site deploying Django with mod-wsgi and httpd

I'm trying to deploy a django project on CentOS 7, but I keep getting ImportError: No module named site on my httpd error log.
This is my config file for httpd located at /etc/httpd/conf.d:
<VirtualHost *:8008>
WSGIProcessGroup programa_registos
WSGIDaemonProcess programa_registos python-home=/var/www/html/programa_registos/.env python-path=/var/www/html/programa_registos
Alias /static /var/www/html/programa_registos/static
<Directory /var/www/html/programa_registos/static>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/programa_registos/programa_registos/wsgi.py
<Directory /var/www/html/programa_registos/programa_registos>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
My project is located at /var/www/html/programa_registos and my virtualenv at /var/www/html/programa_registos/.env.
Here is my wsgi.py file, located at /var/www/html/programa_registos/programa_registos:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "programa_registos.settings")
application = get_wsgi_application()

Stuck with deploying django with apache + mod_wsgi

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.

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?