Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Daniel Markham
Daniel Markham
10,976 Points

Python 3.5, Flask, VirtualEnv, and Apache2

Hi all,

I have been trying, not succeeding, in setting up a website via Digital Ocean using Python 3.5, Flask, a virtual environment, and Apache2. I have scoured the web and cannot find a decent beginner tutorial to get the website up and running, i.e. "hello world" would suffice at this point.

Does anyone have an example apache config file, wsgi file, and a low-level explanation that covers how to point Apache2 at a virtual environment? I think I am mostly struggling with getting the config file as I have found several examples of wsgi files. Just struggling putting it all together.

Thanks in advance for any help.

Best,

Dan

Daniel Markham
Daniel Markham
10,976 Points

Thanks again Gerald. I was finally able to get this working. That e-Book is dense but I am trying to make my way through it.

That book is one of my favorites! :) I am a Network automation Engineer, so being able to muddle through the boring OSI model to automate [server setup, docker deployments, SNMP based monitoring, etc] is my jam! If you need more advice just let me know!

3 Answers

http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/

This is a really straight forward document, its also assuming that you are familiar with how wsgi responds to server requests, if you aren't familiar with it download:

https://ebooks-it.org/1430258543-ebook.htm

(Foundations of Python Network Programming)

I suggest reading this book irregardless but for this particular issue focus on chapters 9,10 and 11.

Daniel Markham
Daniel Markham
10,976 Points

Many thanks Gerald. You've provided me with a treasure trove of information.

@danielmarkham one more thing, when I first was deploying a python project I was overwhelmed myself on the number of configuration walkthroughs out there on the interwebs. Just remember that computers are logic machines:

  1. Request gets sent from a user through a broweser.

  2. That request hits your NGINX/HTTP proxy (this step is only relevant if you are using a proxy, usually only relevant if you are hosting multiple sites, but they do add a layer of security)

  3. Proxy passes the request to apache

  4. Apache (depending on the request.. For instance let's say it's a GET request for your homepage) passes the reqeuest to wsgi.

  5. Wsgi processes the request, gets the homepage from a view and sends it back through the lamp stack to the browser.

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

Daniel Markham did you manage to resolve this? I have configured my mod_wsgi but I am still getting errors. Everything I have tried doesn't work. I have ssh Fonzi@52.232.108.191 and my password, but each time I try to configure my apache file and wsgi file and reload apache2 nothing happens when I enter the ip address into the browser.

Daniel Markham
Daniel Markham
10,976 Points

Hi Mayur Pande ,

Yes, I did get it working after a LONG time. Here is my folder structure as it stands:

/var/www/

--FlaskApp
    --FlaskApp
        --static
            --img -->for images
        --templates
        --venv -->virtual environment for python
        --__init__.py
    --flaskapp.wsgi

I believe you are referring to the ".wsgi" file, and here is what I have to use the virtual env:

#!/usr/bin/python
activate_this = '/var/www/FlaskApp/FlaskApp/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'your secret key. If you share your website, do NOT share it with this key.'

I hope this helps. I had to piece together quite a few resources from the Interwebs and Gerald's resources were extremely useful as well. The flask site (bottom) and a couple of others were helpful:

http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/

Hope this helps and let me know how it goes.

-Dan

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Daniel Markham thanks, I managed to sort it out in the end, as I was running python 3 there was a problem with the mod_wsgi so I had to download the version for python 3.

Also my wsgi file I had to insert another path to my directory like this;

#! /usr/bin/python3
import sys
import logging
activate_this = '/var/www/html/Webscraper/Webscraper/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/Webscraper/")
sys.path.insert(0,"/var/www/html/Webscraper/Webscraper/")

from Webscraper import app as application