This is Part 2 of my tutorial for Flask Webserver Deployment. In the last section we acquired our own personal Linux server. Now we want to get connected to it and run our first Flask Test Application.
Content of this Tutorial
- Part 1: Choosing the right webservice provider
- Part 2: Flask Development Server
- Part 3: Gunicorn WSGI Server
- Part 4: Nginx Proxy Server
Preparing the Server
Get connected
Via the operating panel of our service provider we chose to use Linux Debian "Jessie" as our operating system. Now we make contact by using the Secure Shell (SSH). On a unix-like OS we can connect to the VServer by calling the following command:
$ ssh root@hostname_or_ip
We want to connect as root so we are able to modify our installed software. The server will answer by asking for the password and then gracefully present us its shell. The first thing we want to do is update our installed software packages:
$ apt-get update ... $ apt-get upgrade ...
! You need root privileges for these commands. If you are not logged in as root use sudo here.
Install Python and git
Now all installed packages are up-to-date and we can start installing our framework. As I am a Python3 kind of person I will describe how to deploy a Python3 Flask Application. There is still an ongoing debate about whether to use Python2 or Python3. Just for the record: I prefer Python3 very much over Python2 mainly because all Strings are unicode now and I don' t have to worry about encoding anymore. Also most packages now support Python3 and I didn' t have any compatibility issues with Flask or any of its extension packages so far. If you chose Python2 just install the equivalent packages. So what do we need: git: The "flasky" application is hosted on github so we need git to deploy it to our server. python3 and python3-pip for quick and easy package installation
$ apt-get install -y git python3 python3-pip ...
Add a new user
We don't want our application to run with root privileges. So we create a new user apps that will run it.
$ adduser apps Adding user `apps' ... Adding new group `apps' (1001) ... Adding new user `apps' (1001) with group `apps' ... Creating home directory `/home/apps' ... Copying files from `/etc/skel' ... Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for apps Enter the new value, or press ENTER for the default Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n]
This command creates the new user apps in the new user group apps. That's what we want. We are asked to choose a password and confirm it. Afterwards we are asked for some further information. We take take default by pressing ENTER.
Create the Flask Test Application
Let's change into the new users home directory and start up our favorite editor.
$ cd /home/apps $ vim flasktest.py
We create our sample application in a new file called flasktest.py.
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello World' if __name__ == '__main__': app.run(host='0.0.0.0', port=80)
I know this is a bit boring but it serves our purpose.
Install Python Packages in a Virtual Environment
A Flask application typically has a number of Python packages it depends on. Normally that is of course Flask and some Flask extensions like Flask-Shell or Flask-SQLAlchemy. It is best practice to keep those package dependencies in a requirements file named requirements.txt. Our requirements.txt will just contain the Flask package so it looks like:
flask
We want our Python application to run in a virtual environment. This way it is isolated from the global Python packages and is easy to run multiple Flask application on one server. For this we install the virtualenv package.
$ pip3 install virtualenv
We create a new virtual environment and activate it. Then we install our required packages.
$ virtualenv venv ... $ source venv/bin/activate ... (venv) $ pip install -r requirements.txt
Run the development server
As a first shot we want to use the Flask development webserver to serve our application. It is not a performant webserver and it should not be used for production. But we just want to know if all packages are installed correctly and the application is running. We start our Webserver by calling:
$ sudo python flasktest.py
We need root privileges to bind our server to port 80, so I use sudo here. Alright, we got it working. You can access the incredible 'Hello World' via your browser by calling your server ip-address. In my next post we will serve our application with a much more performant application server called gunicorn.