Website deployment and server configuration can be a rather unrewarding task. To save you the trouble of configuring your Python applications to run on Apache, we put together a script to do the dirty work for you. If you follow the instructions below, you should have your Flask application up and running in no time
git pull handoutcode masterThere should be a folder called "Flask_example" containing a single file called "main.py." Notice that the "main.py" file defines a variable called "app" which is assigned to a Flask application object. This is the entry point to the web application.
git add . git commit -a -m "first deploy" git push origin dev:deploy_only
MY_REPO/assignment4/db/mydatabase.db
DATABASE = "/home/me/Documents/6170_code/assignment4/db/mydatabase.db"The previous code will obviously break if you choose to move your project to a different folder. Instead, do something like this:
DATABASE = os.path.join(os.path.dirname(__file__),"db","mydatabase.db")This command calculates the absolute path to "mydatabase.db" given the absolute path to your main Python file.
git push origin dev:submit
app.debug = True
What is happening behind the scenes? When you run your web application from your laptop (or desktop), Flask automatically creates a lightweight "test server" on your computer which listens for requests on a specific port on your computer. On a large shared server such as courses.csail.mit.edu, however, we don't have convenience of spawning new test servers on different ports.
In one way or another, Apache must associate a specific URL with your Python application. A naive solution would simply invoke your Python application every time a client requests that specific url. However, this would mean that the server would have to initialize a Python environment and load your code on every single web request. This is certainly not a very scalable solution.
Instead, Apache only loads the Python environment during the very first web request by essentially spawning a new, separate process to house your web application. Though the first request may be slow, subsequent requests are quick because the application has already been loaded into RAM.
The "index.fcgi" file which the autodeploy script added to your project tells Apache where to find the entry point to your application. It additionally wraps your application in a middleware which prints pretty error messages whenever your application crashes.
However, what exactly is the Flask application, and how does Apache know how to use it? In fact, Python developers have settled on a convention called the "WSGI" application which provides a simple interface for communication between the web server (Apache) and your web application. This modular design makes it easy, for example, to wrap web applications in extra functionality very much the same way that Python function decorators add functionality to regular Python functions. If you want to learn more, you can read about it here.