Wednesday 6 October 2010

Bottle & Supervisord

Bottle

I made a url shortner using bottle at some point though.

Supervisor Conf

Add the following to your /etc/supervisord.conf file. I’ve changed the paths because yours will be different.

 [program:yourappname] command=/path/to/python /path/to/bottleapp/yourapp.py directory=/path/to/bottleapp/ user=yourusername autostart=true autorestart=true redirect_stderr=true  

The first path in the command value refer to your python path or your virtualenv python path. Once that’s done you just have to start & stop supervisord.

 $ killall supervisord $ supervisord  

A quick look into the supervisorctl program should show that the new service is running

 $ supervisorctl jamiecurle                       RUNNING    pid 12374, uptime 0:07:16 jmcrl                            RUNNING    pid 12375, uptime 0:07:16 supervisor>   

Splendid, now quit out of supervisorctl and we’re ready to set up nginx

Nginx

For those that haven’t heard of Nginx

You could use any webserver for this step, as all we’re going to be doing is proxying requests from port 80 to our application port, which defaults to 8080 for bottle.

If you’re using Debian or Debian variant such as Ubuntu and you’ve installed Nginx using apt, then you’ll have a funky config file layout like apache. All we’re going to do is add a config file for a virtualhost. Because I’m using Ubuntu, I’m going to make that file in /etc/nginx/sites-available. You’ll probably have to sudo this because you might not shouldn’t have permission to write into the /etc/ directory. You should call your file something nice and semantic such as ‘mydomain.com’

 $ sudo touch /etc/nginx/sites-available/mydomain.com 

Now it’s just a case of supplying the correct config information for our virtual host and restarting Nginx.

 server{     server_name mydomain.com;     root /path/to/root/;     location / {         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;         proxy_set_header Host $http_host;         proxy_redirect off;         proxy_pass http://127.0.0.1:8080;         break;       } }  

The above code is telling nginx x that for all requests for jmcrl.com to forward them to http://127.0.0.1:8080- which is where we’ve configured supervisord to run our bottle app.

Debian / Unbuntu Jiggery Pokery

As I mentioned above on Debian & Debian variants the layout for nginx config is like that of Apache, with one difference - there’s no command (a2ensite) for enabling and deactivating your available sites.

Fear not, all the command done (or appeared to do that I noticed) was create or remove symlinks between sites-available and site-enabled. So if your using Debian or a Debian variant then you’ll have to do the following step to get Nginx to pick up your config file.

 $ sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com 

Restart Nginx

Almost done now, we just need to restart Nginx.

 $ sudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com 

Once this is done, visit your url and behold. Or in my case, be redirected as this is the default behavior for bottle-shorturl when no id is supplied.

Bottle Logo

I wanted a nice Bottle logo for this post and I couldn’t find a decent hi-res one, so I remade the logo in Illustrator. You can download the bottle vector logo for use in your Bottle projects.