Deploying a Flask Project on WebFaction with Git [UPDATED]
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
All following instructions will assume you’re still in this directory.
virtualenv venv --python=python2.7
$ . venv/bin/activate $ easy_install-2.7 flask # Installs flask package for the app $ pip install -r /myapp/app/requirements.txt # Installs the packages for the app $ deactivate
Now, my ~/webapps/myapp directory list looks like the following:
drwxr-xr-x 7 uobis uobis 4096 Nov 11 14:29 apache2/ drwxrwxr-x 5 uobis uobis 4096 Nov 11 14:40 venv/ drwxrwxr-x 6 uobis uobis 4096 Nov 14 16:46 myapp/ drwxr-xr-x 2 uobis uobis 4096 Nov 11 14:29 htdocs/ -rw-rw-r-- 1 uobis uobis 292 Nov 18 15:14 wsgi.py
Step 4: Edit apache2/conf/httpd.conf
Using your favorite command line editor, open up the apache2/conf/httpd.conf file:
vim ~/webapps/myapp/apache2/conf/httpd.conf
Load Alias module (optional)
You’ll see a section where Apache Modules are being loaded. I had to manually add the Alias module to the bottom of the list (shown below).
. . 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.
Modify Alias and <Directory>
Add the following parameters to your <Directory> section:
WSGIScriptAlias / /home/username/webapps/myapp/wsgi.py <Directory /home/username/webapps/myapp/htdocs> AddHandler wsgi-script .py RewriteEngine on RewriteBase / WSGIScriptReloading On </Directory>
Now for the final edit of the config file.
Step 5: Make sure your main file is right
Ensure that, if your structure is package, your project’s __init__.py file is launching your Flask application. This is what the file at ~/webapps/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()
If you are using modules, the files should contain the app instantiation
app = Flask(__name__)
This will work nicely with our wsgi.py file, which we’ll set up next.
Step 6: Modify the htdocs/wsgi.py file
WebFaction should have created this file. In it are a few scripts, but you can completely remove those. Here is what the ~/webapps/myapp/wsgi.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/accout/myapp.git
~/webapps/myapp/apache2/bin/restart