For the lazy, we have a shell script that will do all this for you on GitHub!
While trying to configure our new Amazon EC2 instance, it was a little too cumbersome and somewhat poorly documented how to setup Flask with uWSGI on nginx. While there were a few great writeups on how to configure this, they fell short to get a working configuration on Amazon’s Linux AMI, which we will try to describe here.
After creating a new EC2 instance and logging in, we will need to install nginx, uWSGI and required libraries.
|
1 2 3 |
sudo yum install python26 python26-devel make automake nginx gcc gcc-c++ python-setuptools git sudo easy_install pip sudo pip install uwsgi virtualenv |
Now we need a Flask project! Let’s just grab the latest simple repo from GitHub:
|
1 2 |
cd ~ git clone https://github.com/orf/simple.git myblog |
Alternatively, you can fork the project on GitHub and pull that one–this is probably the desirable option.
We will quickly setup a virtualenv in our new simple repository.
|
1 2 3 4 |
cd ~/myblog virtualenv venv . venv/bin/activate pip install Flask Flask-SQLAlchemy markdown |
Now let’s give it a test, shall we?
|
1 2 |
(venv)[ec2-user@ip-12-34-56-78 myblog]$ python simple.py * Running on http://127.0.0.1:5000/ |
Looking good, let’s move it real quick to where we want it and fix the permissions to our liking.
|
1 2 3 4 |
cd ~ sudo mkdir -p /var/www/run sudo cp -R myblog /var/www/blog sudo chown -R nginx:nginx /var/www/ |
Next, let’s configure uWSGI. Following Tyler’s writeup, let’s make some directories.
|
1 2 3 |
sudo mkdir -p /var/log/uwsgi sudo mkdir -p /etc/uwsgi/apps-available sudo mkdir -p /etc/uwsgi/apps-enabled |
Now let’s create the uWSGI configuration file with:sudo vim /etc/init/uwsgi.confWith the following content:
|
1 2 3 4 5 6 7 8 9 |
description "uWSGI" start on runlevel [2345] stop on runlevel [06] respawn env UWSGI=/usr/bin/uwsgi env LOGTO=/var/log/uwsgi/emperor.log exec $UWSGI --master --emperor /etc/uwsgi/apps-enabled --die-on-term --uid nginx --gid nginx --logto $LOGTO |
A few things to note here, we are setting up the general uWSGI emperor log and telling it to run as the nginx user for permission purposes.
With uWSGI configured, we can start it up withsudo start uwsgi
Now we can begin to configure our simple fork which runs this blog by creating a new file with:
|
1 |
sudo vim /etc/uwsgi/apps-available/blog.ini |
And we will configure it with the following content:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[uwsgi] # Variables base = /var/www/blog app = simple # Generic Config plugins = http,python home = %(base)/venv pythonpath = %(base) socket = /var/www/run/%n.sock module = %(app) callable = app logto = /var/log/uwsgi/%n.log |
Now we need to link our configuration file to the enabled sites folder:
|
1 |
sudo ln -s /etc/uwsgi/apps-available/blog.ini /etc/uwsgi/apps-enabled/blog.ini |
Finally, we can configure nginx to serve up our new blog.
|
1 |
sudo vim /etc/nginx/conf.d/default.conf |
And we will configure nginx to serve up content from our uWSGI emperor like so:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
server { listen 80; server_name blog; server_name blog.iqyax.org; root /var/www/blog; location /static/ { alias /var/www/blog/static/; expires 30d; access_log off; } location / { include uwsgi_params; uwsgi_pass unix:/var/www/run/blog.sock; } } |
Save the file and let’s fire up nginx, we are ready for launch!
|
1 |
sudo service nginx start |
Hope that you have found this post helpful. Later we hope to describe how to automate this on Amazon EC2 for automatic scaling of your server fleet.
