Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one deploy a django application on OS X Server?

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.

like image 568
bloudraak Avatar asked Sep 02 '12 05:09

bloudraak


People also ask

What is the best way to deploy Django app?

What is the best approach to deploying a Django application? The fastest way to get a Django app deployed is probably Heroku, but I don't recommend it for a production app. The best way to deploy is to get an ubuntu server, setup nginx and a WSGI app like gunicorn. Nginx will handle all of your static files for you.

Where can I deploy my Django website?

For hosting web applications built on Django, you will need to use a platform that lets you deploy the app. One of these platforms is Heroku. Heroku is a cloud platform on which users can build and deploy applications. Heroku relies on Git, a revision control system that lets you manage the program code of your app.


1 Answers

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.

like image 142
zerocog Avatar answered Oct 15 '22 17:10

zerocog