# Deploying a Flask Application on Webfaction

A few months ago i had to deploy for the first time a Flask application on a Webfaction server. After studying this Webfaciton Community thread on the topic, issuing a Webfaction support ticket for help ---Webfaction customer service is excellent--- and trying and failing several times, i figured it out. Here's what i did.

APPNAME
├── APPNAME
│   ├── main.py
│   ...
│   └── wsgi.py
...
├── Pipfile
├── Pipfile.lock


that you are tracking it with Git, that you run it locally in a Python virtual environment, e.g. using Pipenv, and that it works. Now, let's deploy it to Webfaction.

1. Log in to your Webfaction control panel and create a new mod_wsgi/Python application. Also create a corresponding domain and website for the app. I assume you know how to do that.

3. SSH into your Webfaction server and run these commands:

> mkdir home/USERNAME/webapps/APPNAME/APPNAME


You don't need the lib directory in the last line, because you will be using a virtual environment.

4. Set up Git, Git hooks, and push your local repository to Webfaction. See the Webfaction instructions here and here.

5. Create your virtual environment ---let's call it VENVNAME here--- and install your app requirements. I think the easiest way to do this is to install Pip to install Pipenv to manage your virtual environments and packages.

6. If your local repo has a .env environment file containing (Git-ignored) secrets, then create a corresponding .env file in your Webfaction repo.

7. If you haven't done so already, create the file home/USERNAME/webapps/APPNAME/APPNAME/wsgi.py containing the single line from main import server as application.

This assumes that your Flask code lies in the module main.py and that the Flask app you create in that module is call server. If that's not the case, then change the above snippet as needed.

8. Edit the file home/USERNAME/webapps/APPNAME/apache2/conf/httpd.conf to the following, where PORT is the port number that Webfaction assigned to your application (which you can view in your Webfaction control panel for the app):

ServerRoot "/home/USERNAME/webapps/APPNAME/apache2"

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

Listen PORT
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5