Apache is not interpreting python files - django

So, I followed this tutorial on Ubuntu 14.04
http://jee-appy.blogspot.in/2015/04/deploy-django-project-on-apache-using.html
Only thing I changed is instead of his example repository,I have used mine.
And I was getting error
Not Found The requested URL / was not found on this server.
Then, I edited my myproject.conf file
Change made - WSGIScriptAlias /myproject /var/www/myproject.wsgi
So, I'm having output Index of/ then directory and file name. it's not interpreting my python files.

Had the same issue and I modified my wsgi file to look like this:
import os
import sys
path = '/var/www/html/YOURAPP/'
if path not in sys.path:
sys.path.append(path)
path = '/var/www/html/YOURAPP/YOURAPP/'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURAPP.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Hope this helps

Related

os.path.dirname(__file__) not displaying absolute path in python 2.7

I just have a python file:
#!/usr/bin/python
import os
print os.path.dirname(__file__)
but when I execute it, the terminal displays a dot, instead of the absolute path to the script.
Perhaps,
#!/usr/bin/python
import os
print os.path.realpath(__file__)
gives you what you're looking for?

How do I import files from other directory in python 2.7

I have been experimenting with python by creating some programs .The thing is, I have no idea how to import something OUT of the default python directory.
OK
So I did some heavy research and the conclusion is
if u want to access a file saved at different location
use
f = open('E:/somedir/somefile.txt', 'r')
r = f.read()
NOTE: Dont use '\' that were I went wrong.Our system addresses uses '\' So be careful
If you need to just read in a file and not import a module the documentation covers this extensively.
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
Specifically for Windows file systems you will need to do one of the following:
1.) Use forwardslashes vs backslashes. This should work with most OSes.
f = open("c:/somedir/somefile.txt", "r")
2.) Use a raw string.
f = open(r"c:\somedir\somefile.txt", "r")
3.) Escape the backslashes.
f = open("c:\\somedir\\somefile.txt", "r")
If you need to import a module to use in your program from outside your programs directory you can use the below information.
Python looks in the sys.path to see if the module exists there and if so does the import. If the path where you files/modules are located is not in the sys.path, Python will raise an ImportError. You can update the path programmatically by using the sys module.
import sys
dir = "path to mymodule"
if dir not in sys.path:
sys.path.append(dir)
import mymodule
You can check the current sys.path by using:
print(sys.path)
Example:
>>> print(sys.path)
['', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']
>>> sys.path.append("/Users/ddrummond/pymodules")
>>> print(sys.path)
['', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages', '/Users/ddrummond/pymodules']
>>>
You can see that sys.path now contains '/Users/ddrummond/pymodules'.

django production server: root path

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)

Error on importing passenger_wsgi.py file

I'm currently trying to set up a Django project using passenger wsgi. I followed the instructions laid out on this post:
Update new Django and Python 2.7.* with virtualenv on Dreamhost (with passenger)
However, I'm receiving an error "An error occurred importing your passenger_wsgi.py"
I am able to successfully receive a hello word message if I put this as my passenger_wsgi.py:
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return ["Hello, world!"]
But for some reason if I use the following (outlined in the above post), I am unable to get past the error. I replace the word 'project' with my named project on the path.append and os.environ lines and with subdomain.domain.com on the path.insert lines. Am I missing something? I am very new to this stuff and would appreciate any help I can get! Thanks.
Below is the current passenger_wsgi.py that receives the error on importing.
import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/project')
if sys.version < "2.7.3": os.execl("$HOME/<site>/env/bin/python",
"python2.7.3", *sys.argv)
sys.path.insert(0,'$HOME/<site>/env/bin')
sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages/django')
sys.path.insert(0,'$HOME/<site>/env/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
UPDATE I was able to get a passenger wsgi that imports but now I'm getting a 500 error. Here is what I'm sitting at right now:
import sys, os
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), 'project'))
sys.path.insert(0, 'home/<site>/env/bin')
sys.path.insert(0, 'home/<site>/env/lib/python2.7/site- packages/django')
sys.path.insert(0, 'home/<site>/env/lib/python2.7/site-packages')
sys.path.insert(0, 'home/<site>/roommates')
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Once again I'm stuck though. I can't seem to find why I'm getting this error. The current errors that are showing up in the error logs are as follows per attempt:
Premature end of script headers:
Premature end of script headers: internal_error.html
You should change this line:
sys.path.append(os.path.join(os.getcwd(), 'project'))
into
sys.path.append(os.path.join(os.getcwd() + 'project'))
and make sure you change project with the name of your real project (or django app)

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/.