There are 3 possible deployment scenarios for windows:
Both method one and two are quite similar to running the development server except that debugging info is turned off and you want to run the process as a windows service. Running as a windows service depends on the PyWin32 project. You will need to download the pre-built binary that matches your version of Python.
You can install directly into the virtualenv if you run easy_install on the downloaded installer. For example:
Since the web server for CherryPy has good Windows support, is available for Python 2 and 3 and can be gracefully started and stopped on demand from the service, we’ll use that as the web server. You could also substitute another web server, like the one from Twisted.
To install cherrypy run:
pip install cherrypy
Create a new file called pyramidsvc.py file with the following code to define your service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
# uncomment the next import line to get print to show up or see early # exceptions if there are errors then run # python -m win32traceutil # to see the output #import win32traceutil import win32serviceutil PORT_TO_BIND = 80 CONFIG_FILE = 'production.ini' SERVER_NAME = 'www.pyramid.example' SERVICE_NAME = "PyramidWebService" SERVICE_DISPLAY_NAME = "Pyramid Web Service" SERVICE_DESCRIPTION = """This will be displayed as a description \ of the serivice in the Services snap-in for the Microsoft \ Management Console.""" class PyWebService(win32serviceutil.ServiceFramework): """Python Web Service.""" _svc_name_ = SERVICE_NAME _svc_display_name_ = SERVICE_DISPLAY_NAME _svc_deps_ = None # sequence of service names on which this depends # Only exists on Windows 2000 or later, ignored on windows NT _svc_description_ = SERVICE_DESCRIPTION def SvcDoRun(self): from cherrypy import wsgiserver from pyramid.paster import get_app import os, sys path = os.path.dirname(os.path.abspath(__file__)) os.chdir(path) app = get_app(CONFIG_FILE) self.server = wsgiserver.CherryPyWSGIServer( ('0.0.0.0', PORT_TO_BIND), app, server_name=SERVER_NAME) self.server.start() def SvcStop(self): self.server.stop() if __name__ == '__main__': win32serviceutil.HandleCommandLine(PyWebService)
The if __name__ == '__main__' block provides an interface to register the Service. You can register the service with the system by running:
python pyramidsvc.py install
Your service is now ready to start, you can do this through the normal Service snap-in for the Microsoft Management Console or by running:
python pyramidsvc.py start
If you want your service to start automatically you can run:
python pyramidsvc.py update --start=auto
If you want to run many Pyramid applications on the same machine you will need to run each of them on a different port and in a separate Service. If you want to be able to access each one through a different host name on port 80 then you will need to run another web server (IIS or Apache) up front and proxy back to the appropriate service.
There are several options available for reverse proxy with IIS. Versions starting with IIS 7 you can install and use the Application Request Routing if you want to use a Microsoft provided solution. Another option is one of the several solutions from Helicon Tech. The Helicon Ape is available without cost for up to 3 sites.
If you aren’t already using IIS Apache is available for Windows and works well. There are many reverse proxy tutorials available for Apache and they are equally applicable to Windows.
Turn on windows feature for IIS.
Control panel -> “Turn windows features on off” and select
Go to Internet Information Services Manager and add website
Create a file install_website.py and place it in your pyramid project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# path to your site packages in your environment # needs to be put in here import site site.addsitedir('/path/to/your/site-packages') # this is used for debugging # after everything was installed and is ready to meka a http request # run this from the command line: # python -m python -m win32traceutil # It will give you debug output from this script # (remove the 3 lines for production use) import sys if hasattr(sys, "isapidllhandle"): import win32traceutil # this is for setting up a path to a temporary # directory for egg cache. import os os.environ['PYTHON_EGG_CACHE'] = '/path/to/writable/dir' # The entry point for the ISAPI extension. def __ExtensionFactory__(): from paste.deploy import loadapp import isapi_wsgi from logging.config import fileConfig appdir = '/path/to/your/pyramid/project' configfile = 'production.ini' con = appdir + configfile fileConfig(con) application = loadapp('config:' + configfile, relative_to=appdir) return isapi_wsgi.ISAPIThreadPoolHandler(application) # ISAPI installation if __name__ == '__main__': from isapi.install import ISAPIParameters, ScriptMapParams, VirtualDirParameters, HandleCommandLine params = ISAPIParameters() sm = [ ScriptMapParams(Extension="*", Flags=0) ] # if name = "/" then it will install on root # if any other name then it will install on virtual host for that name vd = VirtualDirParameters(Name="/", Description="Description of your proj", ScriptMaps=sm, ScriptMapUpdate="replace" ) params.VirtualDirs = [vd] HandleCommandLine(params)
Activate your virtual env and run the stript:
python install_website.py install --server=<name of your website>
Restart your website from IIS.