Django site listing only directory structure - python

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

Related

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>

Running two seperate Django site from one apache

I am running Ubuntu 14.04 on my notebook and I am using Django 1.6.4 (with virtualenv) together with Apache 2.4.7.
I would like to access two django projects through my local apache. Therefore, theses projects are called kleyboldt (name of a guy I am writing for) and kleyboldt2. Normally I store my web projects under /home/nick/Workspace/Web. After watching tutorial I ended up with two domains pointing to the same django project. To be sure I rewrote the config and placed my projects under /var/www.
At the beginning I wrote two files in order to configure apache.
/etc/apache/sites-available/kleyboldt.conf
WSGIPythonPath /var/www/kleyboldt:/var/www/kleyboldt/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt/kleyboldt.wsgi
ServerName kleyboldt.com
Alias /static /var/www/kleyboldt/static
<Directory /var/www/kleyboldt/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
/etc/apache2/sites-available/kleyboldt2.conf
WSGIPythonPath /var/www/kleyboldt2:/var/www/kleyboldt2/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt2/kleyboldt2.wsgi
ServerName kleyboldt2.com
Alias /static /var/www/kleyboldt2/static
<Directory /var/www/kleyboldt2/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Afterwards I enabled both sites via a2ensite. I checked the sites-enabled directory and the links are placed correctly. In order to redirect kleyboldt.com and kleyboldt2.com to my local apache I changed my /etc/hosts to:
127.0.0.1 localhost
127.0.1.1 mars (name of my computer)
127.0.0.1 kleyboldt.com
127.0.0.1 kleyboldt2.com
To use WSGI I wrote for each project a seperated wsgi file in the project folder:
/var/www/kleyboldt/kleyboldt.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
/var/www/kleyboldt2/kleyboldt2.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt2')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt2.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I created two independent python environments using virtualenv that are now located under /var/www/kleyboldt/env and /var/www/kleyboldt2/env.
Afterwarsd I got two django test sites when I typed both domains in the URL bar of my browser. To differ I added some settings to the settings.py files and wrote custom views.
/var/www/kleyboldt/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt1")
/var/www/kleyboldt2/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt2")
When I type localhost in my browser then I get kleyboldt2 back. When I type kleyboldt2.com in my browser then I get kleyboldt2 back too. Unfortunately I see kleyboldt2 also after typing kleyboldt.com in my browser.
Maybe the bug is to simple for me to discover :D Does anybody know a possible solution?

Django WGSI paths

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/

Categories

Resources