Configure mod_wsgi on Mac OS X? - django

I have been trying to configure mod_wsgi for two days and still no luck. Here is what I did:
Create a sample django project mysite. Run python manage.py runserver and make sure it's working
Create apache directory under mysite, create apache_django_wsgi.conf, mysite.wsgi and an empty __init__.py
Content of apache_django_wsgi.conf:
WSGIPythonHome /usr/bin
WSGIRestrictStdout Off
WSGIDaemonProcess django
WSGIProcessGroup django
Alias /site_media/ "/Users/Garth/Dev/web-app/mysite/media/"
<Directory "/Users/Garth/Dev/web-app/mysite/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
Alias /media/ "/Library/Python/2.6/site-packages/django/contrib/admin/media/"
<Directory "/Library/Python/2.6/site-packages/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
WSGIScriptAlias /mysite "/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi"
<Directory "/Users/Garth/Dev/web-app/mysite/apache">
Allow from all
</Directory>
Content of mysite.wsgi
import os
import sys
sys.path.append('/Users/Garth/Dev/web-app/mysite')
sys.path.append('/Users/Garth/Dev/web-app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Download mod_wsgi.so (precompiled binary for Mac OS X) and put it in /usr/libexec/apache2
Edit /etc/apache2/httpd.conf,
add:
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
Include /Users/Garth/Dev/web-app/mysite/apache/apache_django_wsgi.conf
Run sudo apachectl -k start
If I go to localhost, I can see the files and directories list. But if I go to localhost/mysite (which is the WSGIScriptAlias I configured), I got Internal Server Error.
The error_log of apache is:
[Fri May 13 11:10:38 2011] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Fri May 13 11:10:38 2011] [notice] Digest: generating secret for digest authentication ...
[Fri May 13 11:10:38 2011] [notice] Digest: done
[Fri May 13 11:10:38 2011] [notice] Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8l DAV/2 mod_wsgi/3.3 Python/2.6.1 configured -- resuming normal operations
[Fri May 13 11:10:50 2011] [error] [client ::1] mod_wsgi (pid=10921): Target WSGI script '/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi' cannot be loaded as Python module.
[Fri May 13 11:10:50 2011] [error] [client ::1] mod_wsgi (pid=10921): Exception occurred processing WSGI script '/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi'.
[Fri May 13 11:10:50 2011] [error] Traceback (most recent call last):
[Fri May 13 11:10:50 2011] [error] File "/Users/Garth/Dev/web-app/mysite/apache/mysite.wsgi", line 1, in <module>
[Fri May 13 11:10:50 2011] [error] import os
[Fri May 13 11:10:50 2011] [error] ImportError: No module named os
Does anyone see where goes wrong? The error_log seems to tell a lot. I'm new to web development, and I might have made some obvious mistakes. Thank you very much!

Don't set WSGIPythonHome for a start it isn't needed. It should only be needed in certain situations and this isn't one of them.
In this case you have set it to a wrong value. By rights the fact it is wrong shouldn't have caused a problem as Python should have fallen back to using correct default value, but it may not do that for some reason.
You can also delete the line:
WSGIRestrictStdout Off
as that isn't needed for mod_wsgi 3.3.

Related

Apache + mod_wsgi + django error on windows 10

I followed this guide exactly: https://www.codementor.io/#aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
I am trying to deploy my django application for the first time ever using mod_wsgi and apache on windows 10.
As mentioned in the guide, I changed a few things:
wsgi_windows.py
activate_this = 'C:/pythonvenv/djangoproj/Scripts/activate'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('C:/pythonvenv/djangoproj/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('C:/pythonstuff/djangoproj')
sys.path.append('C:/pythonstuff/djangoproj/djangoproj')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproj.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoprog.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
httpd.conf i added the following two lines as given by: mod_wsgi-express module-config
LoadModule wsgi_module "c:/pythonvenv/djangoproj/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win32.pyd"
WSGIPythonHome "c:/pythonvenv/djangoproj"
httpd-vhosts.conf
# Virtual Hosts
#
# virtual SupervisionTool
<VirtualHost *:80>
ServerName localhost
WSGIPassAuthorization On
ErrorLog "logs/my_application.error.log"
CustomLog "logs/my_application.access.log" combined
WSGIScriptAlias / "C:\pythonstuff\djangoproj\djangoproj\wsgi_windows.py"
<Directory "C:\pythonstuff\djangoproj\djangoproj">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
Alias /static "C:/pythonstuff/djangoproj/djangoproj/static"
<Directory "C:/pythonstuff/djangoproj/djangoproj/static">
Require all granted
</Directory>
</VirtualHost>
# end virtual SupervisionTool
Everytime i try to run httpd.exe to start my webserver, it gives the following error and then doesn't start. The error is found in the logs.
[Mon Jul 06 19:31:33.542403 2020] [mpm_winnt:notice] [pid 19432:tid 748] AH00455: Apache/2.4.41 (Win32) PHP/7.3.12 mod_wsgi/4.7.1 Python/3.8 configured -- resuming normal operations
[Mon Jul 06 19:31:33.542403 2020] [mpm_winnt:notice] [pid 19432:tid 748] AH00456: Apache Lounge VS16 Server built: Aug 9 2019 16:32:28
[Mon Jul 06 19:31:33.542403 2020] [core:notice] [pid 19432:tid 748] AH00094: Command line: 'httpd.exe -d C:/wamp/bin/apache/apache2.4.41'
[Mon Jul 06 19:31:33.546388 2020] [mpm_winnt:notice] [pid 19432:tid 748] AH00418: Parent: Created child process 17612
Python path configuration:
PYTHONHOME = 'c:\pythonvenv\djangoproj'
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\wamp\\bin\\apache\\apache2.4.41\\bin\\httpd.exe'
sys.base_prefix = 'c:\\pythonvenv\\djangoproj'
sys.base_exec_prefix = 'c:\\pythonvenv\\djangoproj'
sys.executable = 'C:\\wamp\\bin\\apache\\apache2.4.41\\bin\\httpd.exe'
sys.prefix = 'c:\\pythonvenv\\djangoproj'
sys.exec_prefix = 'c:\\pythonvenv\\djangoproj'
sys.path = [
'C:\\pythonvenv\\djangoproj\\Scripts\\python38.zip',
'c:\\pythonvenv\\djangoproj\\DLLs',
'c:\\pythonvenv\\djangoproj\\lib',
'C:\\wamp\\bin\\apache\\apache2.4.41\\bin',
]
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'
Current thread 0x00004a24 (most recent call first):
<no Python frame>
[Mon Jul 06 19:31:35.480078 2020] [mpm_winnt:crit] [pid 19432:tid 748] AH00419: master_main: create child process failed. Exiting.
I am not sure what went wrong... can anyone please advise? If not, is there any more hassle-free ways to deploy django on windows 10?

System-wide Django package overrides virtualenv in mod_wsgi

I'm trying to deploy my Django app on mod_wsgi 3.3 (Ubuntu 12.04). It doesn't work only because the (older) system-wide version of Django interferes and overrides the one installed in virtualenv. I've checked that uninstalling the system-wide version fixes this, but I want to have both.
Here's my configuration:
/home/user/project/wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
/etc/apache2/sites-enabled/project
WSGIScriptAlias /project /home/user/project/wsgi.py
WSGIDaemonProcess project python-path=/home/user/project:/home/user/venv/lib/python2.7/site-packages
WSGIProcessGroup project
<Directory /home/user/project>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Here's what I see in /var/log/apache2/error.log:
[Fri Aug 01 16:02:09 2014] [error] [client 127.0.0.1] File "/home/user/project/wsgi.py", line 30, in <module>
[Fri Aug 01 16:02:09 2014] [error] [client 127.0.0.1] from django.core.wsgi import get_wsgi_application
[Fri Aug 01 16:02:09 2014] [error] [client 127.0.0.1] ImportError: No module named wsgi
This is because the global version is 1.3, which doesn't have django.core.wsgi.
The virtualenv version is 1.7. Under virtualenv, Python imports the correct Django version.
You're not activating your virtualenv inside the wsgi file, so Python knows nothing about it.
With the version of mod_wsgi you are using, you could have just said:
WSGIDaemonProcess project python-home=/home/user/venv python-path=/home/user/project
That is, use python-home option to WSGIDaemonProcess to specify the sys.prefix value of the Python virtual environment.
No need to fiddle with sys.path in the WSGI script file.

Error 500 and other website's content

I have some small django projects working on the same server with apache and mod_wsgi. I installed another one project with virtualenvironment and set settings for it in Apache conf file:
<VirtualHost XXX.XXX.XXX.XXX:81 >
ServerName mywebsite.com
CustomLog /var/www/httpd-logs/mywebsite.com.access.log combined
DocumentRoot /var/www/empirik/data/www/mywebsite.com
ErrorLog /var/www/httpd-logs/mywebsite.com.error.log
ServerAlias www.mywebsite.com
SuexecUserGroup empirik empirik
WSGIScriptAlias / /var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py
WSGIDaemonProcess mywebsite.com python-path=/var/www/empirik/data/www/mywebsite.com/env/lib/python2.7/site-packages
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
Order deny,allow
Allow from All
</Directory>
<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
When I try to load website server returns 500 error or another website's content in about 50% times and there are some strange errors in apache log files that I can't understand because it tries to load another website's project settings:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] mod_wsgi (pid=32361): Exception occurred processing WSGI script '/var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py'.
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] Traceback (most recent call last):
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self.load_middleware()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self._setup()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] self._wrapped = Settings(settings_module)
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] ImportError: Could not import settings 'anotherproject.settings' (Is it on sys.path?): No module named anotherproject.settings
Another question is why it still uses default system python but not from virtualenv? Please help!
You cannot use different python executeables under the same apache due to the fact that WSGIPythonHome is set for all virtual hosts globally (see docs). If WSGIPythonHome is not set - mod_wsgi will use system python.
It seems like you have not set up wsgi.py correctly. Your goal in wsgi.py is to manipulate sys.path correctly so that all that your project needs is available. Here's what helped me when I had same problems: apache server not using proper virtualenv with WSGI setting.
Hope that helps.
Your configuration is missing:
WSGIProcessGroup mywebsite.com
Without that, your WSGI application isn't even going to be delegated to the daemon process group. You can check with:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Embedded_Or_Daemon_Mode
Once you have daemon mode being used properly, so long as you are using mod_wsgi 3.4, you can set Python home for that specific daemon process group using:
WSGIDaemonProcess mywebsite.com python-home=/var/www/empirik/data/www/mywebsite.com/env
That way you can simply point at the root of the Python virtual environment and it will be picked up.
You still need though to specify the python-path option as well to the parent directory of your Django project so it and the settings module can be found, which is the problem you are having. Thus likely you want:
WSGIDaemonProcess mywebsite.com \
python-home=/var/www/empirik/data/www/mywebsite.com/env \
python-path=/var/www/empirik/data/www/mywebsite.com
Now if running only that site in the daemon process group, set:
WSGIApplicationGroup %{GLOBAL}
to avoid issues with C extension modules that don't work in sub interpreters.
To be safer, instead of:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
Order deny,allow
Allow from All
</Directory>
you should use:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
<Files wsgi.py>
Order deny,allow
Allow from All
</Files>
</Directory>
That way if screw up Apache configuration, less risk of someone downloading your code and settings file.
And where you have:
<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
Order deny,allow
Allow from all
</Directory>
you would appear to be missing the corresponding:
Alias /media /var/www/empirik/data/www/mywebsite.com/myproject/static
If you don't have an Alias for static media directory, Apache will not serve up files there.
Finally, since you want to force daemon mode and want to avoid embedded mode, set:
WSGIRestrictedEmbedded On
That way if you screw up configuration and things run in embedded mode by mistake as you currently are, you will get an error.

Apache not getting access to virtual environment set up for Django project

I created a virtualenv named 'pyapps' and installed pinax and django in it.I've installed apache 2 and mod_wsgi.I created a directory named 'apache' inside my django project(testproject) and put 'django.wsgi' file inside that directory.Here is the content of my wsgi file:
import os
import sys
# put the Django project on sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
os.environ['DJANGO_SETTINGS_MODULE'] = 'textpisodes.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Then I created a directory '/check/www' and put my project folder and pyapps folder inside '/check/www'.I chmoded 777 on /check/www.
Finally I created a virtual host,it's content is as follows:
<VirtualHost *:80>
ServerAdmin myemail
ServerName djangoserver
WSGIDaemonProcess textpisodes user=rajat threads=10 python-path=/check/www
WSGIProcessGroup textpisodes
WSGIScriptAlias / /check/www/textpisodes/apache/django.wsgi
<Directory /check/www/textpisodes/apache>
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
Now when I try to access 'http://djangoserver',all I'm getting is Internal Server Error.Here are the contents of my apache log file.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Target WSGI script '/check/www/textpisodes/apache/django.wsgi' cannot be loaded as Python module.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] mod_wsgi (pid=4890): Exception occurred processing WSGI script '/check/www/textpisodes/apache/django.wsgi'.
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] File "/check/www/textpisodes/apache/django.wsgi", line 9, in <module>
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Thu Sep 13 18:39:51 2012] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
You need to add your virtual environment path so that apache/python can find modules there.
Either put this in your apache configuration (outside of VirtualHost entry)
WSGIPythonPath /home/me/virtualenv/env1/lib/python2.7/site-packages
Or put it in the sys.path in your wsgi file.

Snow Leopard: Apache + mod_wsgi + django problem

i tried to set up apache + mod_wsgi + django on my snow leopard, but it kept gave me the same errors which listed below. i tried to follow every tutorial on the web that i could found, but still couldn't make it work :( . (im sure all the paths are added into sys.path, but don't know why it had the import error)
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Target WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi' cannot be loaded as Python module.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] mod_wsgi (pid=4333): Exception occurred processing WSGI script '/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi'.
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] File "/Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi", line 8, in <module>
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] import django.core.handlers.wsgi
[Tue Aug 02 14:47:36 2011] [error] [client 127.0.0.1] ImportError: No module named django.core.handlers.wsgi
Here is the apache configuration file:
<VirtualHost *:80>
WSGIDaemonProcess localdjango processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup localdjango
ServerName localdjango
WSGIScriptAlias / /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
<Directory /Users/kyle/wsgi_source/django-tutorial/mysite/apache>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
here is /Users/kyle/wsgi_source/django-tutorial/mysite/apache/django.wsgi
1 import sys
2 import os
3
4 sys.path.append('/Users/eookoo/wsgi_source/django-tutorial')
5
6 os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
7
8 import django.core.handlers.wsgi
9 application = django.core.handlers.wsgi.WSGIHandler()
Thanks in advance.
It is import to know whether you are using the operating system supplied Python or not.
If you are and that is the only Python version on the system, then Django wasn't installed into it. That or if you are using a Python virtual environment, then you haven't set up your WSGI script file or mod_wsgi to use the Python virtual environment.
If you have installed a separate Python version, and Django is installed into it, then likely that mod_wsgi was compiled against the system Python version and not your version.
Alternative to the latter is that you are hitting a problem that occurs with some Python installations that caused framework linking under MacOS X to not work properly and at run time, even though mod_wsgi was compiled against your separate Python version, it is still using the system wide Python framework. In this latter case you need to rebuild mod_wsgi against your separate Python version, but this time use the '--disable-framework' option to the 'configure' script for mod_wsgi before building it.
For a general resource on MacOS X issues for mod_wsgi, ensure you read the documentation at:
http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX