Debian Codegraph @

docs/source/deployment.rst

raw
1 .. _deployment:
2
3 Deploying Pecan in Production
4 =============================
5
6 There are a variety of ways to deploy a Pecan project to a production
7 environment. The following examples are meant to provide *direction*,
8 not explicit instruction; deployment is usually heavily dependent upon
9 the needs and goals of individual applications, so your mileage will
10 probably vary.
11
12 .. note::
13
14 While Pecan comes packaged with a simple server *for development use*
15 (:command:`pecan serve`), using a *production-ready* server similar to the ones
16 described in this document is **very highly encouraged**.
17
18 Installing Pecan
19 ----------------
20
21 A few popular options are available for installing Pecan in production
22 environments:
23
24 * Using `setuptools <https://pypi.python.org/pypi/setuptools>`_. Manage
25 Pecan as a dependency in your project's ``setup.py`` file so that it's
26 installed alongside your project (e.g., ``python
27 /path/to/project/setup.py install``). The default Pecan project
28 described in :ref:`quick_start` facilitates this by including Pecan as
29 a dependency for your project.
30
31 * Using `pip <http://www.pip-installer.org/>`_.
32 Use ``pip freeze`` and ``pip install`` to create and install from
33 a ``requirements.txt`` file for your project.
34
35 * Via the manual instructions found in :ref:`Installation`.
36
37 .. note::
38 Regardless of the route you choose, it's highly recommended that all
39 deployment installations be done in a Python `virtual environment
40 <http://www.virtualenv.org/>`_.
41
42 Disabling Debug Mode
43 --------------------
44
45 .. warning::
46 One of the most important steps to take before deploying a Pecan app
47 into production is to ensure that you have disabled **Debug Mode**, which
48 provides a development-oriented debugging environment for tracebacks
49 encountered at runtime. Failure to disable this development tool in your
50 production environment *will* result in serious security issues. In your
51 production configuration file, ensure that ``debug`` is set to ``False``.
52
53 ::
54
55 # myapp/production_config.py
56 app = {
57 ...
58 'debug': False
59 }
60
61 Pecan and WSGI
62 --------------
63
64 WSGI is a Python standard that describes a standard interface between servers
65 and an application. Any Pecan application is also known as a "WSGI
66 application" because it implements the WSGI interface, so any server that is
67 "WSGI compatible" may be used to serve your application. A few popular
68 examples are:
69
70 * `mod_wsgi <http://code.google.com/p/modwsgi/>`__
71 * `uWSGI <http://projects.unbit.it/uwsgi/>`__
72 * `Gunicorn <http://gunicorn.org/>`__
73 * `waitress <http://docs.pylonsproject.org/projects/waitress/en/latest/>`__
74 * `CherryPy <http://cherrypy.org/>`__
75
76 Generally speaking, the WSGI entry point to any Pecan application can be
77 generated using :func:`~pecan.deploy.deploy`::
78
79 from pecan.deploy import deploy
80 application = deploy('/path/to/some/app/config.py')
81
82 Considerations for Static Files
83 -------------------------------
84
85 Pecan comes with static file serving (e.g., CSS, Javascript, images)
86 middleware which is **not** recommended for use in production.
87
88 In production, Pecan doesn't serve media files itself; it leaves that job to
89 whichever web server you choose.
90
91 For serving static files in production, it's best to separate your concerns by
92 serving static files separately from your WSGI application (primarily for
93 performance reasons). There are several popular ways to accomplish this. Here
94 are two:
95
96 1. Set up a proxy server (such as `nginx <http://nginx.org/en>`__, `cherokee
97 <http://www.cherokee-project.com>`__, :ref:`cherrypy`, or `lighttpd
98 <http://www.lighttpd.net/>`__) to serve static files and proxy application
99 requests through to your WSGI application:
100
101 ::
102
103 <HTTP Client> ─── <Production/Proxy Server>, e.g., Apache, nginx, cherokee (0.0.0.0:80) ─── <Static Files>
104 │
105 ├── <WSGI Server> Instance e.g., mod_wsgi, Gunicorn, uWSGI (127.0.0.1:5000 or /tmp/some.sock)
106 ├── <WSGI Server> Instance e.g., mod_wsgi, Gunicorn, uWSGI (127.0.0.1:5001 or /tmp/some.sock)
107 ├── <WSGI Server> Instance e.g., mod_wsgi, Gunicorn, uWSGI (127.0.0.1:5002 or /tmp/some.sock)
108 └── <WSGI Server> Instance e.g., mod_wsgi, Gunicorn, uWSGI (127.0.0.1:5003 or /tmp/some.sock)
109
110
111 2. Serve static files via a separate service, virtual host, or CDN.
112
113 Common Recipes
114 --------------
115
116 Apache + mod_wsgi
117 +++++++++++++++++
118
119 `mod_wsgi <http://code.google.com/p/modwsgi/>`_ is a popular Apache
120 module which can be used to host any WSGI-compatible Python
121 application (including your Pecan application).
122
123 To get started, check out the `installation and configuration
124 documentation
125 <http://code.google.com/p/modwsgi/wiki/InstallationInstructions>`_ for
126 mod_wsgi.
127
128 For the sake of example, let's say that our project, ``simpleapp``, lives at
129 ``/var/www/simpleapp``, and that a `virtualenv <http://www.virtualenv.org>`_
130 has been created at ``/var/www/venv`` with any necessary dependencies installed
131 (including Pecan). Additionally, for security purposes, we've created a user,
132 ``user1``, and a group, ``group1`` to execute our application under.
133
134 The first step is to create a ``.wsgi`` file which mod_wsgi will use
135 as an entry point for your application::
136
137 # /var/www/simpleapp/app.wsgi
138 from pecan.deploy import deploy
139 application = deploy('/var/www/simpleapp/config.py')
140
141 Next, add Apache configuration for your application. Here's a simple
142 example::
143
144 <VirtualHost *>
145 ServerName example.com
146
147 WSGIDaemonProcess simpleapp user=user1 group=group1 threads=5 python-path=/var/www/venv/lib/python2.7/site-packages
148 WSGIScriptAlias / /var/www/simpleapp/app.wsgi
149
150 <Directory /var/www/simpleapp/>
151 WSGIProcessGroup simpleapp
152 WSGIApplicationGroup %{GLOBAL}
153 Order deny,allow
154 Allow from all
155 </Directory>
156 </VirtualHost>
157
158 For more instructions and examples of mounting WSGI applications using
159 mod_wsgi, consult the `mod_wsgi Documentation`_.
160
161 .. _mod_wsgi Documentation: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#Mounting_The_WSGI_Application
162
163 Finally, restart Apache and give it a try.
164
165 uWSGI
166 +++++
167
168 `uWSGI <http://projects.unbit.it/uwsgi/>`_ is a fast, self-healing and
169 developer/sysadmin-friendly application container server coded in pure C. It
170 uses the `uwsgi <http://projects.unbit.it/uwsgi/wiki/uwsgiProtocol>`__
171 protocol, but can speak other protocols as well (http, fastcgi...).
172
173 Running Pecan applications with uWSGI is a snap::
174
175 $ pip install uwsgi
176 $ pecan create simpleapp && cd simpleapp
177 $ python setup.py develop
178 $ uwsgi --http-socket :8080 --venv /path/to/virtualenv --pecan config.py
179
180 or using a Unix socket (that nginx, for example, could be configured to
181 `proxy to <http://projects.unbit.it/uwsgi/wiki/RunOnNginx>`_)::
182
183 $ uwsgi -s /tmp/uwsgi.sock --venv /path/to/virtualenv --pecan config.py
184
185 Gunicorn
186 ++++++++
187
188 `Gunicorn <http://gunicorn.org/>`__, or "Green Unicorn", is a WSGI HTTP Server for
189 UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. It
190 supports both eventlet and greenlet.
191
192 Running a Pecan application on Gunicorn is simple. Let's walk through it with
193 Pecan's default project::
194
195 $ pip install gunicorn
196 $ pecan create simpleapp && cd simpleapp
197 $ python setup.py develop
198 $ gunicorn_pecan config.py
199
200
201 .. _cherrypy:
202
203 CherryPy
204 ++++++++
205
206 `CherryPy <http://cherrypy.org/>`__ offers a pure Python HTTP/1.1-compliant WSGI
207 thread-pooled web server. It can support Pecan applications easily and even
208 serve static files like a production server would do.
209
210 The examples that follow are geared towards using CherryPy as the server in
211 charge of handling a Pecan app along with serving static files.
212
213 ::
214
215 $ pip install cherrypy
216 $ pecan create simpleapp && cd simpleapp
217 $ python setup.py develop
218
219 To run with CherryPy, the easiest approach is to create a script in the root of
220 the project (alongside ``setup.py``), so that we can describe how our example
221 application should be served. This is how the script (named ``run.py``) looks::
222
223 import os
224
225 import cherrypy
226 from cheroot import wsgi
227 from cheroot.wsgi import PathInfoDispatcher
228 from pecan.deploy import deploy
229
230 simpleapp_wsgi_app = deploy('/path/to/production_config.py')
231
232 public_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'public'))
233
234 # A dummy class for our Root object
235 # necessary for some CherryPy machinery
236 class Root(object):
237 pass
238
239 def make_static_config(static_dir_name):
240 """
241 All custom static configurations are set here, since most are common, it
242 makes sense to generate them just once.
243 """
244 static_path = os.path.join('/', static_dir_name)
245 path = os.path.join(public_path, static_dir_name)
246 configuration = {
247 static_path: {
248 'tools.staticdir.on': True,
249 'tools.staticdir.dir': path
250 }
251 }
252 return cherrypy.tree.mount(Root(), '/', config=configuration)
253
254 # Assuming your app has media on different paths, like 'css', and 'images'
255 application = PathInfoDispatcher({
256 '/': simpleapp_wsgi_app,
257 '/css': make_static_config('css'),
258 '/images': make_static_config('images')
259 })
260
261 server = wsgi.Server(('0.0.0.0', 8080), application, server_name='simpleapp')
262
263 try:
264 server.start()
265 except KeyboardInterrupt:
266 print("Terminating server...")
267 server.stop()
268
269 To start the server, simply call it with the Python executable::
270
271 $ python run.py