Web Hosting - Create and Manage Python Applications

This KB contains instructions for managing Python 3 based applications using Apache/mod_wsgi on Web Hosting.

If you're interested in enabling Python support, please contact webhosting@doit.wisc.edu and provide the URL locations you'd like us to enable mod_wsgi. We encourage you to read the documentation for each tool before getting started:

  • mod_wsgi An Apache module which can host any Python web application which supports the Python WSGI specification
  • pip is the package installer for Python
  • virtualenv is a tool to create isolated Python environments.

Managing your Python virtual environment: 

  • SSH into your Plesk account with an SSH client or the Plesk control panel SSH Terminal
  • By default we'll create your virtualenv environment under your account at $HOME/python-env
  • Shared hosting manage a mapping e.g. WSGIScriptAlias /app /var/www/vhosts/wwwtest.example.wisc.edu/wsgi/app.wsgi
  • The app.wsgi refrenced above is managed by the customer and contains a mapping to the virtualenv
    
    import sys, os, json
     
    VIRTUALENV_PATH='/var/www/vhosts/wwwtest.example.wisc.edu/python-env'
    activate_this=os.path.expanduser(VIRTUALENV_PATH+'/bin/activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))
     
    def application(environ, start_response):
        status = '200 OK'
        output = b'Hello, World!'
     
        response_headers = [('Content-type', 'text/plain'),
                            ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
     
        return [output]
    
  • Before installing dependencies you should activate your vitual env: $ source python-env/bin/activate
  • Install dependencies using Pip: pip install lib1 lib2 lib3
  • We recommend managing a requirements.txt file with your application under source control for specifying these dependencies. e.g.
    • pip freeze > requirements.txt
    • pip install -r requirements.txt