Quickstart: Python + Phusion Passenger

This 5 minute tutorial teaches you to start your application in a Phusion Passenger server, in development mode. Feel what Passenger is and how it works.

Are you looking to deploy your app to production with Passenger, maybe in combination with Nginx or Apache? Take a look at the deployment tutorial.

Table of contents

  1. Loading...

What is Passenger?

Passenger is an open source web application server. It handles HTTP requests, manages processes and resources, and enables administration, monitoring and problem diagnosis.

Passenger is very easy to use, makes deploying in production much easier and is scalable. If you aren't already familiar with the benefits, you can learn more about them.

This tutorial doesn't go very deep into Passenger's concepts. Instead, it serves to get you started as quickly as possible. When you're done with this tutorial, we'll explain the concepts in more detail.

Install Passenger

Preparing the example application

In this tutorial we will use an example "hello world" application, written in Flask:

git clone https://github.com/phusion/passenger-python-flask-demo.git
cd passenger-python-flask-demo

Adding a WSGI file

What is WSGI?

Many Python web frameworks have a builtin web server that is activated by running the app. For example, you normally run a Flask app with the builtin Flask web server like this:

python app.py

Passenger doesn't work like that. It works with the Python WSGI standard. WSGI defines a standard interface for web applications, allowing any web application that implements WSGI to work with any server that supports WSGI. Pretty much any modern Python web framework implements WSGI. This includes Django, Flask and more.

The Passenger WSGI file

Create a WSGI file in the app's directory. Passenger expects it be called passenger_wsgi.py. The contents depends on the application and the web framework, but for our Flask example app, this is what you need to put in the file:

# passenger_wsgi.py
from app import MyApp as application

What just happened?

WSGI works by defining a callable object called application inside the WSGI file. This callable expects a request object, which the WSGI server provides; and returns a response object, which the WSGI server serializes and sends to the client.

Flask's application object, created by a MyApp = Flask(__name__) call, is a valid WSGI callable object. So our WSGI file is as simple as importing the Flask application object (MyApp) from app.py, and calling it application.

Other Python web frameworks require different code, but work with similar principles.

Running the server

You can now run your app with passenger start:

passenger start
# => ======= Phusion Passenger Standalone web server started =======
# => PID file: /Users/phusion/myapp/passenger.3000.pid
# => Log file: /Users/phusion/myapp/passenger.3000.log
# => Environment: development
# => Accessible via: http://0.0.0.0:3000/
# =>
# => You can stop Phusion Passenger Standalone by pressing Ctrl-C.
# => ===============================================================

As you can see in the output, Passenger is now serving your app on http://0.0.0.0:3000/. So if you go to that URL, you will should see your application:

curl http://0.0.0.0:3000/
# => your app's front page HTML

Stopping the server

There are two ways to stop the server. The first is by pressing Ctrl-C in the terminal.

passenger start
# => ...
# => (press Ctrl-C here)
# => Stopping web server... done!

The second way is by starting a seperate terminal, changing the working directory to your application, and running passenger stop:

cd /path-to-your-app
passenger stop

Conclusion

Achievement unlocked. Image taken from https://openclipart.org/detail/60109/award-symbol-by-sheikh_tuhin

Congratulations! You've passed this tutorial and seen Passenger in action. You can find the end result of this tutorial in the example application's git repository's end_result branch.

You may now be interested in intermediate-level tutorial.

Next step

You may now be interested in follow-up tutorials.

Basics

Learn what Passenger is, how it fits in the stack, its fundamental concepts and its basic usage. Focuses mainly on development rather than production.

Read this
Deployment

Learn how easy it is to setup a server and how to deploy your app to production. No worries, we walk you through from the beginning to the end.

Read this

...or go back to the Library index.