How do I set up the .wsgi file and apache configuration to deploy on a Large Flask Application on to my droplet?

April 8, 2016 811 views
Deployment DigitalOcean Python Frameworks Python Debian

I have created a Flask Application based on the Large Application Structure in Miguel Grinberg's Flask Web Development Book. I have followed the tutorial in how-to-deploy-a-flask-application-on-an-ubuntu-vps and have managed to deploy a single file Flask application on my server. What changes/ modifications do I have to implement to my .wsgi file and apache config to be able to host Large Structured applications?

1 comment
  • there are some good ideas in http://www.enigmeta.com/2012/08/16/starting-flask/. The following were found to be in error, but still a good deployment model.

    add *.py[cod] to the .gitignore file, actually maybe suggest picking up template from https://github.com/github/gitignore/blob/master/Python.gitignore

    references to git@github.com:USERNAME/helloflask.git didn't seem to work for me. I had to use https://github.com/USERNAME/helloflask.git, for git remote add origin git@github.com:USERNAME/helloflask.git and git clone git@github.com:USERNAME/helloflask.git

    the fabfile.py needed some changes. Here is what worked for me. Note change on source bin/activate line, had to have pip install in same run command

    from fabric.api import env, run, cd
    
    USERNAME = 'root'
    SERVER = 'helloflask.example.org'
    APP_NAME = 'helloflask'
    PROJECT_DIR = '/var/www/%s/%s' % (SERVER, APP_NAME)
    WSGI_SCRIPT = 'application.wsgi'
    
    env.hosts = ["%s@%s" % (USERNAME, SERVER)]
    
    def deploy():
        with cd(PROJECT_DIR):
            run('git pull')
            run('source bin/activate; pip install -r requirements.txt')
            run('touch %s' % WSGI_SCRIPT)
    
    edited by asb
1 Answer

This question was answered by @lking:

there are some good ideas in http://www.enigmeta.com/2012/08/16/starting-flask/. The following were found to be in error, but still a good deployment model.

add *.py[cod] to the .gitignore file, actually maybe suggest picking up template from https://github.com/github/gitignore/blob/master/Python.gitignore

references to git@github.com:USERNAME/helloflask.git didn't seem to work for me. I had to use https://github.com/USERNAME/helloflask.git, for git remote add origin git@github.com:USERNAME/helloflask.git and git clone git@github.com:USERNAME/helloflask.git

the fabfile.py needed some changes. Here is what worked for me. Note change on source bin/activate line, had to have pip install in same run command

from fabric.api import env, run, cd

USERNAME = 'root'
SERVER = 'helloflask.example.org'
APP_NAME = 'helloflask'
PROJECT_DIR = '/var/www/%s/%s' % (SERVER, APP_NAME)
WSGI_SCRIPT = 'application.wsgi'

env.hosts = ["%s@%s" % (USERNAME, SERVER)]

def deploy():
   with cd(PROJECT_DIR):
       run('git pull')
       run('source bin/activate; pip install -r requirements.txt')
       run('touch %s' % WSGI_SCRIPT)

View the original comment

Have another answer? Share your knowledge.