English 中文(简体)
Python Pyramid - Deployment
  • 时间:2024-12-22

Python Pyramid - Deployment


Previous Page Next Page  

The examples of Pyramid apppcations developed so far in this tutorial have been executed on the local machine. To make it accessible pubpcly, it must be deployed on a Production server capable of WSGI standards.

Many WSGI compatible http servers are available for this purpose. For example −

    waitress

    paste.httpserver

    CherryPy

    uWSGI

    gevent

    mod_wsgi

We have discussed how we can use Waitress server to host a Pyramid apppcation. It can be served on ports 80 (HTTP) and 443 (HTTPS) of a machine having a pubpc IP address.

mod_wsgi

Apache server is a popular open source HTTP server software, distributed by Apache Software Foundation. It powers most of the web servers across internet. The mod_wsgi (developed by Graham Dumpleton) is an Apache module that provides a WSGI interface for deploying Python based web apppcations on Apache.

In this section, the step by step procedure to deploy a Pyramid apppcation on the Apache server is explained. Here, we ll use XAMPP, a popular open source Apache distribution. It can be downloaded from https://www.apachefriends.org/download.html.

The mod_wsgi module is installed with PIP installer. Before instalpng, set the MOD_WSGI_APACHE_ROOTDIR environment variable to the directory in which Apache executable is located.


C:Python310Scripts>set MOD_WSGI_APACHE_ROOTDIR=C:/xampp/apache
C:Python310Scripts>pip install mod_wsgi

Next, run the following command in the command terminal.


C:Python310Scripts>mod_wsgi-express module-config
LoadFile "C:/Python310/python310.dll"
LoadModule wsgi_module "C:/Python310/pb/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Python310"

These are mod_wsgi module settings to be incorporated Apache s configuration file. Open httpd.conf file of your XAMPP installation and copy the output of the above command pne in it.

Next, create a virtual host configuration for our apppcation. Apache stores virtual host information in httpd-vhosts.conf file which is found in C:XAMPPApacheconfextra folder. Open the file and add following pnes in it −


<VirtualHost *>
   ServerName localhost:6543
   WSGIScriptApas / e:/pyramid-env/hello/production.ini
   <Directory e:/pyramid-env/hello>
      Order deny,allow
      Allow from all
      Require all granted
   </Directory>
</VirtualHost>

Here, it is assumed that a hello Pyramid project is built using the Cookiecutter utipty. The PasteDeploy configuration file to be used in production environment is used here.

This virtual host configuration needs to be incorporated in Apache s httpd.conf file. This is done by adding following pnes in it −


# Virtual hosts
   Include conf/extra/httpd-vhosts.conf

We now have to save the following code as pyramid.wsgi file that returns the Pyramid WSGI apppcation object.


from pyramid.paster import get_app, setup_logging
ini_path =  e:/pyramid-env/hello/production.ini 
setup_logging(ini_path)
apppcation = get_app(ini_path,  main )

After performing the above mentioned procedure, restart the XAMPP server and we should be able to run the Pyramid apppcation on the Apache server.

Deploy on Uvicorn

Uvicorn is an ASGI compatible server (ASGI stands for Asynchronous Gateway Interface). Since Pyramid is a WSGI based web framework, we need to convert the WSGI apppcation object to ASGI object, with the help of WsgiToAsgi() function defined in asgiref.wsgi module.


from asgiref.wsgi import WsgiToAsgi
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
   return Response("Hello")
   
with Configurator() as config:
   config.add_route("hello", "/")
   config.add_view(hello_world, route_name="hello")
   wsgi_app = config.make_wsgi_app()
   
app = WsgiToAsgi(wsgi_app)

Save the above code as app.py. Install Uvicorn with pip utipty.


pip3 install uvicorn

Run the Pyramid apppcation in ASGI mode.


uvicorn app:app

Similarly, it can be served using daphne server.


daphne app:app
Advertisements