article← all posts
Getting a Flask website up and running in Ubuntu
This is a guide to get a Flask website up and running on Ubuntu 12.04 LTS using nginx and uWSGI. There are many routes to take when it comes to Python on the web; this just is my personal favorite. Some people enjoy configuring servers, while others view it as a chore. Regardless, this guide should get you up, running, and ready to make something awesome in no time!
Installation
nginx
To install nginx you first need to add the repository. Add the following to /etc/apt/sources.list.d/nginx-lucid.list :
You will also want to add the gpg key to the apt keyring:
Finally, to install nginx, run:
unknown nodeuWSGI
You can use pip to install the latest version of uWSGI by doing the following:
unknown nodeTo configure uWSGI to run as a daemon, you first want to create a separate uwsgi user:
You then want to create an upstart configuration file, to run uWSGI in the background. Add the following to /etc/init/uwsgi.conf :
To set up logging, you can add a logrotate configuration file at /etc/logrotate.d/uwsgi:
and to prime the log file, you can run:
unknown nodeIt is okay if there is an error with the postrotate script. uWSGI cannot be restarted because it is not currently running.
virtualenv
Using virtualenv is a good idea because it compartmentalizes the environment of your application. To install virtualenv, you can use pip:
unknown nodeConfiguration
Flask
Now to set up the website! In this case, the website is going to be called 'helloworld' and it is going to be set up in /srv/www/helloworld. Feel free to change it, just adjust these instructions accordingly.
The first step is to create the directory:
unknown nodeand a subdirectory for static files, to be hosted by nginx:
unknown nodeThe next step is to create a virtual environment for the application to run in:
unknown nodeand to install Flask in that environment:
unknown nodeNow that that is done, let's create a sample 'Hello World' application, in application.py:
The permissions of the directory now have to be configured. uWSGI needs read permission to read the contents of the scripts, and write permission to save compiled python files:
unknown nodeNginx
The final step is to configure nginx. First, remove the default configuration file:
unknown nodeNow, add the helloworld configuration file at /etc/nginx/conf.d/helloworld.conf:
Running it
Now that you have successfully configured your website, you can run it using:
unknown nodeExtensions
Flask
Flask is fantastic because it is very extensible. Check out the documentation to get started.
More Websites!
It is really easy to add more websites to this configuration. Simply create a new application directory, set it up, and create a second nginx configuration. uWSGI is configured to be in virtualhost mode, so it can handle multiple websites at once.
Portability
You can also use uWSGI as an http server, for testing purposes. If you have a copy of your website checked out in the current directory, you can run
unknown nodeand uWSGI will create a HTTP server hosting your application on port 9090. This is incredibly useful for development.
Discuss on Hacker News here.
Discussed on Hacker News — 64 points, 22 comments.