Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Arbesko files application, built in Flask & Python
Python
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
files
.gitignore
Procfile
README.md
requirements.txt
runserver.py

README.md

Arbesko Files Application

This is in action right now on http://files.arbesko.com

It's an incredibly simple front-end to a directory of files. Take a look at util/default.py for all the various settings.

Deployment

There are a few prerequisites for deployment. The below is based off of a vanilla Ubuntu 12.04 installation.

  • nginx
  • virtualenv

Step 0, deploy the project

Check out the git repository and move it into your desired production location. For our use, this is /var/www/files

Next, after installing virtualenv and virtualenvwrapper ensure that you have created a virtualenv and installed the required dependencies:

mkvirtualenv files.arbesko.com
cd /var/www/files
pip install -r requirements.txt

This assumes that the git repository root is at /var/www/files, the location of requirements.txt

Step 1, nginx

After installing a recent build of nginx, you'll need to configure it. Below is a sample configuration:

# Arbesko Files App
# Flask + uWSGI
# files.arbesko.com

server {
    listen 80 default;
    server_name files.arbesko.com

    root /home/arbesko/files/htdocs;

    charset utf-8;

    location /static {
        alias /var/www/files.arbesko.com/files/static;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/files.arbesko.com.sock;
    }

    location /download {
        alias /home/arbesko/files/htdocs;
        internal;
    }
}

In production, this is is saved as /etc/nginx/sites-available/files.arbesko.com

Which you'll then need to symlink into sites-enabled and restart nginx in order to take effect:

cd /etc/nginx/sites-enabled
ln -s ../sites-available/files.arbesko.com
sudo service nginx restart

This assumes that we have uwsgi running on the socket established at /tmp/files.arbesko.com.sock which leads us to our next configuration step.

Step 2, uWSGI

Note: uWSGI will be installed via the pip install -r requirements.txt command. If that does not go well, it's most likely due to missing dependencies. Those are installed via the following command on an Ubuntu/Debian box:

apt-get install build-essential python-dev libxml2-dev

The next step will involve configuring uWSGI and deploying it with supervisor to ensure that we can start and stop it. Also, supervisor will help us by automatically starting uWSGI when the server boots.

Install Supervisor

sudo apt-get install supervisor

Next, we'll create the config file to manage our uWSGI processes for this Files application:

cd /etc/supervisor/conf.d
vim files.arbesko.com.conf

We're now inside of vim, where we can enter our configuration. Copy and paste this into that file as a starting point:

[program:files.arbesko.com]
command=/home/michael/.virtualenvs/files.arbesko.com/bin/uwsgi
    -s /tmp/files.arbesko.com.sock
    -w files:app
    -M
    --processes 4
    --max-requests=200
    --uid=www-data
    --chmod-socket
directory = /var/www/files.arbesko.com
environment = ARB_FILES_ROOT='/home/arbesko/files/htdocs'
user = www-data
autostart = true
autorestart = true
stdout_logfile = /var/log/arbesko/files.arbesko.com.log
redirect_stderr = true

The above config is mostly self-explanatory. We point to the binary for uWSGI that was added inside of our virtualenv.

The uWSGI parameters are of note:

  • -s /tmp/files.arbesko.com.sock this determines the socket which the app server operates on
  • -w files:app this determines the wsgi application that it will look for. In our case, the code for this can be seen by viewing __init__.py in the root of the project.
  • -M tells uWSGI to operate in master mode
  • --processes 4 will run four processes, to increase concurrency
  • --max-requests=200 this is not required, but will kill a process after 200 requests to ensure any memory leakage is nipped in the bud.
  • --uid=www-data this runs the app servers as our www-data user
  • --chmod-socket this will make sure that the socket is owned by www-data as well, which allows nginx to communicate on it without any permissiosn errors.

Also of note is that we are setting the ARB_FILES_ROOT environment variable which determines where to serve files from. This is also configurable from inside of the util/defaults.py file in our app, but the ENV variable will take precedence if it is defined.

Start Her Up

At this point we can fire it all up and should see things working for us.

sudo /etc/init.d/supervisor start

If all goes well, you'll now see the files app running on your server. Try hitting it via your server ip or the domain defined.

Something went wrong with that request. Please try again.