Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

A template for deploying a flask app to apache server.[NOT A QUESTION RATHER A SOLUTION]

Has anyone had a nightmare deploying a flask app to apache. I have created a template for flask app that has all the folders required including the css and js files for twitter bootstrap. I have also included a .wsgi file and a user guide for deployment. The template assumes all apache2 prerequisites have been installed.

https://github.com/squadran2003/flask-bootstrap-apache-template

happy coding.

Tutorial to deploy a flask app to ubuntu running a apache server.

http://andreascormack.me.uk/tutorialdetails/Deploy-a-flask-app-to-ubuntu-running-a-apache-server

Oziel Perez
Oziel Perez
61,321 Points

Ok I have several questions, as I've been at it for 3 DAYS trying to get this to work:

Instead of deploying to a standalone ubuntu server or other linux distro, I'm deploying to a virtual host on AMPPS in Mac OSX Mavericks. Because of that, I cannot "create a new virtual host" but rather conform to using the <VirtualHost> that is in the httpd.conf file. Will that mess things up for me?

Secondly, I notice that virtualenv is in most if not all tutorials, but my understanding is that this is not required. Do I NEED to have virtualenv installed for this to work? I'm just using the python instance that comes with AMPPS (python 3.5.1) so I just want this to work system wide.

Other configurations and setups I have to point out is that, since this is my development server, I am running several other sites on the same apache instance. So given that I have the DocumentRoot "/Applications/AMPPS/www/", I have several folders for each site in that DocumentRoot, so is it ok if I deploy my flask app and wsgi to it's own folder? My current set up is "/Applications/AMPPS/www/braintree" and in that braintree directory I have braintree.wsgi and "python/" and in that python directory I have main.py.

Lastly, I'm so confused on how the importing works, especially for wsgi. One tutorial makes it seem like I'm importing the "app" variable from main.py, so my wsgi looks like this right now: from main import app as application. But in your template you have "from Yourapp import app as application", making it seem as if I'm importing the FILE in the yourapp directory, and then from the python docs it's still different. They make it seem as if I have to import the app variable from the DIRECTORY, and having the python file as init.py, so I don't even know what's right and wrong anymore.

Speaking of which, is all of that code necessary in the wsgi file? Again, a lot of tutorials are different and say all you need is the import statement. Others have the import and the additional path attached to the python path (which I think is the correct one)

I would just do all of this in my ubuntu virtual box but I don't want that, I wanna make sure I can do it on any apache platform first, starting with mac osx.

1 Answer

Hi Oziel

I have not tried deploying to a mac environment so i cannot comment much about that. In terms of the Virtual host, its good to have separate virtual host for each app. You could try having many Virtual hosts within the same httpd.conf file. Once you restart the apache2 server you should be able to tell if the Virtual host sections are all fine, as if not, you will see errors. Yes you can deploy your Flask app and wsgi to its own folders long as your paths are all correct i.e your Virtual host path points to the correct Flask app. I have deployed multiple Flask Apps on one server with no issues. In terms of Virtual environment, i recommend you create a virtual environment for each app so resources dont conflict with other apps. In regards to the imports, app is the app.py file from the root of that folder. I would also keep all the lines of code in the wsgi file especially the line that activates the virtual enviroment. I have made amends to my template on github so you might want to have a look at it again.

hope that helps you

Oziel Perez
Oziel Perez
61,321 Points

Thanks a lot. After a lot more fiddling and a trip to the django documentation, it turns out that if I run my flask app in daemon mode, I need to set process-group=mygroup on the script alias directive, something no one ever mentioned. And as soon as I did that, everything started working. You should consider mentioning that in your template as well. I will definitely consider using virtualenv and mutliple virtual hosts for other tests and for live apps; just wanted to see flask at work in the most bare configuration