django production server: root path - django

I am developing a django application. On the development server, everything works just fine.
On the production server (using apache), nothing is working.
1/ I have the error TemplateDoesNotExist at /.
In my settings.py file:
SITE_ROOT = os.path.abspath(os.path.dirname(__name__)). This is the project root path.
templateDir = os.path.join(SITE_ROOT, 'templates/')
TEMPLATE_DIRS = (
templateDir
)
This is the templates path.
2/ If I change SITE_ROOT with the absolute path of the project:
SITE_ROOT="/var/www/europolix"
Templates seem to be recognize but I have another mistake:
No module named getEurlexIdsFunctions
Here is the code:
import sys
sys.path.append('import')
import getEurlexIdsFunctions as eurlexIds
I think that once again the problem comes from a relative path. Apache seems to search 'import' in "var/www/" and not in "var/www/europolix/". Am I right?
Here is my apache configuration:
WSGIScriptAlias /europolix /var/www/europolix/europolix/wsgi.py
WSGIPythonPath /var/www/europolix/
<Directory /var/www/europolix/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Is it a problem of root path not recognized, or is there another problem?
Many thanks.

Well, a couple of things. When working with settings.py is better to declare all the paths as absolute paths. I see in your code that you have this line
SITE_ROOT = os.path.abspath(os.path.dirname(__name__))
for site's root but I think is better if you use __file__ global variable instead of __name__. Like this:
SITE_ROOT = os.path.abspath(os.path.dirname(__file__))
I have a django app in production server and all I had to add to my httpd.conf about wsgi was the load_module directive and this line inside the virtual host
WSGIScriptAlias / C:/Users/ike/Documents/Work/Sincronoz/code/apache/django.wsgi
specifying the alias to the django.wsgi script as the root.
Then in django.wsgi script I have this code:
import os, sys
sys.path.append(r'<full site root to where is settings.py>')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project_module.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I think you're better off working with absolute paths and when you got it all working then try to accommodate it for your needs with maybe relative path if you have to.
Hope it helps.

in django project in file wsgi.py i add this 3 lines
import sys
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),'..')
sys.path.append(DJANGO_PATH)

Related

Django on apache: Could not find platform dependent libreries <exe_prefix>

I'm trying to deploy a django app in an Apache Server (Wamp) using a virtual environtment, but getting that error. Everything is going well, the problem seems to be happen in the wsgi.py file.
The wsgi.py never start the venv so this never start the app.
Here is my httpd-vhost.conf:
ServerName my.app.name
ServerAdmin myadminname#localhost.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIPassAuthorization On
Alias /static C:/wamp/apache2/htdocs/<myappname>/frontend/build/static/
<Directory "C:/wamp/apache2/htdocs/<myappname>/frontend/build/static/">
Allow from all
Require all granted
</Directory>
<Directory "C:/wamp/apache2/htdocs/<myappname>/<mysetting's django folder>">
<Files wsgi.py>
Allow from all
Require all granted
</Files>
</Directory>
#WSGIDaemonProcess <my.app.group> python-path="C:/wamp/apache2/htdocs/<app.name>/env/Lib/site-packages"
#WSGIProcessGroup <my.app.group>
WSGIScriptAlias / "C:/wamp/apache2/htdocs/<app.name>/<settings folder>/wsgi.py"
</VirtualHost>
Here is my wsgi.py file:
import os
import sys
# Add the virtual environment path to the system path
sys.path.append('C:/wamp/apache2/htdocs/<app.name>/env/Lib/site-packages')
# activate_this = 'C:/wamp/apache2/htdocs/<app.name>/env/Scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
# exec(open(activate_this).read(),dict(__file__=activate_this))
# Activate the virtual environment
activate_env = 'C:/wamp/apache2/htdocs/<app.name>/env/Scripts/python'
exec(open(activate_env, 'rb').read(), {'__file__': activate_env})
# Set the DJANGO_SETTINGS_MODULE environment variable
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
# Import the Django application from the Django project
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
In the wsgi.py file there are two ways I found for activate the venv. The venv don't have the activate_this.py file but there was an answer I found answer-here that say you simply copying it from the virtualenv package solve the problem. I tried and worked (in Windows 10). But then I tried in a lower Windows version and got that error. Then I found the other solution without the activate_this.py file but still don't work.

Permissions error when accessing Django admin page served by Apache

I am new to Django development and this site has been invaluable so far and I'm impressed with how much I've learned. That being said, I am running into a problem and I've been crawling through the related posts on this site yet can't seem to find anything that solves my issue.
Here is what I've tried but no luck.
So I am hoping that one of you might be able to take a look at what I'm doing and point me in the right direction. I am trying to get my Apache web server to serve up the CSS files for Django's admin page. But, when visiting: http://localhost/admin/, I get the error: Forbidden. You don't have permission to access /admin/ on this server.
The CSS/JS for my site is located in mysite/static and the CSS/JS for the admin page is located in the Django installation folder, Django-1.5/django/contrib/admin/static/admin. Here are the relevant bits in my settings.py file:
STATIC_ROOT = '/home/me/Desktop/djcode/mysite/production_static'
STATIC_URL = '/static/'
STATIC_DIRS = (
"/home/me/Django-1.5/django/contrib/admin/static/admin",
"/home/me/Desktop/djcode/mysite/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I've run the python manage.py collectstatic command which pulls all the necessary static files from my two directory locations and places them in a folder called production_static in my project folder.
Here is the production_static folder layout:
$ cd production_static
$ ls
admin css images img js
The admin folder contains all the static files needed for the admin page to display. But for some reason, the css folder contains both my site's CSS as well as the admin CSS. Same with the js folder. The images folder is my site's images and the img folder is the admin site's images. I don't know why the collectstatic command is making two copies of the admin's static files.
Here is Apache's httpd.conf file:
AliasMatch ^/([^/]*\.css) /home/me/Desktop/djcode/mysite/production_static/css/$1
AliasMatch ^/([^/]*\.css) /home/me/Desktop/djcode/mysite/production_static/admin/css/$1
Alias /media/ /home/me/Desktop/djcode/mysite/media/
Alias /static/ /home/me/Desktop/djcode/mysite/production_static/
Alias /admin/ /home/me/Desktop/djcode/mysite/production_static/admin/
<Directory /home/me/Desktop/djcode/mysite/production_static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/me/Desktop/djcode/mysite/media>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/me/Desktop/djcode/mysite/mysite/wsgi.py
WSGIPythonPath /home/me/Desktop/djcode/mysite
<Directory /home/me/Desktop/djcode/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I am making sure that I restart the Apache server every time I make changes to the httpd.conf file.
And here is my urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'mysite.views.homepage_view'),
# other url-view mappings here
url(r'^admin/', include(admin.site.urls)),
)
My homepage and other pages load fine. The CSS and JS are all wonderful. It's just when I try to access http://localhost/admin/ do I get a permissions error.
Get rid of this alias:
Alias /admin/ /home/me/Desktop/djcode/mysite/production_static/admin/

apache 500 internal error when updating from django 1.3 to 1.4

Originally when I installed Django 1.3 with wsgi on my ubuntu server I used the included setup.py file and so when I wanted to update followed the Remove any old versions of Django section of the install guide by renaming the "django" folder in my site-packages "django.old" and then installing the new version by using the the setup.py file for Django 1.4
After restarting my apache server I got a standard 500 Internal error. I checked the apache error log and discovered that ADMIN_MEDIA_PREFIX has been deprecated so following the Django 1.4 release notes I removed ADMIN_MEDIA_PREFIX from the settings file and moved the admin files into the static directory under a folder called "admin" as indicated.
I restarted my apache server again and received the same standard 500 error but this time when I tried running a tail on the apache error log no new errors registered.
Without any further error messages I am really stuck so any help will be appreciated.
Below is the content of my apache site config file and the wsgi file
site config:
<VirtualHost *:80>
ServerAdmin me#mysite.com
ServerName www.mysite.com
ServerAlias mysite.com
# Indexes + Directory Root.
# DirectoryIndex index.html index.htm index.php
DocumentRoot /home/www/www.mysite.com/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ /home/www/www.mysite.com/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog /home/www/www.mysite.com/logs/error.log
CustomLog /home/www/www.mysite.com/logs/access.log combined
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/www/www.mysite.com/htdocs/>
Options FollowSymLinks MultiViews
AllowOverride All
allow from all
</Directory>
### Connect Django to the URL using WSGI (the django recommended method)
WSGIScriptAlias /myproject /django/myproject/django.wsgi
### Alias to the location of the static dir (images, css, js etc.)
Alias /myproject/static /django/myproject/static
<Directory /django/myproject/static>
Order deny,allow
allow from all
</Directory>
</VirtualHost>
django.wsgi:
import sys
import os
import django.core.handlers.wsgi
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
application = django.core.handlers.wsgi.WSGIHandler()
sys.path.insert(0, '/django/myproject/')
sys.path.insert(0, '/django/myproject/')
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
Please note I have tried to remove or rename any identifying information from these files for security reasons, so if there is an obvious syntax error etc. it is probably due to this editing. The original versions of these files are the same accept for the name changes and worked well under Django 1.3
django 1.4 have a wsgi.py file configuratiom included:
See the documentation:
https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/#the-application-object

Mod_wsgi , django and apache not working correctly

Configuration :
Application location: /home/cha0s/hello
Wsgi file directory: /home/cha0s/hello/apache/django.wsgi
django.wsgi
import os
import sys
path = '/home/cha0s/hello'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODEULE']='hello.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Apache file : /etc/apache2/sites_available/hello
hello
<VirtualHost *:80>
ServerName blabla.com
DocumentRoot /home/cha0s/hello
WSGIScriptAlias http://blabla.com /home/cha0s/hello/apache/django.wsgi
<Directory /home/cha0s/hello/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Question:
So the problem is it kind of works , but it opens directory just like a list of files , not like a django website. Any idea whats wrong? I read somewhere on stackoverflow that mod_python may be the problem , so i deleted it .
Your WSGIScriptAlias line is nonsense. It's a path, not a URL. Should be:
WSGIScriptAlias / /home/cha0s/hello/apache/django.wsgi
Also, you've misspelled DJANGO_SETTINGS_MODULE in the wsgi file.
You need to add '/home/cha0s' to sys.path.
Also go watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
This explains other things you could have got wrong, but since you don't explain what the error is you are getting, hard to tell what else is broken.

Django set up with WSGI in directory below the domain root: Url problems in templates

Django has been up and running on my mod_wsgi implementation of Apache (on Windows 7 x64, btw) for a bit. This was a huge pain, complete with having to actually hand-modify registry values to get things to install correctly and to get Django to use the same MySQL install as all my other apps. I also had to modify the PATH variable by double-escaping parentheses (Program Files (x86)) because they were screwing with batch files. But this is mostly Microsoft's fault and I'm rambling.
Here is my issue:
All of the URLs used in the URLCONF and in views work correctly. The only thing which doesn't is in templates when I try to work off the site root URL. I am running a development server and I have two Django sites, this particular one running off of www.example.com/testing.
In the templates, if I just put "/" in an < a >, then it will point to www.example.com/, and NOT www.example.com/testing/. I have read a topic on this but the issue wasn't resolved because the poster was being stubborn. I am posting my WSGI configuration in the httpd.conf below:
Listen 127.0.0.1:8001
VirtualHost *:8001 >
ServerName ****.net/testing
ServerAdmin *******#gmail.com
ErrorLog ********.log
WSGIScriptAlias /testing ****/htdocs/testing/apache/django.wsgi
Directory ****/htdocs/testing/apache >
Order deny,allow
Allow from all
/Directory>
Location "/media">
SetHandler None
/Location>
/VirtualHost>
Note: all "<" omitted so the tags would show
Here is the file django.wsgi in the above directory:
import sys
import os
sys.path.insert(0, '*****/Django/')
sys.path.insert(0, '*****/htdocs/')
sys.path.insert(0, '*****/htdocs/testing')
sys.path.insert(0, '*****/htdocs/testing/apache')
os.environ['DJANGO_SETTINGS_MODULE'] = 'testing.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
import testing.monitor
testing.monitor.start(interval=1.0)
I have an Nginx frontend which passes any non-static file from testing/ to :8001 for Apache to catch with this virtualhost.
If I omit the root "htdocs/" line from the django.wsgi file, I just get 500 errors on the site. Also, if I use the URL form relative to the current URL (ie "example/" instead of "/example/"), that works alright. But if I add the initial "/" to put the URL off the root, it will make it off of "www.example.com" instead of "www.example.com/testing" like I wanted.
Sorry for the very long post, but I wanted to be as clear as possible. Thank you for your time.
This is why you should not hard-code URLs in your templates. Of course / will take you to the root of the site, not the root of your app - that's what it's supposed to do.
Instead, give your root view a name in your urlconf:
urlpatterns = patterns('',
url('^$', 'myapp.views.index', name='home')
)
now in your template you can do:
Home
and this will correctly resolve to /testing/.