LECTURE 15 Web Servers. DEPLOYMENT So, we’ve created a little web application which can let users search for information about a country they may be visiting.

Slides:



Advertisements
Similar presentations
Website Development with PHP and MySQL Introduction.
Advertisements

Fundamentals of Python: From First Programs Through Data Structures
Linux Operations and Administration
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
Deploying Ruby on Rails How to make your application actually serve Dan Buettner 18 Oct 2007.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Putting What We Learned Into Context – WSGI and Web Frameworks A290/A590, Fall /16/2014.
HOW WEB SERVER WORKS? By- PUSHPENDU MONDAL RAJAT CHAUHAN RAHUL YADAV RANJIT MEENA RAHUL TYAGI.
1 Apache. 2 Module - Apache ♦ Overview This module focuses on configuring and customizing Apache web server. Apache is a commonly used Hypertext Transfer.
Website Design Lecture 1. Outline Introduction to the module Outline of the Assessment Schedule Lecture Static XHTML, client side and server side Why.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
First Indico Workshop WEB FRAMEWORKS Adrian Mönnich May 2013 CERN.
Apache Web Server Quick and Dirty for AfNOG 2015 (Originally by Joel Jaeggli for AfNOG 2007) ‏
Apache JMeter By Lamiya Qasim. Apache JMeter Tool for load test functional behavior and measure performance. Questions: Does JMeter offers support for.
1 Session 1: Introduction to PHP & MySQL iNET Academy Open Source Web Development.
NJIT 1 Apache Tomcat (Version 6.0) THETOPPERSWAY.COM.
LECTURE 11 Networking Part 2. NETWORKING IN PYTHON At this point, we know how to write a simple TCP client and simple TCP server using Python’s raw sockets.
LECTURE 14 Web Frameworks. WEB DEVELOPMENT CONTINUED Web frameworks are collections of packages or modules which allow developers to write web applications.
Chapter 4 Request and Response. Servlets are controlled by the container.
 Cloud Computing technology basics Platform Evolution Advantages  Microsoft Windows Azure technology basics Windows Azure – A Lap around the platform.
Windows Server 2003 { First Steps and Administration} Benedikt Riedel MCSE + Messaging
L.A.M.P. İlker Korkmaz & Kaya Oğuz CS 350. Why cover a lecture on LAMP? ● Job Opportunities – There are many hosting companies offering LAMP as a web.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Lecture 15 Web Servers.
Data Virtualization Tutorial… SSL with CIS Web Data Sources
Installing TMG & Choosing a Client Type
Essentials of UrbanCode Deploy v6.1 QQ147
Node.Js Server Side Javascript
Chapter Objectives In this chapter, you will learn:
Apache web server Quick overview.
StackStorm: DevOps to ChatOps
Shared Services with Spotfire
Servlet Fudamentals.
Self Healing and Dynamic Construction Framework:
Data Virtualization Tutorial… CORS and CIS
E-commerce | WWW World Wide Web - Concepts
E-commerce | WWW World Wide Web - Concepts
IT Atoumation / Conf. Mgmt...
By Dr. Kodge Bheemashankar G
PHP / MySQL Introduction
Discord Bot Senior Project
Node.Js Server Side Javascript
MVC Framework, in general.
X in [Integration, Delivery, Deployment]
CSE 451: Operating Systems Winter 2006 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Introduction to Ansible
CSE 451: Operating Systems Autumn 2003 Lecture 16 RPC
CSE 451: Operating Systems Winter 2007 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Chapter 40 Remote Method Invocation
Objectives In this lesson you will learn about: Need for servlets
Module P3 Practical: Building a webapp in nodejs and
CSE 451: Operating Systems Winter 2004 Module 19 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Spring 2012 Module 22 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Autumn 2009 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Chapter 46 Remote Method Invocation
Lecture 5: Functions and Parameters
Planning and Storyboarding a Web Site
AbbottLink™ - IP Address Overview
Chapter 46 Remote Method Invocation
CSE 451: Operating Systems Autumn 2010 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Winter 2003 Lecture 16 RPC
Web Servers (IIS and Apache)
CS/COE 1520 Jarrett Billingsley
CSE 451: Operating Systems Messaging and Remote Procedure Call (RPC)
IIS and .NET Security Application Pools Pamella Smith June 18, 2009.
Web Application Development Using PHP
Concepts in ASP.NET Core App
Presentation transcript:

LECTURE 15 Web Servers

DEPLOYMENT So, we’ve created a little web application which can let users search for information about a country they may be visiting. The steps we’ve taken so far: 1.Writing the backend functionality of our website – factbook_scraper.py 2.Using Flask to write our web application, which calls our factbook_scraper module. We’re going to be terrible software engineers for a moment and assume our web application is ready to be deployed.

DEPLOYMENT Even though we’ve been using Flask’s development server to serve our application, this should not be used publicly. Instead, we should set up a production environment. We could just pick a platform-as-a-service option such as OpenShift, Gondor, CleverCloud, Heroku, etc. That way, we just need to worry about our application and not the server details. But we’re not going to do that! Let’s create our own server (assuming we obtained a machine we could use as the server).

WSGI Before we begin setting up our server, let’s discuss WSGI. WSGI is the Web Server Gateway Interface. WSGI is a Python standard, described in PEP (Python Enhancement Proposals) 3333, that details how web servers should communicate with web applications written in Python. It is not a module or server or package. It’s just a specification.

WSGI Before WSGI, a traditional web server did not understand or have any way to run Python applications. Eventually, an Apache module called mod_python was created to execute arbitrary Python code. In the late 1990s and early 2000s, most Python web applications were run by Apache configured with mod_python (but also some other custom APIs for specific web servers). But mod_python was just a small project developed by one person – eventually, a standard method for executing Python code for web applications was necessary.

WSGI Enter WSGI! WSGI specifies a uniform standard for invoking Python modules within web applications. There are two sides to the WSGI specification: “server” and “application”. The server side provides environment information and a callback function to the application side. The application processes the request, and returns the response to the server side using the callback function it was provided. If a web framework is WSGI compliant, its applications should be servable by any WSGI-compliant server. This means we can choose our framework and server independently.

WSGI WSGI-compliant frameworks include Django, Flask, Pylons, Pyramid, etc. (so many choices!) WSGI-compliant web servers include Gunicorn, Tornado, Waitress, etc. We also have the option of embedding Python into a non-Python based web server by using mod_wsgi (Apache), ModPython (Apache), Nginx WSGI (Nginx), etc.

DEPLOYMENT We’re going to deploy our little website using Gunicorn fronted by Nginx. Web Browser Web Server (Nginx) WSGI Server (Gunicorn) generate search.html search.html sends search.html, which requires style.css GET style.css style.css

DEPLOYMENT Why use Nginx? The simple answer is that even though we need a server that can execute Python (WSGI server), there are features of a non-Python based server that we’d like to utilize: we can distribute requests through Nginx to multiple gunicorn instances, we can perform high-performance static file serving, efficiently handle high connection levels, etc. It’s better to use Nginx as a reverse proxy so we can take advantage of its features as a powerful HTTP server, but we’re really serving dynamic content from Gunicorn.

DEPLOYMENT An elegant pictorial representation of our setup (from realpython.com)

GETTING STARTED The very first thing we need to do is install nginx and gunicorn of course. This class will temporarily turn into a Unix class for a minute. $ sudo apt-get install nginx-full gunicorn

CONFIGURING NGINX This is the easy part. Start up nginx. $ sudo /etc/init.d/nginx start Now, /etc/nginx/sites-enabled/ contains symlinks to configuration files in /etc/nginx/sites- available/. Configuration files in sites-available are not active until they are enabled. We’re going to remove the default symlink, create a new site configuration file for our flask application, and create a link for it in sites-enabled.

CONFIGURING NGINX $ sudo rm /etc/nginx/sites-enabled/default $ sudo touch /etc/nginx/sites-available/flask_project $ sudo ln -s /etc/nginx/sites-available/flask_project /etc/nginx/sites-enabled/flask_project The first two are self explanatory, but the last command creates a symbolic link between the two file arguments. Essentially, the sites-enabled file will point sites-available file. Note: be careful when copying/pasting commands. Some characters do not transfer exactly as intended. If all else fails, write the command out manually.

CONFIGURING NGINX Now, we can actually throw some stuff into the configuration file for our Flask project. server { location / { proxy_pass } location /static { alias /path/to/travel_app/app/static/; } /etc/nginx/sites-enabled/flask_project When an HTTP request hits the ‘/’ endpoint, reverse proxy to port 8000 on localhost, where gunicorn will be listening.

CONFIGURING NGINX Now, we can actually throw some stuff into the configuration file for our Flask project. server { location / { proxy_pass } location /static { alias /path/to/travel_app/app/static/; } /etc/nginx/sites-enabled/flask_project Do not route requests for static files through the WSGI server. Serve directly.

CONFIGURING NGINX Now, we can actually throw some stuff into the configuration file for our Flask project. server { location / { proxy_pass } location /static { alias /path/to/travel_app/app/static/; } /etc/nginx/sites-enabled/flask_project Nginx will bind to localhost:80 by default.

CONFIGURING NGINX AND STARTING GUNICORN And restart nginx. Now start up gunicorn inside of the Flask app directory. What we’re doing is telling gunicorn to serve app.t_app on localhost, port $ sudo /etc/init.d/nginx restart Restarting nginx: nginx. travel_app$ gunicorn app:t_app –b localhost:8000

CONFIGURING NGINX AND STARTING GUNICORN And restart nginx. Now start up gunicorn inside of the Flask app directory. However, the gunicorn command defaults to the host version – we want to use our virtual environment. As long as we pip install gunicorn into travel_env, we can specifically refer to this version. $ sudo /etc/init.d/nginx restart Restarting nginx: nginx. travel_app$ travel_env/bin/gunicorn app:t_app –b localhost:8000

CHECKING OUT OUR SITE Yay! Let’s search for some information.

CHECKING OUT OUR SITE More yay! Now, let’s extend our website to request information by keyword, rather than country.

CHECKING OUT OUR SITE Noooooo! What’s happening!?

CONFIGURING GUNICORN 502 Bad Gateway The nginx server was acting as a gateway or proxy and received an invalid response from the upstream server. So, it’s Gunicorn’s fault. Let’s check it out. Gunicorn is nice enough to shout the following at us: [534] [CRITICAL] WORKER TIMEOUT (pid:539)

CONFIGURING GUNICORN Bottom line: it takes too long to scrape the Factbook for every country and Gunicorn is timing out while waiting for the response to be created. There are some solutions we can put together script-side (notably, threading) but let’s take a step back and learn about how Gunicorn works.

GUNICORN Gunicorn is a Python-based WSGI-compliant HTTP server for Unix (obviously). Gunicorn is based on the pre-fork worker model. There is a central master process which manages a set of worker processes responsible for handling all requests and responses. There’s only one type of master process and it’s just a reactor loop that listens for signals and responds accordingly. There are, however, a bunch of different worker types.

GUNICORN Sync Workers Default. Handles a single request at a time. Async Workers Called eventlets (requires the eventlet package). Cooperative multi-threading – threads explicitly yield back control after some time. Tornado Workers Used for writing applications using the Tornado framework.

GUNICORN Sync workers are alright for most simple web applications that don’t require a lot of resources but they’re easy to overwhelm, that’s why we use Nginx upfront. Async workers are required for resource- and time-intensive applications. In our case, our application makes a long blocking call to scrape the Factbook. So let’s change our Gunicorn configuration. Previously, we just used the default config but now we’ll make our own.

CONFIGURING GUNICORN Let’s create a config file which can be passed to the gunicorn command at start-up. travel_app/gunicorn/gunicorn.py import os bind = " :8000" workers = 2 worker_class = 'eventlet' logfile = "logs/error.log" loglevel = 'debug' Async Workers According to the Gunicorn docs, 4-12 workers should be able to handle hundreds of thousands of requests per second.

SERVING THE APPLICATION Now, we can issue the following to start up the Gunicorn server: $ travel_env/bin/gunicorn app:t_app –c gunicorn/gunicorn.py Note on requirements: you may need to install python-dev, greenlet and eventlet. $ sudo apt-get install python-dev $ pip install greenlet $ pip install eventlet

CHECKING OUT OUR SITE…AGAIN Yay!!!

CONFIGURATION OPTIONS So that’s basic deployment with Gunicorn and Nginx. Some other considerations: Explore the Gunicorn and nginx config file settings to see which setup options are available to you. Create a supervisor so that you don’t have to manually restart your server when changes are made ( Create git/mercurial hooks to automate application updates. Create a fabfile with Fabric to automate the deployment process.