XHTML.net

Technology talks by Loïc d’Anterroches

News, articles, PHP, scripts, XHTML/CSS, …

  1. Home
  2. Scripts

Run your Django app with the CherryPy server - DjangoCerise

The 2006-12-03 at 19:31 by Loïc d'Anterroches filed under Scripts.

If you like Django but do not want to run it with mod_python within Apache. Here is a wrapper around the excellent CherryPy WSGI server.

DjangoCerise, wrapper of CherryPy for Django

This package is just a clean wrapping of the excellent CherryPy WSGI server to serve Django webapplication. This has been created as I had not the ability to run Django using mod_python for my setup. The goal was to be able to run many different Django projects with a single wrapper and a configuration file for each project.

Download DjangoCerise 1.2.

Content of the archive

wsgiserver.py
the excellent WSGI server of the CherryPy project
webserver.py
a python wrapping of wsgiserver.py to specifically serve Django applications.
myprojectconf.py-dist
an example of configuration file.
daemon.sh
an example of optional starter script.
README
a file with what you are reading at the moment

Installation

The installation is really easy. You only need to have Django installed on your system and your Django project settings file accessible in your Python path. To test this just open a Python shell and type:

>>> import myproject.settings

If no errors, everything is ok and you can go further with installation of the webserver.

  • Copy the content of this archive in a folder on your server, for example /home/yourlogin/server
  • Make a copy of myprojectconf.py-dist to myprojectconf.py
  • Make a copy of daemon.sh with the name myproject.sh

Now you need to edit the myprojectconf.py and myproject.sh files to reflect the configuration of your Django project and the location of the different files on your system.

myprojectconf.py

The 3 important variables to change are:

DJANGO_SETTINGS
Same value as the DJANGO_SETTINGS_MODULE of the project you want to run.
DJANGO_SERVE_ADMIN
Do you want to server the admin files or not.
PIDFILE
The path to the PID file to control the daemon.
myproject.sh
DAEMON
The full path to the webserver.py file available in this archive.
PIDFILE
The same file as in myprojectconf.py
BASEDIR
The folder in which the myprojectconf.py file is.
CONFIG
If your config is in myprojectconf.py it must be myprojectconf without the .py. (Simply the python module name of your configuration file)

Running the server

You can run the server through the myproject.sh daemon wrapper or using a direct python call.

Direct python call

Run simply:

> python /path/to/webserver.py --conf myprojectconf

If you are not running it as daemon as defined in the myprojectconf.py file, simply type CTRL+C to stop the server.

Through myproject.sh
Start the server
./myproject.sh start
Stop the server
./myproject.sh stop
Restart the server
./myproject.sh restart

Running the server proxied by NGINX

I am running my website using the NGINX webserver acting as a proxy for my CherryPy Django instance. Here is an example of configuration.

http {
   include       /etc/nginx.mime.types;
   default_type  application/octet-stream;
   access_log  /var/log/nginx/access.log combined;
   sendfile        on;
   keepalive_timeout  65;
   tcp_nodelay        on;
   server {
       listen 80;
       server_name yourserver.com;
       access_log  /var/log/nginx/yourserver.com-access.log combined;
       error_log  /var/log/nginx/yourserver.com-error.log;
       location / {
           proxy_pass http://localhost:8088;
           proxy_set_header X-Forwarded-Host $server_name;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
       }
   }
   server {
   	other... like to serve the media files
   }
}

This is just an example, you should adapt it to your configuration.

You can learn more about NGINX on the NGINX website.

Changelog

  • 1.2 - Update to the latest version of the CherryPy WSGI server, fixed issues reported in the comments and added SSL support.
  • 1.1 - Force the delete of the PID file when using daemon.sh.
  • 1.0 - Initial release.

Download


Comments from readers

Vizcayno said:

Hello:
Thanks for your effort.
I got it to work on win XP SP2 but, of course, not running at the daemon level and only making the next changes:
1) Replaced os.chdir("/tmp") by os.chdir("c:/temp"). May be you could add a new variable (e.g. TEMP_DIR) in myprojectconf.py

2) Because of compilation error I commented next lines:
signal.signal(signal.SIGUSR1, self.signal_handler)
signal.signal(signal.SIGHUP, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)

Cherrypy 3.0 will be very soon with us, it has SSL incorporated. Would it encourage you to work on it?

Very best regards.

Loïc said:

I think the best motivation factor is people asking for the support. So yes, I will release a version with SSL support when CherryPy 3.0 will be available and me back from a nice Christmas break.

vizcayno said:

Loïc:
It is me again ...
I would like to know if it is possible to serve static files with your nice application, without using NGINX.
Thank you for your attention and very best regards.
Vizcayno.

Viscayno said:

Implementation of DjangoCerise as a Windows XP SP2 service
----------------------------------------------------------------------------
General indications
-----------------------
I could run DjangoCerise as a Windows Service (on WinXp Sp2). I did not try on Win2000/win2003 but I believe it is possible. First create the service using command “instsrv.exe” to put the name of the service (any) and the name of the command to execute called “srvany.exe”; as I understand srvany is equivalent to “cmd.exe”; both executables are part of the “Windows Resource Kits”. Once the service is created, it is necessary to get into regedit and look for the service name created, then include some keys for setting the environment variables, programs to run, directories, etc.

Detailed indications
-----------------------
1) Please, follow and understand the indications about DjangoCerise given by Loïc.
2) If you don’t have the files “instsrv.exe” and “srvany.exe” please get it from the “Windows Resource Kits” (WRK): http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en , and follow the installation instructions indicated there. Not only the 2 files will be installed, WRK has almost 200 files.
3) Once installed and assuming the 2 files are in C:\Program Files\Windows Resource Kits\Tools, open a cmd.exe session and type:
C:\Program Files\Windows Resource Kits\Tools\instrsrv djweb “C:\Program Files\Windows Resource Kits\Tools\ srvany.exe”
4) Please read the next articles.
http://support.microsoft.com/kb/137890 (How To Create a User-Defined Service)
http://support.microsoft.com/kb/197178 (Passing Environment Variables to Applications Started by SRVANY)
http://support.microsoft.com/kb/152460 (Troubleshooting SrvAny Using Cmd.exe)

5) After you carefully have read previous articles, once you are into the Windows registry and, assuming also that DjangoCerise is in “c:\os\webserver\djangocerise” and that the config file name is “conf_test” containing the values established according to DjangoCerise README, go to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\djweb and create a key called “Parameters”; then into this key, create next values:
a. AppDir (string value or REG_SZ) with value data C:\os\WebServer\djangocerise\
b. AppEnvironment (multi-string value or REG_MULTI_SZ) wit value date:
path=c:\python25
pythonpath=c:\os\django
SystemRoot=c:\winnt
Note 1: Press Enter to separate every variable. SystemRoot is necessary for the python _socket module. As I did not have errors, I did not put variable DJANGO_SETTINGS_MODULE but may be it is necessary under certain circumstances.
Note 2: One way to avoid AppEnvironment would be doing a .bat file, where to put the environment variables and inmediately invoke to: python C:\os\WebServer\djangocerise\webserver.py --conf conf_docum_test, however the problem I observed is that when you stop the service, the .bat file terminates the execution but the python.exe program invoked previously by the .bat file, continues running so it is necessary to kill it manually although, the next site found a probable solution: http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
c. Application (string value or REG_SZ) with value data c:\python25\python.exe
d. AppParameters (string value or REG_SZ) with value data c:\os\WebServer\djangocerise\webserver.py --conf conf_test

That is all, please try it starting, invoking your django application using a Web browser and stopping the new service. Any feedback is welcome.

Chad Whitacre said:

See also: Stephane, a tiny shim to serve Django with Aspen:

http://code.google.com/p/aspen-commons/wiki/Stephane

Aspen is a Python webserver:

http://www.zetadev.com/software/aspen/

Stephan said:

Hi Loïc, thank you very much for DjangoCerise! Will there be a version based on CherryPy 3.x supporting SSL?

cwells said:

Works great for me, thanks!. Since I also use Nginx, SSL is a non-issue (proxied apps never see the SSL anyway), but CP3 is shows huge performance boosts over CP2, so that would be another reason to use it.

cwells said:

As a followup, I replaced wsgiserver.py with the one from CP3 and it seems to work just fine.

Eder said:

There is a python win32 library that handles the installation and management of python services in Windows environments.

So far can I remember, it is from the Python for Windows Extensions package (http://sourceforge.net/projects/pywin32)

fulltooalok said:

Thanks everybody , I got mysite working on Windows 2k3 . Can anybody help me in setting images, css, javascript paths on windows(In linux we have physical location as /var/www/).

fulltooalok said:

Hi Loïc,

First of all thanks a lot for you article 'Run your Django app with the CherryPy server - DjangoCerise' [http://www.xhtml.net/scripts/Django-CherryPy-server-DjangoCerise].

I have configured Django on Windows 2k3 using DjangoCerise wrapper. Currently I have 3 django projects running. I am not running the server proxied by NGINX. The URLs are like,

http://192.168.0.20:8011/mysite1/
http://192.168.0.20:8022/mysite2/
http://192.168.0.20:8033/mysite3/

Now I configured the DNS and got the URLs like,

http://mydns.co.in:8011/mysite1/
http://mydns.co.in:8022/mysite2/
http://mydns.co.in:8033/mysite3/

In order to hide the port I set the sub domains for 3 separate IPs and ran the projects on Port 80 so it does not appear in the URL as,

http://dom1.mydns.co.in/mysite1/ ==> 192.168.0.20
http://dom2.mydns.co.in/mysite2/ ==> 192.168.0.21
http://dom3.mydns.co.in/mysite3/ ==> 192.168.0.22

But as you can see this is not an feasible option to have separate IP for separate projects in order to hide the ports.

If I may request your urgent help in sorting this issue, it would be great. I would like to know if I can make use the setting 'SERVER_NAME' in myprojectconf.py for hiding the port, is yes how.

Desparately awaiting reply,

Thanking you in advance,

Aalok Shelar
Software Development Consultant,
Pune, INDIA

Loïc said:

You need to proxy the projects behind a web server like Apache or NGINX if you do not want to use one IP address per project.

Sergey Shepelev said:

Running my Django project with manage.py runserver - everything is good.
Running with djangocerise webserver - 500 no such table django_session.
I tried to delete whole sqlite DB file and do manage.py syncdb again, it creates all tables fine, but still "no such table django_session" through Cherry.
This mess is on my Windows XP.

Sergey Shepelev said:

Solved. One should use absolute path to SQLite DB, because webserver does chdir to /tmp. I hate chdir. Relative paths FTW. :)

Loïc said:

Thanks for your comments, I have upgraded to the latest version of the CherryPy WSGI server and fixed this chdir stuff.

Everything is now available in version 1.2.

Arnold said:

Thank you for this info. I'll use it in my project on django.

Voice your ideas

It is painless and I try not to kill electrons in the process.


Your email is required but will not be shared nor displayed.


Do you think your comment will force me to write even better stuff next time? If so, you simply rock.


Logo of Plume CMS