django deployment issue with mod_wsgi - django

I am trying to deploy my django project which is located at home/doga/headend/ and just to run it on the localhost (will be a LAN accessable project). My main problem is that I can use the site well however the /admin/ folder is giving me Internal Server Error error.
anyway here is my etc/apache2/sites-available/default file
<VirtualHost *:80>
ServerName /
ServerAlias */
DocumentRoot /home/doga/headend/
LogLevel warn
WSGIScriptAlias / /home/doga/headend/apache/django.wsgi
Alias /media /home/doga/headend/media/statics
Alias /admin_media /usr/lib/python2.4/site-packages/django/contrib/admin/media
</VirtualHost>
and here is my home/doga/headend/apache/django.wsgi file
import os, sys
import django.core.handlers.wsgi
sys.path.append('/home/doga/')
sys.path.append('/home/doga/headend')
os.environ['DJANGO_SETTINGS_MODULE'] = 'headend.settings'
application = django.core.handlers.wsgi.WSGIHandler()
lastly my main url.py
from django.conf.urls.defaults import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^headend/', include('headend.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^table/(?P<pid>.*)/$', 'main.views.table_view'),
(r'^graph/(?P<pid>.*)/$', 'main.views.graph_view'),
(r'^graph/$', 'main.views.platform_graph_view'),
(r'^table/$', 'main.views.platform_view'),
(r'^csv/$', 'main.views.csv_view'),
(r'^recent/$', 'main.views.recent_view'),
(r'^$', 'main.views.main_view'),
(r'^cs/(?P<number>.*)/$', 'main.views.ch_view'),
#(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
#(r'^$', 'main.views.main_view'),
#(r'^media/(?P<path>.*)$', 'django.views.static.serve',
# {'document_root': '/home/uluc/headendmedia/statics'}),
)

I don't believe you should be setting the DocumentRoot to /home/doga/headend. Won't this give access to all your source code?
What detail does the Apache log give for the Internal Server Error?

Related

Django-cms root not found on production using i18n

Currently on django-cms 2.4.2. Encountered a peculiar problem, when on local machine where DEBUG=True, visiting 127.0.0.1/ will redirect to 127.0.0.1/en/. When running on a production server (virtualenvs, apache), visiting mysite.com/ will raise a 404 but display the home page behind while mysite.com/en/ displays the home page. Been trying to find the root of the problem but can't seem to figure out if it's in the urls, wsgi or settings.
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin_tools/', include('admin_tools.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(r'^accounts/', include('allauth.urls')),
... apps ...
url(r'^', include('cms.urls')),
)
# Static files
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
else:
from django.conf.urls.defaults import *
handler500 = 'error_pages.views.server_error'
handler404 = 'error_pages.views.custom_404'
handler403 = 'error_pages.views.permission_denied'
handler400 = 'error_pages.views.bad_request'
I have django.middleware.locale.LocaleMiddleware and cms.middleware.language.LanguageCookieMiddleware added in settings.
In my Apache httpd.conf:
NameVirtualHost *:27567
# Begin site configuration
<VirtualHost *:27567>
WSGIScriptAlias / /path/to/wsgi.$
ErrorLog "/path/to/log/error_path.log"
</VirtualHost>
Tried logging any errors that django might encounter and only received:
WARNING 2013-09-18 14:04:34,380 Not Found: /
Edit:
Seems like when Debug=True, the problem goes away. I have set allowed_hosts settings to 'localhost' and 'domain.com'. Have checked sequence of middleware loaded, i18n is set to true etc but still can't pinpoint where the issue lies.
Edit 2:
Just found the problem. It lies in the error pages where the views for error pages are:
from django.shortcuts import render
def server_error(request):
return render(request, '500.html')
Most likely a clash between django-cms' urls.

Once I activate admin, Django under mod_wsgi can't find other URLs

I'm trying to do the Django tutorial. Part 1 went OK (i.stack.imgur.com/US5GN.jpg)
However, once I activate the admin site the root I get (i.stack.imgur.com/EXfE4.jpg). Edit (as S.O. doesn't yet allow me to post images; thanks, cberner):
Page not found (404)
Request Method: GET
Request URL: http://dj1.net/
Using the URLconf defined in dj1_net.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, , didn't match any of these.
dj1.net is my test domain (editing /etc/hosts), and while dj1.net/admin/ now works as expected, I don't know why dj1.net/ throws this error once I uncomment (as instructed) the line
url(r'^admin/', include(admin.site.urls)),
under mysite/urls.py (in my case dj1_net/urls.py). It seems to me everything should work as before unless the GET request is against /admin/.
My stack: a Debian 6.0 VPS with Apache and mod_wsgi. I want to avoid Django's built-in server: if I can't deploy it for real, I'd rather know earlier rather than later. In fact, for easy PHP support I'm very interested in an Apache-based solution.
For further reference, here is my /etc/apache2/sites-available/dj1.net:
<VirtualHost *:80>
ServerName dj1.net
DocumentRoot /var/www/dj1_net
Alias /static/ /var/www/dj1_net/static/
Alias /media/ /var/www/dj1_net/media/
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIScriptAlias / /var/www/dj1_net/django.wsgi
WSGIProcessGroup dj1_net
WSGIDaemonProcess dj1_net processes=2 threads=16 maximum-requests=1000 display-name=apache-dj1_net-wsgi
</VirtualHost>
Finally, here is /var/www/dj1_net/django.wsgi:
import sys
import os
import os.path
sys.path.append(os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'dj1_net.settings'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
Just to be upfront, I know close to nil about Apache server configuration and WSGI. I do, however, want to make this set-up work before embarking in Django. I followed a mix of the official instructions and this post, since I frankly couldn't make get Django to work applying either instructions verbatim. I'm afraid the bottom of the problem must lie there...
Any tips will be greatly appreciated.
Cheers!
S.P.
Second edit. Here is dj1_net/urls.py (thanks, scytale):
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'dj1_net.views.home', name='home'),
# url(r'^dj1_net/', include('dj1_net.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
You don't have a url defined for the root of your site. You'll need to add one, for example:
url(r'^$', 'dj1_net.views.my_view_function'),
That would go after the line that defines the admin URL.

Moving to production environment broke my application

I've just deployed my application on dreamhost and it's stopped working when uploading files. Passenger just produces a 500 internal error. It works on my development set up.
My passenger file looks like:
import sys, os
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), '/photosoc'))
os.environ['DJANGO_SETTINGS_MODULE'] = "photosoc.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I get the standard 404 error(debug is still enabled). I have had to change my urls file though from:
from django.conf.urls.defaults import patterns, include, url
from competition.models import Image
from competition import *
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
admin.site.register(Image)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photosoc.views.home', name='home'),
# url(r'^photosoc/', include('photosoc.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# url(r'^image/$', 'competition.views.index'),
url(r'^image/uploadImage', 'competition.views.uploadImage'),
url(r'^image/uploadCompleted', 'competition.views.uploadCompleted'),
url(r'^image/uploadFailed', 'competition.views.uploadFailed'),
)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))
To the following:
from django.conf.urls.defaults import patterns, include, url
from competition.models import Image
from competition import *
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
admin.site.register(Image)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'photosoc.views.home', name='home'),
# url(r'^photosoc/', include('photosoc.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# url(r'^image/$', 'competition.views.index'),
url(r'^image/uploadImage', 'competition.views.uploadImage'),
url(r'^image/uploadCompleted', 'competition.views.uploadCompleted'),
url(r'^image/uploadFailed', 'competition.views.uploadFailed'),
)
This is the only change I've made to the application from the switch from development to production. So what else could have gone wrong?
You may have a permission problem.
It seems like you are writing image files. Does the process that is running Django have permission to write files? When you are running Django locally with ./manage.py runserver Django will get the permissions of the user that runs from the command line, but in a typical production environment you'd be running with the permissions of the Apache user. This user may not have permission to write those image files. You could try temporarily hardcoding the write directory to /tmp to see if that makes a difference.
Of course, you will really need see that 500 error stack trace to know what's going on.
Are your settings exactly the same in what middleware classes your importing? Double check that these are the same.
Related:
ModSecurity: Output filter: Failed to read bucket (rc 104): Connection reset by peer

The current URL, , didn't match any of these - using multiple sites

I wish to have multiple django installations. One at / (which is working fine) and one at /adam. The one at slash adam is redirected by apache correctly, until you try and visit an app. E.g. /admin works but /adam/admin does not work. I get the error:
Page not found (404)
Request Method: GET
Request URL: http://[CENSORED]/adam/
Using the URLconf defined in bms.urls, Django tried these URL patterns, in this order:
^admin/doc/
^admin/
The current URL, , didn't match any of these.
Notice the empty commas. The apache virtual host is:
<VirtualHost *:80>
ServerName [CENSORED]
DocumentRoot /home/user/bms
Alias /static/admin/ /usr/local/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/contrib/admin/media/
<Directory /home/user/bms/apache>
Order allow,deny
Allow from all
</Directory>
<Directory /home/ajt1g09/bms/apache>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess bms user=user group=user processes=2 threads=25 python-path=/usr/local/lib/python2.7/site-packages
WSGIProcessGroup bms
WSGIScriptAliasMatch ^/adam(.*) /home/ajt1g09/bms/apache/django.wsgi
WSGIScriptAlias / /home/user/bms/apache/django.wsgi
</VirtualHost>
And the django.wsgi file in ajt1g09/bms/apache:
import os
import sys
path = '/home/ajt1g09/bms'
if path not in sys.path:
sys.path.append(path)
sys.path.append('/usr/local/lib/python2.7/site-packages')
sys.path.append('/home/ajt1g09')
os.environ['DJANGO_SETTINGS_MODULE'] = 'bms.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And finally, the urls.py file in ajt1g09/bms (clearly showing /admin is there):
from django.conf.urls.defaults import
patterns, include, url
#Uncomment the next two lines to enable the admin: from django.contrib
import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'bms.views.home', name='home'),
# url(r'^bms/', include('bms.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)), )
I have no idea what the problem is.
You shouldn't be using:
WSGIScriptAliasMatch ^/adam(.*) /home/ajt1g09/bms/apache/django.wsgi
Just use:
WSGIScriptAlias /adam /home/ajt1g09/bms/apache/django.wsgi
The WSGIScriptAliasMatch will not work as written because you haven't re substituted the matched part from back into last argument. Ie.,
WSGIScriptAliasMatch ^/adam(.*) /home/ajt1g09/bms/apache/django.wsgi$1
You should though simply not be using WSGIScriptAliasMatch. That is for advanced use cases only and requires you be very careful in using it because how you use it can impact what SCRIPT_NAME/PATH_INFO are set to when passed to application and it is those that urls.py matching is based off.

Run Django with URL prefix ("subdirectory") - App works, but URLs broken?

Below are the relevant configuration files, also at http://dpaste.com/97213/ .
The apache config is currently working, because accessing 'example.com/' shows me the index.html file I have placed at the document root.
I'd like to serve Django/apps at the prefix '/d', so 'example.com/d/' would load the default app, 'example.com/d/app3' would load another, as configured in urls.py.
Serving Django, I'm using the suggested mod_wsgi, on Linux.
Currently, I can access the Ticket app at 'example.com/d', but when the #login_required decorator tries to send me to the login page, I get sent to 'example.com/accounts/login', rather than the expected 'example.com/d/accounts/login'.
Since the default app loads correctly, I'm not sure what I'm doing wrong here, or if this is a bug in Django when generating the urls.
Any suggestions?
EDIT:
As a note, if I change the apache config line:
WSGIScriptAlias /d /home/blah/django_projects/Tickets/apache/django.wsgi
to
WSGIScriptAlias / /home/blah/django_projects/Tickets/apache/django.wsgi
The application, commenting, and logging in all work fine. Even going to 'example.com/admin' loads the admin, although I've left the admin media broken, so no stylesheets are loaded.
--- Configs Follow:
#
# /home/blah/django_projects/Ticket/urls.py
#
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('ticket.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^comments/', include('django.contrib.comments.urls')),
)
#
# /home/blah/django_projects/Ticket/apache/django.wsgi
#
import os, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append('/home/blah/django_projects')
sys.path.append('/home/blah/django_projects/Tickets')
os.environ['DJANGO_SETTINGS_MODULE'] = 'Tickets.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
#
# /etc/apache2/sites-available/django_tickets_wsgi (apache conf)
#
NameVirtualHost *
<VirtualHost *>
Alias /media /home/blah/django_projects/Tickets/media
WSGIScriptAlias /d /home/blah/django_projects/Tickets/apache/django.wsgi
WSGIDaemonProcess exmaple_com user=blah group=blah processes=1 threads=10
WSGIProcessGroup example_com
ServerAdmin localhost#example.com
ServerName example.com
DocumentRoot /var/www/
<Directory /var/www/>
Options -Indexes FollowSymLinks -MultiViews -Includes
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature Off
</VirtualHost>
This is a possible duplicate of Django Apache Redirect Problem, as that answer solved this problem.
I only eventually stumbled on that answer by opening almost all of the 'related questions' here, just out of desperation. From my perspective, I think my question has some valuable "search friendly" words.
EDIT: The answer: (via alex vasi)
Things to try:
Change current domain to "yourdomain.tld/cflow" in the "sites" framework. It's easy to do using django admin or dumpdata/loaddata manage.py commands.
Looks like your site is using login_required decorator. In that particular case you can add to settings.py:
LOGIN_URL = '/[prefix]/accounts/login/'
In your urls.py rename urlpatterns to base_urlpatterns; then add the followinig definition at the end of the same file:
urlpatterns = patterns('',
'^', include(base_urlpatterns), # iff you wish to maintain the un-prefixed URL's too
'^your_prefix/', include(base_urlpatterns),
)