Django WGSI paths - python

I am having problems setting up wgsi with django. I'm following this http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/ . Yet I am still really confused as to where to put the .wsgi file and if I need to set the sys.path. I have tried it both directly outside and inside the web root and I can't get anything to work as expected.
# /home/ben/public_html/django_test/testproject/apache/django.wsgi:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Relivant apache conf:
DocumentRoot "/home/ben/public_html/django_test/testproject/"
WSGIScriptAlias / "/home/ben/public_html/django_test/testproject/apache/django.wsgi"
Apache Logs Error (standard apache 500 page):
ImportError: Could not import settings 'testproject.settings' (Is it on sys.path? ...
I can at get django to at least throw an error of it's own by using this:
import os
import sys
path = '/home/ben/public_html/django_test/testproject'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
which resulted in this django error page:
ImportError at /admin/
No module named testproject.urls

I put the wsgi at same level than settings.py, and looks like this:
import os
import sys
sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourprojectname.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
this is the apache conf file:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName www.yourprojectname.com
Alias /media/ /home/diegueus9/workspace/yourprojectname/media/
<Directory /home/diegueus9/workspace/yourprojectname/media/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptReloading On
WSGIDaemonProcess yourprojectname
WSGIProcessGroup yourprojectname
WSGIApplicationGroup yourprojectname
WSGIPassAuthorization On
WSGIScriptAlias / /home/diegueus9/workspace/yourprojectname/yourfile.wsgi
ErrorLog /var/log/apache2/yourprojectname-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/yourprojectname-access.log combined
</VirtualHost>

As a quick fix you can, next to adding this to sys.path:
path = '/home/ben/public_html/django_test/testproject'
also add this:
path2 = '/home/ben/public_html/django_test'
You can also change Python path in your Apache VirtualHost:
WSGIDaemonProcess [...] python-path=/home/ben/public_html/django_test
But you should also review mod_wsgi docs and learn what Graham Dumpleton advises there.

Here is a nice tutorial that shows you how to use mod_wsgi with Django and also some other items that are not related to this question.

I just spent a few hours troubleshooting this very issue and my fix was SELinux was prohibiting access to my /django/ folder.
To check if SEStatus is blocking your access, view the log at /var/log/audit/audit.log
My fix was a simple:
restorecon -R /var/www/django/

Related

408 Request Timeout Using Apache + mod_wsgi + Python Django in Windows

I have a Django app which is deployed in local network using Apache + mod_wsgi under Windows. When I run python manage.py runserver, everything works fine. But when I start the Apache Service, I cannot access the app. The only response I get from the access.log is the error code 408. Below is my httpd.conf:
LoadFile "c:/users/felix/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/felix/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/felix/appdata/local/programs/python/python37"
ServerName localhost
WSGIScriptAlias / "D:/dev/test_server/django/django/wsgi_windows.py"
Listen 8000
<VirtualHost *:8000>
WSGIPassAuthorization On
ErrorLog "logs/django.error.log"
CustomLog "logs/django.access.log" combined
Alias /static "D:/dev/test_server/staticfiles"
<Directory "D:/dev/test_server/staticfiles">
Require all granted
</Directory>
<Directory "D:/dev/test_server/django/django">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
And below is the wsgi_windows.py file:
# Activate the virtualenv
activate_this = 'D:/dev/test_server/.venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))
import os # noqa
import sys # noqa
import site # noqa
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('D:/dev/test_server/.venv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('D:/dev/test_server/django')
sys.path.append('D:/dev/test_server/django/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'django.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django.settings")
from django.core.wsgi import get_wsgi_application # noqa
application = get_wsgi_application()
I'd appreciate any ideas or hints on the issue.
start apache at command line with 'httpd' and look for error messages. If Apache has a problem at startup there is no message in the log files.
check error.log
You can place 'print('xyz') even in the settings.py and elsewhere and this way by checking error.log see how your app is setup and how far a request is processed. If your app get stuck somewhere like this you find the code where it is stuck
I have the same error after install scipy library and use it in some scrip in django. I found that some libraries like "numpy" and "scipy" only work in the Python main interpreter and you have to force the WSGI to run in the global app group to run it. Adding this line in my conf file work for me.
WSGIApplicationGroup %{GLOBAL}

Configuring mod_wsgi for wamp server on windows

I am new in Django development. I have created an Django application and tested in development server i.e. 127.0.0.1:8080/mysite. Then I decided to run this app on Apache server 2.4.9.
As all we know the best option is configuring mod_wsgi.
My problem is Apache server never runs after configuring as bellow:
Keep mod_wsgi.so
downloaded on 'C:\wamp\bin\apache\apache2.4.9\modules\'.
Insert "LoadModule wsgi_module modules/mod_wsgi.so" to httpd.conf
Restart wamp server
I am using 32 bit of Python,Apache and mod_wsgi. Python is installed for all user. Please help me -
First of all copy mod_wgi in modules folder,
then add following to httpd.conf modules list:
LoadModule wsgi_module modules/mod_wsgi.so
attention that naming is important (should suffix with _module)
Add the following to your httpd.conf
Include path_to_your_proj/django_wsgi.conf
django_wsgi.conf file:
WSGIScriptAlias path_to/django.wsgi
<Directory project_path>
Allow from all
Order allow,deny
</Directory>
Alias /static path_to_static_files
django.wsgi file:
import os
import sys
#Calculate the path based on the location of the WSGI script.
CURRENT_DIR = os.path.dirname(__file__).replace('\\','/')
PROJECT_ROOT = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir))
SETTINGS_DIR = os.path.join(PROJECT_ROOT,'proj_dir_name')
if PROJECT_ROOT not in sys.path:
sys.path.append(PROJECT_ROOT)
if SETTINGS_DIR not in sys.path:
sys.path.append(SETTINGS_DIR)
os.chdir(SETTINGS_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'proj_name.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If you're using a virtualenv (that I suggest) do as follows:
httpd.conf file:
# virtual env02
<VirtualHost ip-adddress_or_dns:port_if_other_than_80>
ServerName just_a_name # like : app01
ServerAlias just_an_alias # like : app01.mydomain.com
ErrorLog "logs/env02.error.log"
CustomLog "logs/env02.access.log" combined
WSGIScriptAlias / direct_path_to_you_wsgi_with_forward_slashes # like: C:/virtual/venv01/proj_name/wsgi.py
<Directory proj_dir_with_forward_slashed>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static path_to_static_folder_with_forward_slashes
<Directory path_to_static_folder_with_forward_slashes>
Require all granted
</Directory>
</VirtualHost>
# end virtual env02
wsgi.py file:
activate_this = 'c:/virtual/env02/scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('c:/Organizer/virtual/env02/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('c:/virtual/proj_name')
sys.path.append('c:/virtual/proj_name/default_app_name')
os.environ['DJANGO_SETTINGS_MODULE'] = 'default_app_name.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "default_app_name.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Running django webapp on apache using mod_wsgi

I am new to working with apache and mod_wsgi. But I do have little experience in django, so from some tutorials I tried to run django app through apache webserver using mod_wsgi.
I created mysite in /var/www/
then in mysite/application I created application.wsgi ...
import os
import sys
sys.path.append('/var/www/mysite/application')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and in /etc/httpd/sites-available I created file named mysite.conf ...
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin id#somewhere.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite/
<Directory /var/www/mysite>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Directory>
WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mysite
</Virtualhost>
Then I ran a2ensite mysite.conf, didn't showed any error.
Then in /etc/httpd/hosts/ I added one line my-ipddress mysite.com
I gave permission chmod 777 to all the above files and to folder /var/www/mysite. Now when I open mysite.com on browser I see apahce's default page nothing from django.
I am using fedora 21.
You haven't put in anything in that configuration to serve your Django site via WSGI: you're missing the WSGIScriptAlias line (see the documentation:
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Note that you shouldn't really be putting things in /var/www; and also you shouldn't need to create your own WSGI file, Django creates one for you when you create a project.

client denied by server configuration

I am trying to setup django project by apache and mod_wsgi file. But I am getting this error client denied by server configuration: /home/ghrix/production . I have google this errro and found lot of solutions but nothing worked for me.
My code are as follows :
production.wsgi
import os
import sys
sys.path = ['/home/ghrix/myproject/'] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
production.conf file :
<VirtualHost *:80>
WSGIScriptAlias / /home/ghrix/production.wsgi
ServerName firstweb.com
ServerAlias firstweb.com
Alias /static/ /home/ghrix/myproject/static/
<Directory /home/ghrix/myproject/ >
Options Indexes FollowSymLinks
WSGIProcessGroup production
WSGIApplicationGroup %{GLOBAL}
Require all denied
</Directory>
</VirtualHost>
I solved the problem by making two changes :
1) Require all denied -> change this to -> Require all granted
2) If you look at the project created by you there is already a wsgi.py file in you project directory (same dir where your settings are placed by default) , So you do not have to create seperate wsgi file as I have created initially. Just point to that wsgi.py in your conf file inside apache2 as
WSGIScriptAlias / /<path to my-project wsgi.py file>
That's it you error is resolved. But still if you are getting one more error after this your settings module is not find then you have to edit your wsgi file and add two lines of code :
import sys
sys.path = ['<path to my project>'] + sys.path
That's it your project will run smoothly now.
So complete wsgi and conf files will look like this :
wsgi.py file :
import os
import sys
sys.path = ['<path to my project>'] + sys.path
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
and conf file :
<VirtualHost *:80>
WSGIScriptAlias / /<path to my project>/wsgi.py
ServerName secondweb.com
ServerAlias secondweb.com
Alias /static/ /<path to my project>/static/
<Directory /<path to my project>/ >
Options Indexes FollowSymLinks
Require all granted
</Directory>
</VirtualHost>

Django site listing only directory structure

I am trying to host a site developed in Django. I did all the configs as per http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/ and also the official Django documentation for deployment. However, when I hit the site, the directory structure is getting displayed but not the actual content.
Please help me as to what could be going wrong. Thanks.
EDIT:
Showing my Apache config as requested.
This is my /etc/apache2/sites-available/SiteName
<VirtualHost *:80>
ServerAdmin XYA
ServerName SiteName
ServerAlias IPAddress
WSGIScriptAlias / /var/www/SiteName/index.wsgi
Alias "/static/css" "absolute path to /static/css"
<Location "/static/css">
SetHandler None
</Location>
...
</VirtualHost>
This is my index.wsgi
import os
import sys
import site
site.addsitedir('path to /.virtualenvs/SiteName/local/lib/python2.7/site-packages')
sys.path.append('path to site_root')
os.environ['DJANGO_SETTINGS_MODULE'] = 'SiteName.settings'
activate_env = os.path.expanduser("path to /bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Categories

Resources