Mod_wsgi , django and apache not working correctly - django

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.

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.

Django: how to configure Apache to serve Django apps with mod_wsgi

I tried to set up a Django app with Apache and mod_wsgi, but ran into a problem that I have no ideas where is the cause. The app works fine with the command "python manage.py runserver", but when I tried to run it with Apache, I got the following errors in the Apache error log file.
Current thread 0x00007fb4880ad940 (most recent call first):
<no Python frame>
Python path configuration:
PYTHONHOME = '/data/anaconda3/envs/partsdb'
PYTHONPATH = (not set)
program name = 'python3'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = '/usr/bin/python3'
sys.base_prefix = '/data/anaconda3/envs/partsdb'
sys.base_exec_prefix = '/data/anaconda3/envs/partsdb'
sys.platlibdir = 'lib64'
sys.executable = '/usr/bin/python3'
sys.prefix = '/data/anaconda3/envs/partsdb'
sys.exec_prefix = '/data/anaconda3/envs/partsdb'
sys.path = [
'/data/anaconda3/envs/partsdb/lib64/python38.zip',
'/data/anaconda3/envs/partsdb/lib64/python3.8',
'/data/anaconda3/envs/partsdb/lib64/python3.8/lib-dynload',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
I have the following lines in an Apache conf file.
WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
WSGIScriptAlias / /data/partsdb/partsdb/wsgi.py
<Directory "/data/partsdb/partsdb">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
I also replaced the following two lines in the Apache conf file
WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
with the following two lines, but got the same errors.
WSGIDaemonProcess partsdb python-path=/data/partsdb/partsdb python-home=/data/anaconda3/envs/hla3db_venv
WSGIProcessGroup partsdb
The file /data/partsdb/partsdb/wsgi.py just contains the following lines of codes.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'partsdb.settings')
application = get_wsgi_application()
Upon a brief debugging, I found out that the errors were from this line in wsgi.py.
from django.core.wsgi import get_wsgi_application
My machine's OS is redhat 8, and the Apache version is 2.4.37. Thanks for any info/hints.
Try the following code:
WSGIDaemonProcess partsdb python-path=/data/partsdb/partsdb python-home=/data/anaconda3/envs/hla3db_venv
WSGIProcessGroup partsdb
WSGIApplicationGroup %{GLOBAL}
WSGIPythonHome /data/anaconda3/envs/partsdb
WSGIPythonPath /data/partsdb/partsdb
WSGIScriptAlias / /data/partsdb/partsdb/wsgi.py
<Directory "/data/partsdb/partsdb">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static /path/to/directory/defined/as/STATIC_ROOT/
# The first part is defined by STATIC_URL in settings.py
<Directory /path/to/directory/defined/as/STATIC_ROOT>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName mysite.app
ServerAdmin webmaster#localhost
# You do not need to specify DocumentRoot
# I have seen advice not to do so
ErrorLog ${APACHE_LOG_DIR}error.log
CustomLog ${APACE_LOG_DIR}access.log combined
</VirtualHost>
Please note I used this for a single app running in daemon mode as suggested in the documentation, on a Ubuntu server. Check the permissions on the folders, in my case I had to change permissions to allow apache to read and write to the folders the site was in.
You can also look at the answer Graham Dumpleton gave a few years back here:
Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'

Cannot resolve path in Flask application running on WSGI/Apache [duplicate]

This question already has an answer here:
Refering to a directory in a Flask app doesn't work unless the path is absolute
(1 answer)
Closed 5 years ago.
I am trying to deploy a FLASK application in Ubuntu VPS using wsgi but I get an Internal server error. Checking the apache.log gives me following error:
No such file or directory: 'files/filesystem/filesystem.pickle'.
My directory tree looks like this under /var/www/:
dmft_seacrh_engine
files
filesystem
filesystem.pickle
dmft_search.wsgi
dmft_search
controllers
search.py
I am trying to open the filesystem.pickle inside search.py as following:
with open('/files/filesystem/filesystem.pickle', 'rb') as handle:
filesystem = pickle.load(handle)
Contents of the dmft.wsgi file are:
activate_this = '/opt/dmft/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/dmft_search_engine")
from dmft_search import app as application
This is how my conf file looks:
<VirtualHost *:80>
ServerName dmft_search_engine.com
WSGIScriptAlias / /var/www/dmft_search_engine/dmft_search.wsgi
<Directory /var/www/dmft_search_engine/dmft_search/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/dmft_search_engine/dmft_search/static
<Directory /var/www/dmft_search_engine/dmft_search/static/>
Order allow,deny
Allow from all
</Directory>
Alias /files /var/www/dmft_search_engine/files/$
<Directory /var/www/dmft_search_engine/files/>$
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Kindly help me out. I have also added an alias for /files in .conf file.
How can I resolve this?
The problem is that the app run in the context of root directory
so the correct path should be:
with open('var/www/html/dmft_search_engine/files/filesystem/filesystem.pickle', 'rb') as handle:
filesystem = pickle.load(handle)

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)

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