<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Guguweb - windows</title><link href="https://www.guguweb.com/" rel="alternate"></link><link href="https://www.guguweb.com/feeds/windows.atom.xml" rel="self"></link><id>https://www.guguweb.com/</id><updated>2022-01-26T17:19:44+01:00</updated><subtitle>Freelance developer and sysadmin</subtitle><entry><title>Django on windows: how to make Django run in CherryPy as a Windows service</title><link href="https://www.guguweb.com/2013/10/13/django-on-windows/" rel="alternate"></link><published>2013-10-13T20:23:11+00:00</published><updated>2022-01-26T17:19:44+01:00</updated><author><name>Augusto Destrero</name></author><id>tag:www.guguweb.com,2013-10-13:/2013/10/13/django-on-windows/</id><summary type="html">&lt;p&gt;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 &lt;a href="https://code.djangoproject.com/wiki/WindowsInstall"&gt;many ways&lt;/a&gt; to achieve this goal. I found the &amp;#8220;pure Python&amp;#8221; approach described here simpler to deploy and configure, yet very powerful and …&lt;/p&gt;</summary><content type="html">&lt;p&gt;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 &lt;a href="https://code.djangoproject.com/wiki/WindowsInstall"&gt;many ways&lt;/a&gt; to achieve this goal. I found the &amp;#8220;pure Python&amp;#8221; approach described here simpler to deploy and configure, yet very powerful and production ready for my needs.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;div class="toc"&gt;&lt;span class="toctitle"&gt;Table of Contents&lt;/span&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="#install-python-27"&gt;Install Python 2.7&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#install-cherrypy"&gt;Install CherryPy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#cherrypy-server-started-as-a-windows-service"&gt;CherryPy server started as a Windows Service&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#install-django"&gt;Install Django&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#_1"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#give-a-powerful-db-engine-to-your-django-app-install-mysql"&gt;Give a powerful DB engine to your Django app: install MySQL!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#make-your-django-project-an-installable-python-package"&gt;Make your Django project an installable python package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#django-running-in-cherrypy-as-a-windows-service"&gt;Django running in CherryPy as a Windows service&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#conclusions"&gt;Conclusions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id="install-python-27"&gt;Install Python 2.7&lt;/h2&gt;
&lt;p&gt;First of all you have to install the &lt;a href="http://www.python.org/getit/"&gt;Python programming language&lt;/a&gt; support for Windows. I used Python 2.7.5 Windows Installer.&lt;/p&gt;
&lt;p&gt;Then to be able to invoke the &lt;em&gt;python&lt;/em&gt; command from the command line &amp;#8211; no matter what the current work directory is &amp;#8211; add Python to system path. Go to&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Control panel &amp;gt; System &amp;gt; Advanced &amp;gt; Environment variables&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;look for &lt;em&gt;path&lt;/em&gt; variable, click on &lt;em&gt;Edit&lt;/em&gt; and add this at the end:&lt;/p&gt;
&lt;pre&gt;C:\Python27\;C:\Python27\Scripts;&lt;/pre&gt;

&lt;p&gt;You have to close the Command prompt and then reopen it to have the &lt;em&gt;path&lt;/em&gt; variable updated.&lt;/p&gt;
&lt;h2 id="install-cherrypy"&gt;Install CherryPy&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://www.cherrypy.org/" title="CherryPy"&gt;CherryPy&lt;/a&gt; is a popular WSGI framework written in Python. Sometimes it is used &lt;strong&gt;instead of&lt;/strong&gt; Django to develop full fledged web applications. Here we will use the powerful &lt;a href="http://docs.cherrypy.org/stable/deployguide/index.html"&gt;WSGI and HTTP server&lt;/a&gt; implementation in CherryPy to host our Django application. Install latest version of CherryPy using a convenient &lt;a href="http://download.cherrypy.org/cherrypy/3.2.2/CherryPy-3.2.2.win32.exe"&gt;Windows installer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;CherryPy will be installed in the standard path &lt;em&gt;C:\Python27\Lib\site-packages&lt;/em&gt;. To check your installation you can try the first tutorial by writing the following commands in the Command Prompt:&lt;/p&gt;
&lt;pre&gt;cd C:\Python27\Lib\site-packages\cherrypy\tutorial
python tut01_helloworld.py&lt;/pre&gt;

&lt;p&gt;Then open a web browser and go to &lt;em&gt;http://127.0.0.1:8080&lt;/em&gt; , if you see &lt;em&gt;Hello World&lt;/em&gt; your CherryPy installation was successful.&lt;/p&gt;
&lt;h2 id="cherrypy-server-started-as-a-windows-service"&gt;CherryPy server started as a Windows Service&lt;/h2&gt;
&lt;p&gt;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 &lt;a href="http://sourceforge.net/projects/pywin32/files/pywin32/"&gt;pywin32 package&lt;/a&gt; to have Windows Service integration in Python.&lt;/p&gt;
&lt;p&gt;You can find a simple &lt;a href="http://tools.cherrypy.org/wiki/WindowsService"&gt;example script&lt;/a&gt; in the CherryPy wiki for launching a CherryPy based app as a Windows Service:&lt;/p&gt;
&lt;p&gt;Save the script with the name &lt;em&gt;cherrypy_service.py&lt;/em&gt;, then you can use the following commands to install the service in Windows and control the service afterwards:&lt;/p&gt;
&lt;p&gt;Install service with:&lt;/p&gt;
&lt;pre&gt;python cherrypy_service.py install&lt;/pre&gt;

&lt;p&gt;and start it with&lt;/p&gt;
&lt;pre&gt;python cherrypy_service.py start&lt;/pre&gt;

&lt;p&gt;If you do modifications to the script, use&lt;/p&gt;
&lt;pre&gt;python cherrypy_service.py update&lt;/pre&gt;

&lt;p&gt;to update the service, then&lt;/p&gt;
&lt;p&gt;python cherrypy_service.py restart&lt;/p&gt;
&lt;p&gt;to restart the modified service. You can also configure the service to start automatically from:&lt;/p&gt;
&lt;pre&gt;Control panel &amp;gt; Administration tools &amp;gt; Services&lt;/pre&gt;

&lt;h2 id="install-django"&gt;Install Django&lt;/h2&gt;
&lt;p&gt;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 &lt;a href="https://docs.djangoproject.com/en/1.5/"&gt;Django documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Download the &lt;a href="https://www.djangoproject.com/download/"&gt;latest version of Django&lt;/a&gt;, extract the tar.gz using &lt;a href="http://www.winzip.com/"&gt;WinZip&lt;/a&gt; (unfortunately tar.gz is not supported in my version of Windows), then install Django with the usual:&lt;/p&gt;
&lt;pre&gt;python setup.py install&lt;/pre&gt;

&lt;p&gt;Try your Django installation with the following command in the Command Prompt&lt;/p&gt;
&lt;pre&gt;python -c "import django; print(django.get_version())"&lt;/pre&gt;

&lt;p&gt;if Django is installed correctly it will print the version number of Django, for instance &lt;em&gt;1.5.4&lt;/em&gt;. Now you can create your Django project and apps as described in the &lt;a href="https://docs.djangoproject.com/en/1.5/intro/tutorial01/"&gt;first tutorial&lt;/a&gt; in the Django documentation.&lt;/p&gt;
&lt;h2 id="_1"&gt;&lt;/h2&gt;
&lt;h2 id="give-a-powerful-db-engine-to-your-django-app-install-mysql"&gt;Give a powerful DB engine to your Django app: install MySQL!&lt;/h2&gt;
&lt;p&gt;Download the &lt;a href="http://dev.mysql.com/downloads/mysql/5.5.html#downloads"&gt;MySQL 5.5 windows installer&lt;/a&gt; and launch it; the installer will ask you some configuration preferences, the most important is the root password for the DB administration&lt;/p&gt;
&lt;p&gt;Enter mysql shell with your MySQL root account (assuming you chose &lt;em&gt;mysecret&lt;/em&gt; as your root password):&lt;/p&gt;
&lt;pre&gt;mysql -uroot -pmysecret&lt;/pre&gt;

&lt;p&gt;In the mysql shell create a test database and a test user with all privileges for the test DB:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CREATE DATABASE test_django;&amp;lt;br /&amp;gt;
CREATE USER django@localhost IDENTIFIED BY 'django';&amp;lt;br /&amp;gt;
GRANT ALL PRIVILEGES ON test_django.* TO django@localhost;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;To be able to use MySQL as a DB backend in Django you have to install a last package: &lt;a href="https://pypi.python.org/pypi/MySQL-python"&gt;MySQL-python&lt;/a&gt;, by downloading and launching a convenient Windows installer.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;pre&gt;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.
  }
}&lt;/pre&gt;

&lt;p&gt;Now you can try syncdb to check the correct installation and configuration of Django and MySQL:&lt;/p&gt;
&lt;pre&gt;python manage.py syncdb&lt;/pre&gt;

&lt;p&gt;if all goes well it will create some &amp;#8220;internal&amp;#8221; Django tables in the MySQL database and it will ask you to create a Superuser account for your Django project.&lt;/p&gt;
&lt;h2 id="make-your-django-project-an-installable-python-package"&gt;Make your Django project an installable python package&lt;/h2&gt;
&lt;p&gt;That&amp;#8217;s simple! Just download &lt;a href="https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py"&gt;ez_setup.py&lt;/a&gt; save it in the Django project folder (where manage.py resides). Then in the same folder create a new file &lt;em&gt;setup.py&lt;/em&gt; with this content:&lt;/p&gt;
&lt;pre&gt;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
)&lt;/pre&gt;

&lt;p&gt;Now you can install your Django project by doing:&lt;/p&gt;
&lt;pre&gt;python setup.py install&lt;/pre&gt;

&lt;h2 id="django-running-in-cherrypy-as-a-windows-service"&gt;Django running in CherryPy as a Windows service&lt;/h2&gt;
&lt;p&gt;This &lt;a href="http://www.defuze.org/archives/262-hosting-a-django-application-on-a-cherrypy-server.html"&gt;great article&lt;/a&gt; explains how to serve a Django application from the CherryPy WSGI server, I borrowed the code given in their &lt;a href="https://bitbucket.org/Lawouach/cherrypy-recipes/src/c8290261eefb/frameworks/django_"&gt;Bitbucket repo&lt;/a&gt; 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 &lt;a href="https://docs.google.com/file/d/0B_nvs3fbLCaySDVHdW9ONUo1QW8/edit?usp=sharing"&gt;zip package&lt;/a&gt; from Google Drive with all the code.&lt;/p&gt;
&lt;p&gt;You can use the following command to install the Windows service:&lt;/p&gt;
&lt;pre&gt;python cherrypy_django_service.py install&lt;/pre&gt;

&lt;p&gt;Then you can start the service with:&lt;/p&gt;
&lt;pre&gt;python cherrypy_django_service.py start&lt;/pre&gt;

&lt;p&gt;The script &lt;em&gt;cherrypy_django_service.py&lt;/em&gt; 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.&lt;/p&gt;
&lt;p&gt;Also, the script assumes that your Django project is called &lt;em&gt;django_site.&lt;/em&gt; If your Django project has a different name (likely) you have to edit &lt;em&gt;cherrypy_django_service.py&lt;/em&gt; accordingly.&lt;/p&gt;
&lt;p&gt;If everything works you should see your Django site at the address:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;http://127.0.0.1:8090&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You can change the port in the &lt;em&gt;cherrypy_django_service.py&lt;/em&gt; script, together with other CherryPy options, which are however beyond the scope of this how-to.&lt;/p&gt;
&lt;h2 id="conclusions"&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;The method described here is IMHO a quite simple and straightforward way to deploy Django on Windows. I&amp;#8217;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.&lt;/p&gt;</content><category term="sysadmin"></category><category term="cherrypy"></category><category term="django"></category><category term="windows"></category></entry></feed>