Django on windows: how to make Django run in CherryPy as a Windows service

Recently a client asked me to investigate the possibility to have a Django application hosted in a Windows environment. I looked around a bit and there are many ways to achieve this goal. I found the “pure Python” approach described here simpler to deploy and configure, yet very powerful and production ready for my needs.

I will explain how to install Python, CherryPy and Django on Windows, and how to configure them to serve a Django application from the CherryPy WSGI server, running as a Windows service.

Install Python 2.7

First of all you have to install the Python programming language support for Windows. I used Python 2.7.5 Windows Installer.

Then to be able to invoke the python command from the command line – no matter what the current work directory is – add Python to system path. Go to

Control panel > System > Advanced > Environment variables

look for path variable, click on Edit and add this at the end:

C:\Python27\;C:\Python27\Scripts;

You have to close the Command prompt and then reopen it to have the path variable updated.

Install CherryPy

CherryPy is a popular WSGI framework written in Python. Sometimes it is used instead of Django to develop full fledged web applications. Here we will use the powerful WSGI and HTTP server implementation in CherryPy to host our Django application. Install latest version of CherryPy using a convenient Windows installer.

CherryPy will be installed in the standard path C:\Python27\Lib\site-packages. To check your installation you can try the first tutorial by writing the following commands in the Command Prompt:

cd C:\Python27\Lib\site-packages\cherrypy\tutorial
python tut01_helloworld.py

Then open a web browser and go to http://127.0.0.1:8080 , if you see Hello World your CherryPy installation was successful.

CherryPy server started as a Windows Service

It is very convenient to have your CherryPy based WSGI server launched as a Windows Service. In this way you can control your WSGI server using the standard management tools of Windows. You have to install the pywin32 package to have Windows Service integration in Python.

You can find a simple example script in the CherryPy wiki for launching a CherryPy based app as a Windows Service:

Save the script with the name cherrypy_service.py, then you can use the following commands to install the service in Windows and control the service afterwards:

Install service with:

python cherrypy_service.py install

and start it with

python cherrypy_service.py start

If you do modifications to the script, use

python cherrypy_service.py update

to update the service, then

python cherrypy_service.py restart

to restart the modified service. You can also configure the service to start automatically from:

Control panel > Administration tools > Services

Install Django

Django is a popular web framework written in Python. A complete description of Django and how to use it is beyond the scope of this how-to, but I suggest you to look at the wonderful tutorials in the Django documentation.

Download the latest version of Django, extract the tar.gz using WinZip (unfortunately tar.gz is not supported in my version of Windows), then install Django with the usual:

python setup.py install

Try your Django installation with the following command in the Command Prompt

python -c "import django; print(django.get_version())"

if Django is installed correctly it will print the version number of Django, for instance 1.5.4. Now you can create your Django project and apps as described in the first tutorial in the Django documentation.

 

Give a powerful DB engine to your Django app: install MySQL!

Download the MySQL 5.5 windows installer and launch it; the installer will ask you some configuration preferences, the most important is the root password for the DB administration

Enter mysql shell with your MySQL root account (assuming you chose mysecret as your root password):

mysql -uroot -pmysecret

In the mysql shell create a test database and a test user with all privileges for the test DB:

CREATE DATABASE test_django;
CREATE USER django@localhost IDENTIFIED BY 'django';
GRANT ALL PRIVILEGES ON test_django.* TO django@localhost;

To be able to use MySQL as a DB backend in Django you have to install a last package: MySQL-python, by downloading and launching a convenient Windows installer.

Then you can configure Django to use the newly created DB, by modifyng the DATABASES setting in the settings.py module of your Django project:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test_django',
    'USER': 'django',
    'PASSWORD': 'django',
    'HOST': '', # Empty for localhost through domain sockets
    'PORT': '', # Set to empty string for default.
  }
}

Now you can try syncdb to check the correct installation and configuration of Django and MySQL:

python manage.py syncdb

if all goes well it will create some “internal” Django tables in the MySQL database and it will ask you to create a Superuser account for your Django project.

Make your Django project an installable python package

That’s simple! Just download ez_setup.py save it in the Django project folder (where manage.py resides). Then in the same folder create a new file setup.py with this content:

from ez_setup import use_setuptools
use_setuptools()
from setuptools import setup, find_packages
setup(
  name = "django_site",
  version = "0.1",
  packages = find_packages(),
  author = "John Doe",
  author_email = "john@example.com",
  description = "Test Django",
  include_package_data = True
)

Now you can install your Django project by doing:

python setup.py install

Django running in CherryPy as a Windows service

This great article explains how to serve a Django application from the CherryPy WSGI server, I borrowed the code given in their Bitbucket repo and I modified it a little to better fit my needs. I also added a script to make our CherryPy server launched as a Windows service, as described before. You can download a zip package from Google Drive with all the code.

You can use the following command to install the Windows service:

python cherrypy_django_service.py install

Then you can start the service with:

python cherrypy_django_service.py start

The script cherrypy_django_service.py assumes your Django project is installed in a standard Python path; this is guaranteed if you installed your Django project as a Python package as described before.

Also, the script assumes that your Django project is called django_site. If your Django project has a different name (likely) you have to edit cherrypy_django_service.py accordingly.

If everything works you should see your Django site at the address:

http://127.0.0.1:8090

You can change the port in the cherrypy_django_service.py script, together with other CherryPy options, which are however beyond the scope of this how-to.

Conclusions

The method described here is IMHO a quite simple and straightforward way to deploy Django on Windows. I’m particularly interested in your feedback about this approach and the results you obtained. Also the scalability and the performance of this solution is a very interesting discussion topic for me. Please leave a comment below if you have something to share.

About these ads

2 Responses so far

  1. 1

    Kok Hoor said,

    Are you able to mount static folders via CherryPy while doing all this? I am not able to. Probably that’s because everything is passed to django?

    • 2

      baxeico said,

      Yes, I was able to make static files served by CherryPy itself, look in the file djangoplugin.py contained in the zip package linked in the article, the key is in the following code:


      # App specific static handler
      static_handler = cherrypy.tools.staticdir.handler(
      section=”/”,
      dir=os.path.split(settings_module.STATIC_ROOT)[1],
      root=os.path.abspath(os.path.split(settings_module.STATIC_ROOT)[0]))
      cherrypy.tree.mount(static_handler, settings_module.STATIC_URL)


Comment RSS · TrackBack URI

Comments are closed.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: