问题
How would one deploy a Django application on OS X Server 2.0 without using homebrew or a different flavor of python than the one shipped with OS X 10.8.1? I'm using the cocoa bindings in a Django application and had trouble getting it to work with homebrew on my desktop machine (running OS X 10.8.1); hence the request to deploy the application on the system installed version of Python.
I have the following OS X Server environment, with the following already installed:
- OS X 10.8.1
- OS X Server 2.0
- Python 2.7.2
- Apache 2.2.22
Django 1.4.1 was installed using the following command:
sudo easy_install django
My first attempt is to deploy an empty website, and once that succeeds, deploy a real application to be used in production. The project was created at /Library/Server/Web/Data/WebApps/mysite/
using the following command
django-admin.py startproject mysite
I ran the application using the following command. It simply confirmed that the application was up and running. It is the standard "It worked!" page when you first created a project.
python manage.py runserver 8080
I then created a file /Library/Server/Web/Config/apache2/httpd_mysite.conf
with the following content:
WSGIScriptAlias /mysite /Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py
I further created a file /Library/Server/Web/Config/apache2/webapps/com.example.mysite.wsgi.plist
with the following content:
<?xml version="1.0" encoding="UTF-7"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>com.example.mysite.wsgi</string>
<key>displayName</key>
<string>Python "My Site" app</string>
<key>launchKeys</key>
<array/>
<key>proxies</key>
<dict/>
<key>installationIndicatorFilePath</key>
<string>/Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py</string>
<key>includeFiles</key>
<array>
<string>/Library/Server/Web/Config/apache2/httpd_mysite.conf</string>
</array>
<key>requiredModuleNames</key>
<array>
<string>wsgi_module</string>
</array>
</dict>
</plist>
The file com.example.mysite.wsgi.plist
was adapted from com.apple.webapp.wsgi.plist
and httpd_mysite.conf
adapted from httpd_wsgi.conf
. Both these files are used to run the "standalone" python application successfully when configured through the server manager.
I then created a site with the server manager, confirmed my application was in the list of web applications. However, when I visit http://example.com/mysite I get a 500 error. The logs has the following entries (IP addresses changed to 1.2.3.4 for privacy reasons):
[Sat Sep 01 21:49:17 2012] [warn] Init: Name-based SSL virtual hosts only work for clients with TLS server name indication support (RFC 4366)
[Sat Sep 01 21:49:17 2012] [notice] Apache/2.2.22 (Unix) PHP/5.3.13 with Suhosin-Patch mod_wsgi/3.3 Python/2.7.2 mod_fastcgi/2.4.6 mod_ssl/2.2.22 OpenSSL/0.9.8r DAV/2 configured -- resuming normal operations
[Sat Sep 01 21:50:13 2012] [error] [client 1.2.3.4] (8)Exec format error: exec of '/Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py' failed
[Sat Sep 01 21:50:13 2012] [error] [client 1.2.3.4] Premature end of script headers: wsgi.py
It doesn't seem that the WSGI module is handling the request, but instead, the request may be processed using FCGI. However, the log indicates that mod_wsgi/3.3
was loaded.
When I created a standard Python application that looks as follows:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
And update the files to point to /Library/Server/Web/Data/WebApps/helloworld.wsgi
rather than /Library/Server/Web/Data/WebApps/mysite/mysite/wsgi.py
then "Hello World" is displayed. I therefore assume that wsgi is correctly configured and able to execute applications and that something else is wrong with my setup.
回答1:
Some Apache configurations with distros map .py to either CGI or FASTCGI and this will conflict with mod_wsgi. This is why mod_wsgi recommends using a .wsgi extension. So, rename 'wsgi.py' to 'site.wsgi' and then use 'site.wsgi' in the WSGIScriptAlias.
BTW, can you confirm that there is a precompiled mod_wsgi.so shipped with Mountain Lion server.
回答2:
I found myself struggling to migrate my macports django to Mountain Lion Server 10.8.2, and for some reason the answers here helped me remember the mod_wsgi.so differences.
I overwrote the /Applications/Server.app/Contents/ServerRoot/usr/libexec/apache2/mod_wsgi.so with my /opt/local/apache2/modules/mod_wsgi.so.
I was hoping to remove the MacPorts apache2 completely, but I think I better keep it around. I still prefer MacPorts Python at the moment, since it is easier to keep up to date with the numerically intensive packages.
Yes, also make sure you change that wsgi.py to wsgi.wsgi.
I copied httpd_wsgi.conf to httpd_mywsgi.conf; I added my django.wsgi to /Library/Server/Web/Data/WebApps.
In /Library/Server/Web/Config/apache2/webapps, I copied the com.apple.webapp.wsgi.plist.
It looks like you chose the same plist pattern.
I am still working out my static content, however, thought the mod_wsgi.so discovery could help someone else migrating from MacPorts.
回答3:
Two things:
If you want to use the WSGIScriptAlias directive you need to have the mod_wsgi apache module. I don't have OS X server but as far as I can see the module does not exists in the /usr/libexec/apache2 folder on mountain lion standard. You gonna have to download it from here. Don't know if the command line tools are included in the OS X server. I do need to install X code to get them on the standard version. There seems to be some instruction on how to install it on macos x
Have a look at Homebrew. This is a really great way to add extra software to the mac. In a twist you could install the latest python, nginx and uwsgi. Nginx and uwsgi is a great way to deploy django apps. I find it more flexible and efficient than mod_wsgi (this second point is highly subjective).
Good luck
来源:https://stackoverflow.com/questions/12233628/how-does-one-deploy-a-django-application-on-os-x-server