Deploying a Flask app on Ubuntu: uWSGI, nginx and Upstart

Let’s get straight to the point. I have a Flask app that I want to run on Ubuntu. I shall use uwsgi and nginx. Upstart will be used to monitor the process and restart it if it’s killed or dies an untimely death.

Running the uWSGI app with nginx

It’s very easy to run Flask apps with uwsgi, just cd into the directory where your main app file exists and pass the uwsgi module name. If your main app file is called “” and if your Flask app instance is called “app” inside, then your uWSGI module name would be “main:app” from the directory where resides.

Now, we need to configure nginx:

Here, we first need to define an upstream. We define the “flaskapp” upstream for our app. Then inside the location block, we use it as the source of our uwsgi_pass directive. It would tell nginx to connect to the uwsgi server we defined and serve as a bridge between the internet and the flask uwsgi app.

Upstarting the uWSGI process

Now, what happens if our uWSGI process dies? nginx would no longer be able to connect to the app and throw a “bad gateway” error. So we need to make sure that if the process dies from an error or somehow gets killed, it is restarted automatically. There are a few tools which can do this. Supervisord is the most popular one, Circus is also very nice. However, I like to use Upstart for cases like this. Upstart comes with Ubuntu and used by the OS for many of it’s own services. It’s easy to use, already available, reliable and most importantly – it allows me to create a service too!

To create our upstart config, we cd into “/etc/init” and create a new file named “flaskapp.conf”. Then we put the following contents in there:

Here, we’re telling Upstart to launch our app on startup and respawn whenever the process dies. We put our commands in a script block. We have defined our service, now time to fire it up. If you have any uwsgi process run with the same config, first kill them. Then run this command to start the service:

Right, you can use the usual “start|stop|restart|status” service commands with it. The service name is derived from the file name we used in “/etc/init”. The log of the process would be found in “/var/log/upstart/flaskapp.log”.

From Upstart to Systemd

Ubuntu is moving away from Upstart to systemd. Most of my apps use Ubuntu 14.04 LTS so I can still use Upstart fine. However, if your version of Ubuntu doesn’t ship Upstart, you can use systemd for similar tasks. Here’s the link: systemd for upstart users

Python and Django on Vagrant

Well, this is going to be another straight forward post with loads of shell commands. The background is simple, I love OS X for the many apps it offers but at the same time I always had the joy of developing on a Linux machine. I decided to use Vagrant to have one central box for my projects. I use separate vagrant boxes for complicated projects. But didn’t like the idea of having individual boxes for simple ones.

I work on Python and Django a lot. So in this post I am going to document my setup.

Vagrant File:

# I have assigned a private IP to the box
# I have assigned a hostname to it.
# I am using Ubuntu Precise Pangolin 64 bit as my base box (which I already have added)

Hosts File (/etc/hosts):

After I setup the box, I logged in using SSH:

Now, the usual Ubuntu setup.

Bash Profile in the Ubuntu machine:

# To make the Django built in server available from outside the box I need to run it on so that it listens on all interfaces. So I added a handy alias.

# Added the virtualenv stuff to the profile

Bootstrapping a project:

Now the app would be available on:

I also modify the ~/.virtualenvs//bin/postactivate script to cd into the project source directory. Saves time when I hit “workon env_name”

That’s it 🙂

Debugging MongoDB Connection Issues

I am working on a project where I connect to MongoDB from PHP using the PECL extension. Regardless of your stack, we might time to time face strange issues when our clients can’t connect to the mongodb server. In this post, I would document what I did in my case.

First test if the mongodb server can be connected to. Use the “mongo” shell client.

If the connection succeeds, then the server is definitely reachable. You have made some mistakes in configuring your client. A very common mistake is having a malformed connection URI. So double check if you typed in your host, port and other details properly in the connection string.

If the above fails, that means the server is not responding. To begin with, try restarting it.

If the server started well, try again. Still connection failed? OK, the server might be damaged. How about checking the log files?

Nothing interesting? Or do not understand the format? Well, then let’s try blindly repairing the server:

Your problem should be resolved by now. But if it doesn’t solve the issue, your problem is not a common one. Try Googling further or asking on StackOverflow or other programming forums.

Building a FM Radio Transmitter with Raspberry Pi

DISCLAIMER: Building FM transmitters and transmitting on FM frequency requires government permissions in many countries/regions. I am posting this for educational purposes and I strongly recommend not to use this for any other purposes. I shall not be liable for any use of the contents of this post.

I have owned a Raspberry Pi for a while now but haven’t really done anything significant with it other than installing a LAMP server and running some random Python codes on it. Today, while reading Rasp Pi related articles on the internet, I came across a reddit thread where a guy posted about his home made raspberry pi based FM transmitter which he used to relay random messages from inside his car to the surprise of other people on the street. I’m sure he had so much fun doing that though that was illegal! 😀

It was such an inspiration for me, specially because I needed no extra hardware! The Pi hardware itself is capable of transmitting in really short range (~10cm). I started googling on how I can do the same and soon I found enough resources. In fact I found the source code and a precompiled binary to do this. I couldn’t wait to test! I plugged in my Pi and transferred the source code to the device and turned the radio on with the provided sample clip. Then browsed for the default station on my android phone. Voila, it was working!

It is really really simple!

# Grab the application package – Download.

# Turn on and SSH into your Raspberry Pi. Unzip the package and transfer the directory to the Pi.

# cd into the directory and run:

Please note that you need root access for the transmitter to work. So make sure you used “sudo”. You can replace the sound.wav with any file that is 16 bit 44.1kHz Mono format.

By default the transmitter will transmit on FM 103.3 MHz frequency. You can pass the frequency as an additional parameter to the application. Eg.

The transmitter range can be extended up to ~100m by connecting wires to the GPIO pins. I’m going to try that tomorrow and see how it goes.

The application package also contains a python module that provides an API to programmatically run sounds.

So what’s next? I’m going to look for a decent sound conversion tool to convert my mp3s into mono wav. And I might write a small program to rotate the tracks on it.

Import SQL Script to MySQL using the Command Line

Summary: We have acquired a large databse dump which we need to import into our local system. Since the database size is large, phpmyadmin can’t help. MySQL command line tool comes to our rescue 🙂

Change directory to where our SQL script is:

First login to mysql:

Change database to our target database:

Execute the SQL Script:

Configuring Amazon EC2 Instance with Ubuntu: Enabling remote root access

The other day, I was setting up an amazon EC2 instance for Leevio. It is a rare experience since we do not setup a new instance everyday! 😀 So, I am gonna share what I did to do what.

First, Hasin vai gave me a secret email address and a password which I used to login to the Amazon backend. There are quite a number of tabs for different services. I chose the EC2 tab. There was no instance created, so I went ahead and created a new instance. While creating it I was driven through a simple configuration wizard. It asked me to select the instance type, create some tags for the instance and choose a operating system image and finally I created network access permission. I chose Ubuntu 11.10 (the then latest one). The permission set dictates which IPs shall get access to which ports. I opened up port 80 for http connection and port 22 for SSH and SFTP. There are preset settings to open all TCP ports, UDP ports and All ports etc. You can choose the access level manually or pick one of this presets. I did manually since I wanted to learn how it worked.

Then I saved the ssh keys and secured them in a secret place on my HDD 😀 I also noted the public DNS of the instance. I typed in the following command to login to the ubuntu machine running on the instance:

The first problem I got is the “too open permission” for the key file. In fact if your key file has such a permission that other users on the same machine can access it, you can not use the key. Keys are meant to be private and make them private by issuing the following command:

Now I issued the previous command and I got in! Yay! 😀

My default username is “ubuntu”. To configure stuff, I need to be “root”. So I hit:

By default the root username doesn’t have a password. When prompted for a password, just press enter. I logged in as root. But wait, I am using a Linux machine and the root user has no password? I can’t stand that! So I decided to change the password of root account first. The process is simple:

I was asked twice the password and upon confirmation, the root account now had a super secret password which is known to only me in the whole universe 😀

The second issue we still have is that root user still can’t login via ssh. To do so, we have to enable PasswordAuthentication on the server. With my mighty root power, I installed vim on the fly and edited the heart of ssh daemon:

I navigated to the “PasswordAuthentication” section and changed it’s value from “no” to “yes”. Then I reloaded the daemon by typing:

Then I typed in these commands to exit the root shell and logout from the remote shell:

I then assigned an IP address to the instance from the IP management section and reloaded the instance. Now I can login to the server from my laptop using the command:

PS: Using the key pair to login is a more secured and recommended way of doing the stuff. But it’s easier to do stuff using remote root login.

Installing PHPUnit on Ubuntu

Summary: I am on Ubuntu 11.10 and have the necessity to install PHPUnit. From my past experience, I know that installing via repository installs old version + fails to run. So I decided to install it via PEAR. Here’s a walk through!

Upgrade PEAR
Damn! The shipped version of pear via the “php-pear” package is outdated. The first thing we need to do is upgrade our PEAR setup. We have to ask pear to upgrade itself!

That should upgrade our pear to the latest version available!

Setup PHPUnit
To install PHPUnit, we have to set auto discovery turned on and discover the PHPUnit PEAR channel. Then we force install PHPUnit with all dependencies. That’s simple!

If everything goes okay, we have a working setup of PHPUnit by now! Type “phpunit –version” on console and check out the output! Ooops! It didn’t work! We’re getting an error message regarding “require_once()”. The issue is obvious, the PEAR directory which contains all PEAR packages haven’t been added to the php include_path.

Fix it this way:

Vim editor window will open with the empty buffer. If you’re not comfortable with vim, just use another text editor. Type in the following directives:

Save and quit. We had to do this manually because pear doesn’t automatically set the include_path to the pear location (which is “/usr/share/php/”).

Now try on terminal:

The output should look like:

Happy Unit testing!

Ubuntu 11.10 Fix: Unplugging Power Cable Suspends Notebook

The scenario is simple. I am using Ubuntu 11.10 64bit on my Compaq Presario CQ42. When power cuts or I unplug the adapter, I get a warning message that battery is critically low and it suspends a few seconds later. After searching for a while I found the solution.

Use this following command on Terminal:

It should solve the problem! 🙂

Displaying Skype on Notification Area in Ubuntu 11.10

I am using Ubuntu 11.10 64 bit (don’t ask me why! grr!) and I hate Unity. Besides all the inconvenience – the Skype icon doesn’t appear on the notification area. From Google found this solution:

It works! 😀

Fixing NTFS devices in Ubuntu

I am running Zorin OS 5 which is based on Ubuntu. I had a NTFS drive which was somehow corrupted. This is how I fixed the issue:

Install ntfsprogs package:

Find the drive’s device path:

Unmount the drive if already mounted:

Run ntfsfix tool to fix common NTFS issues:

Now mount again and have fun:

That was simple, no? 😀