Newsroom Update

Beginning in May, a special Today at Apple series titled “Made for Business” will offer small business owners and entrepreneurs free opportunities to learn how Apple products and services can support their growth and success. Learn more >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

OSX Server mountain lion Run python web app wsgi

I'm using OSX Server.app of mountain lion. I want to run python web application on my Server.

How can I add wsgi application to my Web sites?

I think I must edit /Library/Server/Web/Config/apache2/httpd_wsgi.conf and /Library/Server/Web/Config/apache2/webapps/com.example.mywebapp.plist.

How should I edit these files?

Server.app-OTHER, OS X Mountain Lion (10.8.2)

Posted on Oct 18, 2012 2:47 AM

Reply
Question marked as Best reply

Posted on Oct 19, 2012 8:19 AM

Server.app already includes the mod_wsgi module and a default mod_wsgi webapp example at /Library/Server/Web/Data/WebApps/hello.wsgi. It's best to leave these as-is and duplicate the files to add additional sites just in case you mess up the config.


Decide where to store your site preferably in the Sites folder. You are right about editing the files you mentioned, however copy and rename them to something like httpd_wsgi.maasaamiichii.conf. Edit that file to set your own WSGIScriptAlias, for example

WSGIScriptAlias /WSGI /Users/maasaamiichii/Sites/WSGI.wsgi

Also edit the .plist file changing 3 keys to your own stuff to match the .conf file. This file should be edited in XCode and remember the ownership has be root:wheel 644 to run. After adding these files you may need to restart the server and/or Server.app.


Next edit the file at /private/etc/hosts to include an entry like

127.0.0.1 maasaamiichii.dev

This so your browsers know the domain is on your devbox machine.


With these steps done, open the Server.app and select Websites in the sidebar. Invent a website name, let's say maasaamiichii.dev is your devbox name and maasaamiichii.com is your internet name. Path needs to match the WSGIScriptAlias path above. Select "Advanced Settings" and enable the middle 3 options (htaccess, folder & cgi). In the bottom box select the Python WSGI webapp to run on that site. Save and you are finished. Back on the front window you can see your website by clicking the lower right corner link.


Please note that the default mod_wsgi as included at

/Applications/Server.app/Contents/ServerRoot/usr/libexec/apache2/mod_wsgi.so

runs the included Python version 2.6? and as such is outdated. Python 3 scripts may not run without custom building mod_wsgi. In addition, the included mod_wsgi is built for "embedded mode" and so the example script forgets all data between threads. To make it remember values persistently the module needs to be compiled for "daemon mode". So depending on what your purpose is for this website, the default config may not be enough persistence.

14 replies
Question marked as Best reply

Oct 19, 2012 8:19 AM in response to maasaamiichii

Server.app already includes the mod_wsgi module and a default mod_wsgi webapp example at /Library/Server/Web/Data/WebApps/hello.wsgi. It's best to leave these as-is and duplicate the files to add additional sites just in case you mess up the config.


Decide where to store your site preferably in the Sites folder. You are right about editing the files you mentioned, however copy and rename them to something like httpd_wsgi.maasaamiichii.conf. Edit that file to set your own WSGIScriptAlias, for example

WSGIScriptAlias /WSGI /Users/maasaamiichii/Sites/WSGI.wsgi

Also edit the .plist file changing 3 keys to your own stuff to match the .conf file. This file should be edited in XCode and remember the ownership has be root:wheel 644 to run. After adding these files you may need to restart the server and/or Server.app.


Next edit the file at /private/etc/hosts to include an entry like

127.0.0.1 maasaamiichii.dev

This so your browsers know the domain is on your devbox machine.


With these steps done, open the Server.app and select Websites in the sidebar. Invent a website name, let's say maasaamiichii.dev is your devbox name and maasaamiichii.com is your internet name. Path needs to match the WSGIScriptAlias path above. Select "Advanced Settings" and enable the middle 3 options (htaccess, folder & cgi). In the bottom box select the Python WSGI webapp to run on that site. Save and you are finished. Back on the front window you can see your website by clicking the lower right corner link.


Please note that the default mod_wsgi as included at

/Applications/Server.app/Contents/ServerRoot/usr/libexec/apache2/mod_wsgi.so

runs the included Python version 2.6? and as such is outdated. Python 3 scripts may not run without custom building mod_wsgi. In addition, the included mod_wsgi is built for "embedded mode" and so the example script forgets all data between threads. To make it remember values persistently the module needs to be compiled for "daemon mode". So depending on what your purpose is for this website, the default config may not be enough persistence.

Oct 21, 2012 10:32 PM in response to maasaamiichii

Don't do any of that, your Python script is Apache agnostic. It doesn't know what a .conf or .plist file is. Everything runs from the server end and those files all get loaded when Server.app starts up. I am referring to the underlying software, not the gorgeous GUI application.


In fact, you can expect .conf files to disappear in Apple's future vision of Server.app. They will gradually be replaced by the vastly superior .plist format.

Oct 21, 2012 10:50 PM in response to maasaamiichii

You're not adding any such wsgi webapp to a script and not importing any modules. You can duplicate the example script located at

/Library/Server/Web/Data/WebApps/hello.wsgi

to put where you want and rename it to what you want.


Then add a script alias file by duplicating this file:

/Library/Server/Web/Config/apache2/httpd_wsgi.conf


Remember, ALL .conf and .plist files you duplicate MUST be in the same folder to work.


Rename the file to something like httpd_wsgi.Gnarlodious.conf and in that file change the alias and path to the location of your .wsgi script. Restart the server and your webapp will load up. error.log will say something like this:


[Sun Oct 21 23:41:16 2012] [info] mod_wsgi (pid=31275): Starting process 'Sectrum' with uid=70, gid=70 and threads=1.

[Sun Oct 21 23:41:16 2012] [info] mod_wsgi (pid=31275): Initializing Python.

Oct 22, 2012 12:20 AM in response to Gnarlodious

Thank you for your reply.

I want to run this python web application.

https://github.com/xcoo/davdav


I copied com.apple.wsgi.plist to com.davdav.plist.


<plist>

<dict>

<key>name</key>

<string>com.davdav</string>

<key>displayName</key>

<string>davdav</string>

<key>launchKeys</key>

<array/>

<key>proxies</key>

<dict/>

<key>installationIndicatorFilePath</key>

<string>/Users/maasaamiichii/Public/davdav/davdav/app/davdav.wsgi</string>

<key>includeFiles</key>

<array>

<string>/Library/Server/Web/Config/apache2/httpd_wsgi_davdav.conf</string>

</array>

<key>requiredModuleNames</key>

<array>

<string>wsgi_module</string>

</array>

</dict>

</plist>


And I created httpd_wsgi_davdav.conf

like


WSGIScriptAlias /davdav /Users/maasaamiichii/Public/davdav/davdav/app/davdav.wsgi



When I accessed to example.com/davdav, it shows internal server error.

I have an error that mod_wsgi target wsgi script cannot be loaded as Python modules.



Are there some settings of http_wsgi_davdav.conf?


When I write WSGIDaemonProcess or WSGIPythonHome in httpd_wsgi_davdav.conf, I cannnot access to web site.

Oct 22, 2012 7:52 AM in response to maasaamiichii

That is a lot to expect, especially the part about serving DAV images from the Public folder. I am going to reduce this to a procedure to make it as easy as possible for you to understand.


First, you should be able to click here and get the mod_wsgi example webapp:

http://0.0.0.0/wsgi


Your browser should show text like:

This text is displayed by a Python script, executed via the webapp.plist(8) mechanism.

Python version 3.2.3 running mod_wsgi.


except that your Python version will be older than mine. Note that you should see the same result with these same URLs:

http://0.0.0.0/wsgi/

http://0.0.0.0/wsgi/hello.wsgi


because wsgi is your WSGIScriptAlias set in file httpd_wsgi.conf


If you do NOT see the above text, enable Python in this window:


User uploaded file


then enable the built-in WSGI webapp like this:

User uploaded file

Now try the above URLs and you should see the Python text. If this is working properly then you are ready for the next step.

Oct 31, 2012 9:20 PM in response to maasaamiichii

Good thinking. But there is a more robust way to configure your Python and also eliminate that line. Create a file at

/Library/Python/3.2/site-packages/sitecustomize.py

keeping in mind your Python version is probably 2.7, I am not sure which is default.

To that file add this text:


import sys

sys.path.append('folderPath')


The advantage of this method is that you can program using Terminal with Python's "interactive mode" and Python will import your files knowing exactly where they are. Its pretty great for programmers.


But if you just want to run a webapp, this solution is overkill. Especially since the file needs to be added to every major software update.

Jan 9, 2013 10:50 AM in response to maasaamiichii

Hi,


Did you try to run Web2py framework in Mountain Lion Server? When I try it it gives me the following error: Premature end o scritp wsgihandler.py (which is the wsgi file for Web2py).


I did manage to run Web2py framework in Mountain Lion (not server) using wsgi and also managed to run Web2py using Apache as a proxy server (mod_proxy).


I would appreciate any help.


thanks.


Flavio.

OSX Server mountain lion Run python web app wsgi

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.