Wednesday, July 23, 2014

Deploying a Flask Project on WebFaction with Git



I found it tedious having to redo the setup of my application on WebFaction whenever there is a new update of the application. I started looking for way to update the content of the application, without affecting the setup configurations of the application on WebFaction.

Step 1: Add your site to Your WebFaction Account
From your WebFaction console:
1.       Log into your WebFaction Console

2.       Go to your “Websites” tab, click the “Add New Website” button
3.      Enter the website name and domains as needed for your project
4.      Under the Contents section, choose Add an Application > Create a New Application
1.      Name to "myapp" 
2.      Set App Category to “mod_wsgi”
3.      App Type should be the most recent version of mod_wsgi that supports your Python version. Make sure that your Python version matches with your mod_wsgi choice ( “mod_wsgi 3.5/Python 2.7″ for this guide).
4.      Click the Save button to add that application
5.      Click the Save button to save your website
Now your WebFaction account should have a new domain, website, and mod_wsgi application.

Step 2: SSH into your new application
In this directory, you’ll see two folders:

- apache2/      # This contains all of your Apache config and action files
- htdocs/       # This is the folder Apache looks into (by default) to launch your project

Step 3: Upload your Flask project using Git
Upload the project/ folder to the application directory using Git. "myapp" directory is created

git clone https://username@bitbucket.org/uobis0/myapp.git
 
 ==OR==

git init
git remote add origin https://username@bitbucket.org/uobis0/myapp.git
git fetch
git branch master origin/master
git checkout master


Now, my
~/webapps/myapp directory list looks like the following:
- apache2/
    - config.py
    - libs/
    - app/
        - static/
        - templates/
        - __init__.py
        - views.py
        - models.my
- htdocs/



Step 4: Edit apache2/conf/httpd.conf

LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule alias_module      modules/mod_alias.so    #Your version of mod_wsgi might not need to add this.


<Directory /home/username/webapps/myapp/htdocs>
    AddHandler wsgi-script .py 
    RewriteEngine On
    RewriteBase /    
    WSGIScriptReloading On
</Directory>


# This will ensure that domain.com/static points to the flask static directory
Alias /static/ /home/username/webapps/myapp/myapp/app/static/
# This points to the file that launches the site
Alias / /home/username/webapps/myapp/htdocs/index.py/

Step 5: Make sure your init.py file is right (optional)
Ensure that, whatever your structure, your project’s __init__.py file is launching your Flask application. This is what the file at ~/webapps/myapp/myapp/app/__init__.py should look like:
from flask import Flask

# Setting up the App
app = Flask(__name__)
app.config.from_object('config')

# Importing the views for the rest of our site
from app import views
 if __name__ == '__main__':
    app.run()

This will work nicely with our htdocs/index.py file, which we’ll set up next.

WebFaction should have created this file. In it are a few scripts, but you can completely remove those. Here is what the ~/webapps/myapp/htdocs/index.py file should contain:

import sys

# Active your Virtual Environment, which I'm assuming you've already setup
activate_this='/home/username/webapps/myapp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

# Appending our Flask project files
sys.path.append('/home/username/webapps/myapp/myapp')

# Launching our app
from main import app as application

Step 7: Restart Apache
The last step will be to restart apache, like so:

~/webapps/myapp/apache2/bin/restart

If you’re having trouble, take a look at your logs in the ~/logs/users/ directory.
The steps above makes updates very easy:

cd ~/webapps/myapp/myapp
git pull https://username@bitbucket.org/uobis0/myapp.git
~/webapps/myapp/apache2/bin/restart

No comments: