How to host Django1.3.1 in Apache2.2? - python

I am Using python 2.7.2,Django 1.3.1, Apache 2.2.22 on WindowsXP(win32). By the documentation i found here i did the step by step, but when the directory section is given
`Alias /media/ C:/Programs/TestDjango/mysite/media/
<Directory C:/Programs/TestDjango/mysite/media/>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / C:/Programs/TestDjango/mysite/apache/django.wsgi
<Directory C:/Programs/TestDjango/mysite/apache>
Order deny,allow
Allow from all
</Directory>`
and restarted the Apache, On opening localhost/mysite i get a Microsoft Visual C++ Library runtime error, and the Apache error log shows "Caught ImproperlyConfigured while rendering: Error loading pyodbc module: DLL load failed: A dynamic link library (DLL) initialization routine failed."....My Django app run in WAMP but wish to know where did i go wrong using Apache2.2.22 alone. Followed many Django documentation but still the same, Please to help me find where did i go wrong. thanks
(identation was fixed by guettli)

I got it solved, it was the version problem, as i worked with Apache 2.2.21 instead of Apache 2.2.22, its working. i followed the step in this link.
Install Python 2.7.2, Django 1.3.1 and Apache2.2.21
Install the modwsgi module.
The module file will be named something like mod_wsgi-win32-ap22py26-2.6.so get mod_wsgi.
Copy it to the modules directory of the Apache installation. E.g., C:/Program Files/Apache Software Foundation/Apache2.2/modules.
Rename it to mod_wsgi.so. Right click--> properties click Unblock and apply
Open Apache's http.conf file.
Add the line LoadModule wsgi_module modules/mod_wsgi.so before all the other LoadModule entries.
Configure Apache for your Django project by adding the following to end of http.conf:
# Static content
Alias /media/ C:/Programs/TestDjango/mysite/media/
<Directory C:/Programs/TestDjango/mysite/media/>
Order deny,allow
Allow from all
</Directory>
# Django dynamic content
WSGIScriptAlias / C:/Programs/TestDjango/mysite/apache/django.wsgi
<Directory C:/Programs/TestDjango/mysite/apache>
Order deny,allow
Allow from all
</Directory>`
Where icardtest is the Django project root. The paths below icardtest will be specific to your project. This configuration serves all static media via the URL space /media/ and all the rest via WSGI and Django.
Create a file django.wsgi and add the following to it:
` import os
import sys
sys.path.append('C:/Programs/TestDjango')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()`
Restart Apache.

Your error has to do with your python setup, not Apache.
Error loading pyodbc module: DLL load failed: A dynamic link library (DLL) initialization routine failed.
This means that the Python you are using with Apache cannot load the pyodbc module. Once you fix this error, you can proceed.

Related

Run Django Website with different mod_wsgi than default

I have an Apache2 server hosting multiple Django websites. All except one use Django 3.8, using a mod_wsgi compiled on Python 3.7. The outlier (let's call it SiteX) uses Django 4.0, which requires Python 3.8+. I want SiteX to use a virtual environment with these newer versions of Python and Django, while the other sites keep using their default versions.
For SiteX, I've created a virtual environment venv in its directory. But from what I understand, the current mod_wsgi installed is not compatible with its version of Python, so specifying WSGIPythonHome in the conf file won't do anything.
the current mod_wsgi (4.7/python 3.7) is loaded in /etc/apache2/mods-available/wsgi.load with the line
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
I've tried creating a new mod_wsgi, this time using the Python module mod_wsgi, which I've installed in venv. In the .conf file for a group of Django sites (that include SiteX) I tried specifying this unique version of the mod_wsgi above the <VirtualHost> block with the line:
LoadModule wsgi_module "/var/www/html/app/SiteX/venv/lib/python3.9/site-packages/mod_wsgi/server/mod_wsgi-py39.cpython-39-x86_64-linux-gnu.so"
but running $ apachectl configtest says: module wsgi_module is already loaded, skipping.
I found a solution for multiple mod_wsgi's using WSGIDaemonProcess but the solution's .conf looks slightly different that mine. Below is a snippet of the relevant lines of the .conf, but I'm not sure where to insert WSGIDaemonProcess.
<IfModule mod_ssl.c>
# LoadModule wsgi_module, WSGIPythonHome, WSGIPythonPath here?
<VirtualHost _default_:443>
...
Alias /static /var/www/html/app/imagingTracking/static/
...
# other django website Directory blocks here
...
<Directory /var/www/html/app/SiteX/static/>
Require all granted
</Directory>
<Directory /var/www/html/app/SiteX/SiteX/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
</IfModule>
In addition I am not sure where to locate relevant .wsgi files for this.
Is my goal even tractable? It would be a nuisance if I'd have to downgrade SiteX just for this reason.

Flask app running on apache resulting in 404 error

I'm using mac os x and trying to get the official flask tutorial to work with Apache, but when I attempt to open the site in localhost on my browser it results in a 404: The requested URL / was not found on this server. I followed these directions for configuring mod_wsgi for flask. I've run the app using the built in server with flask so I know the app itself works.
The following is the resulting error in the Apache error log
[negotiation:error] [pid 7563] [client ::1:50509] AH00687: Negotiation: discovered file(s) matching request: /Library/WebServer/Documents/flaskr/flaskr.wsgi (None could be negotiated).
Here is what I have in my flaskr.wsgi file located in /Library/WebServer/Documents/flaskr/flaskr.wsgi
import sys
sys.path.insert(0, '/Library/WebServer/Documents/flaskr')
from flaskr import app as application
I have the following in my httpd.conf file
Listen 80
<VirtualHost *>
ServerName localhost
WSGIDaemonProcess flaskr user=_www group=_www threads=5
WSGIScriptAlias / /Library/WebServer/Documents/flaskr/flaskr.wsgi
<Directory /Library/WebServer/Documents/flaskr>
WSGIProcessGroup flaskr
WSGIApplicationGroup %{GLOBAL}
Require all granted # httpd 2.4 syntax as noted in the guide
</Directory>
</VirtualHost>
LoadModule wsgi_module /usr/local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-darwin.so
WSGIPythonHome /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6
From terminal when I run sudo apachectl -M it displays wsgi_module (shared). If I comment out the code in my httpd.conf file that I've listed above, the wsgi_module goes away from the enabled modules list, so unless there's something I'm not aware of mod_wsgi seems to be enabled successfully, but as I've mentioned I get a 404 message when trying to view the site.
EDIT
I don't think there's anything wrong with my Apache configuration. I tried this tutorial for getting flask up and running on Apache and it worked. I'll update once I figure out what I did wrong with the "flaskr" app.
Edit - found the problem
Turns out my flaskr.wsgi file was in the wrong directory. It was supposed to be in /Library/WebServer/Documents/flaskr/flaskr/flaskr.wsgi.
The official Flask tutorial I referenced uses the following directory structure
flaskr <----Initially put my `flaskr.wsgi` file in this directory
init.py
flaskr <----Changed to this directory and it worked
flaskr.py
flaskr.db
flaskr.wsgi
Also changed the system path to sys.path.append('/Library/WebServer/Documents/flaskr/flaskr')

apache mod_wsgi error with django in virtualenv

I can't seem to find a good answer to this. Who needs to own the virtualenv when running it as a WSGIDaemon? I assume on my OS (Ubuntu 16) www-data, but I want to be sure. Trying some new things to get this thing working based off of the answer from this post...
django apache configuration with WSGIDaemonProcess not working
Does the django project, the virtualenv folder, or both need to be owned by the apache group? What ownerships need to be in place to serve a django project without specifying a port? Why do I get the following?
The root problem:
Call to 'site.addsitedir()' failed for '(null)'
When I start apache, I get this error. I've followed several different guides, including:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
and
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/
but have had zero success.
My virtual environment path is /usr/local/virtualenvs/servicesite
My django project path is /home/addohm/projects/rtservice/servicesite
this is where manage.py resides,
which leaves /home/addohm/projects/rtservice/servicesite/servicesite as the location of my wsgi.py.
wsgi.py:
SERVICESITE = ['/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages']
import os
import sys
import site
prev_sys_path = list(sys.path)
for directory in SERVICESITE
site.addsitedir(directory)
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
""" **Doesn't seem to work, throwing error in apache logs**
site.addsitedir('/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages')
"""
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
apache2.conf
[...]
WSGIDaemonProcess servicesite python-path=/home/addohm/projects/rtservice/servicesite:/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
Require all granted
</Directory>
<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
[...]
You should not have need to change anything in the original wsgi.py generated by Django for you. It is generally sufficient to have:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
Your Apache configuration should then preferably be:
WSGIDaemonProcess service site python-home=/usr/local/virtualenvs/servicesite \
python-path=/home/addohm/projects/rtservice/servicesite
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py
Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
Require all granted
</Directory>
<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
That is, use python-home for the location of the directory specified by sys.prefix for the virtual environment. Avoid using python-path and referring to the site-packages directory. Using python-home has been preferred way for a very long time and using it ensures that things fail in a more obvious way when you don't do things the correct way.
A few very important things.
The first is that mod_wsgi must be compiled for the specific Python major/minor version you want to use.
Second, the Python virtual environment must be created from the same Python installation as mod_wsgi was compiled for. You can't have mod_wsgi compiled against a system Python installation, but have your virtual environment based off a separate Python installation for same major/minor version in /usr/local.
Third, the user that Apache runs your code as must have read access to any directories/files for the main Python installation, the virtual environment and your application code. When you stick stuff under a home directory, it will not generally have access as the home directory prohibits others from reading anything in the home directory.
Fourth, if the mod_wsgi daemon process group is set to run as a different user than the Apache user, the Apache user still must at least have ability to read the wsgi.py file and all the directories down to that point.
Further reading on virtual environments which is more up to date:
http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html

Unable to access dependent modules in Django project deployed in Apache server

I recently moved my Django project to Apache server. I am facing a few issues with respect to dependency modules being inaccessible after moving the project to Apache.
Here are my configuration files:
This is my bemoss.conf file in the sites-available directory of Apache:
WSGIPythonPath /home/kruthika/workspace/bemoss_web_ui
WSGIPythonPath /home/kruthika/workspace/rtunetwork/volttron
WSGIPythonPath /home/kruthika/workspace/bemoss_web_ui/helper
<VirtualHost *:80>
WSGIScriptAlias / /home/kruthika/workspace/bemoss_web_ui/bemoss.wsgi
ServerName bemoss.com
Alias /static /home/kruthika/workspace/bemoss_web_ui/static
<Directory /home/kruthika/workspace/bemoss_web_ui/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Here is my bemoss.wsgi file:
import os
import sys
sys.path = ['/home/kruthika/workspace']+sys.path
print sys.path
os.environ['DJANGO_SETTINGS_MODULE']='bemoss_web_ui.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My project is accessing a few more python files from another project. I added a reference to those in my .conf file above and in settings.py file as given below.
PROJECT_DIR = os.path.dirname(__file__)
print PROJECT_DIR
sys.path.insert(3,os.path.join(PROJECT_DIR,'ZMQHelper'))
sys.path.insert(1,os.path.join(PROJECT_DIR,'helper'))
sys.path.insert(2, '/home/kruthika/workspace/rtunetwork/volttron')
print sys.path
This is my project folder definition:
When I try to access a link, whose views.py file refers to the above mentioned dependencies, it throws a import module error as given below.
My question here is how to refer to my dependent modules/python classes so that the files are imported properly.
All these imports were working fine when I executed the project on Django's development server. Is there some Apache configuration I am missing or doing wrong? I am new to server configurations and I think I am missing something here.

Python Linking Problem while running Django on Apache with mod_wsgi: symbol not found __cg_jpeg_resync_to_restart

I get this error when I try to run my django site on apache. The site works on the development server:
ViewDoesNotExist at /
Could not import myproject.modulename.views. Error was: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_tkinter.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /Applications/MAMP/Library/lib/libjpeg.8.dylib
I am not sure how to resolve the issue. Other django sites work on this apache installation. The directory is on the path specified in my apache.conf file (see bottom of the post).
The three files referenced in the error message exist in the locations indicated.
_tkinter.so - /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_tkinter.so
ImageIO - /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
libjpeg.8.dylib - /Applications/MAMP/Library/lib/libjpeg.8.dylib
I am not sure what __cg_jpeg_resync_to_restart is.
Below is the original message I put up when I was trying to figure out what the error message meant. Thanks to sacabuche for pointing me in the right direction.
I am trying to get a Django Site to run on apache. It works on the django development server, but I get this error when I try to run it on apache with mod_wsgi. I know mod_wsgi works because I had a small trial site (that was a scaled down version of this stie) working on my mamp development server. A feat I managed with the help of others via this post: Django/mod_wsgi and PHP as Virtual Hosts on same Apache Server using MAMP
The new site uses a different database and is now at the localhost root, but otherwise is very similar, so this error is baffling me.
Here is the code for my apache conf (note: the php site works, and if I redirect the WSGIDaemonProcess to the old site, it loads without problem):
ServerName localhost
UseCanonicalName Off
DocumentRoot "/Applications/MAMP/htdocs"
Alias /phpsite /Applications/MAMP/htdocs/phpsite
<Directory "/Applications/MAMP/htdocs/phpsite">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess site1 display-name=%{GROUP}
WSGIProcessGroup site1
Alias /media/ /Applications/MAMP/htdocs/media/
<Directory /Applications/MAMP/htdocs/media>
Options ExecCGI
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /Users/sequoia/djangoprojects/myproject/apache/django.wsgi
<Directory /Users/sequoia/djangoprojects/myproject/apache>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Thanks.
Well it seems that a symlink is broken, so i looked and i found something in this forum
delete the symlinks "libpng.dylib" and "libjpeg.dylib" in "<installation directory>/lib"
Create new symlinks to the right libs.
ln -s /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libPNG.dylib /Applications/MAMP/Library/lib/libPNG.dylib
ln -s /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib /Applications/MAMP/Library/lib/libJPEG.dylib
I have no Mac rigth here, but first i would verify that the origin of the symlink exist, i hope this will solve your problem.
Don't you miss the + or - sign in your options statement?
Example of statement:
Options -Indexes +FollowSymLinks

Categories

Resources