Flask Subdomain on Hostgator

I recently decided I wanted to create a new subdomain on this site completely separate from my Wordpress domain. One in which I could test out new projects and ideas that I have been working on and share them online. I wasn’t even sure this was possible, and I didn’t see that much information about creating totally separate subdomains that run different web frame works than the main site. At least for HostGator in particular.

I decided that I wanted to use Flask to serve the webpages on my new subdomain. I am really new to the framework but like the simplicity of it so far. I will most likely be using this subdomain for creating web apps or D3 examples and projects.

After doing some research, it seemed like this should be possible with HostGator since they support FastCGI and have python installed on the server (all be it an old version). That should be all we need to get Flask up and running on a subdomain.

First you will need to create an empty subdomain on your web server. You can easily do this by logging into your HostGator cPanel. HostGator sent me access information for the cPannel once I purchased my plan. Once inside the cPannel home screen you can scroll down to the Domains section:

You’ll want to click on Subdomains within the Domains section. Once in Subdomains you can easily create as many new subdomains for your site as you’d like. Here I have entered a subdomain named example (I didn’t actually create this):

The Document Root section will be auto filled once you have picked your subdomain name. In this case it is public_html/example. Once this has been created you’ll need to ssh into your server and create a few config files in the new subdomain directory:

ssh -p 2222 YOURUSERNAME@YOURDOMAIN
cd public_html/YOURSUBDOMAIN

Once inside your subdomain directory you’ll need to create the following files at a bare minimum to run a basic Flask app:

.htaccess
YOURAPP.fcgi
YOURAPP.py

You will also need to download Flask so that you have access to it on your system since HostGator doesn’t come with it. It does, however, come with virtualenv and pip, this will allow us to create a python virtual environment to grab the necessary packages:

virtualenv /PATH/TO/VENV
source /PATH/TO/VENV/bin/activate
pip install flask

This should download all the packages needed to run your Flask app. You can also use this method to download any other package dependancies that your app has. We don’t have permissions to install packages system wide, so virtual environments help us work around that limitation.

Flask has very helpful FastCGI documentation for deploying a Flask app here. I basically copied the example .htaccess and .fcgi files from there. Here is the .htaccess file that I am using:

<IfModule mod_fcgid.c>
   AddHandler fcgid-script .fcgi
   <Files ~ (\.fcgi)>
       SetHandler fcgid-script
       Options +FollowSymLinks +ExecCGI
   </Files>
</IfModule>

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ YOURAPP.fcgi/$1 [QSA,L]
</IfModule>

And here is the .fcgi file that I am using, you’ll notice that I need to append the virtual environment site-packages to the path. This is what gives us access to the Flask module:

#!/usr/local/bin/python

from flup.server.fcgi import WSGIServer
import sys
sys.path.insert(0, '/PATH/TO/VENV/site-packages/')

from YOURAPP import app

if __name__ == '__main__':
    WSGIServer(app).run()

You will also need to update the paths and file names with the names that you have on your system. I would also suggest using a very simple hello world Flask app to start out with just to make sure everything is set up correctly.

One frustrating thing about setting up your app, is that HostGator’s servers like to do a lot of caching so your changes might take some time to appear. You’ll need to be patent and make sure your app is running locally without issues before you push to the server. If anyone knows how to force update the HostGator web server let me know in the comments.

It takes a little work to get up and running but its pretty satisfying once it’s done. I’m excited to get some new projects out there. Here is the link to my new Flask subdomain, it’s quite basic for now but I will be adding more content soon. Let me know if you have any questions or comments below.