Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Flask-Restful.MD #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 71 additions & 3 deletions Week08/Flask-Restful.MD
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,77 @@ app.run(threaded=True)
```

###Deployment
When the server is ready to be deployed to production it is nessesary to wrap it into apache by using the mod_wgsi.
Instructions for using flask under Apache can be found at:
[http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/](http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/)
#Deployment

It is recommended to do this through a virtual python instance (venv), details on how to set up a virtual environment for python can be found [here] [1].

###Install mod_wsgi

On Ubuntu/Debian enter the following in terminal:

$ apt-get install libapache2-mod-wsgi

###Create .wsgi file
Next step is to create a .wsgi file which you need to run your application. For most basic application this should do:


from smuu import app as application


If your application is a singleton you can directly import that one as application.

Save the .wsgi file somewhere you are likely to find it again, since you will be referring to it in a while. For example : "/var/www/smuuu".
Also install your application in the virtual environment.

###Configure Apache

Now we need to create an Apache configuration file for your application. In this config we are running the application under a different user for "security reasons". This configuration only works under linux.

<Virutalhost *>
ServerName dabs.com

WSGIDaemonProcess smuu user=user1 group=group1 threads=5
WSGIScriptAlias / /var/www/smuu/smuu.wsgi

<Directory /var/www/smuu>
WSGIProcessGroup smuu
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>

For more details check out the [mod_wsgi wiki] [2]

###Trouble in paradise

If nothing happens and you sit in your chair smashing your head against the keyboard, try the following countermeasures before throwing your computer through the window:

#####Nothing happens, errorlog shows SystemExit ignored.
You probably have a app.run() call in the applicaiton file which is not guarded by

if __name__ == '__main__':

Delete app.run(), or put it into a if block as shown above.

#####Application gives permission errors
App is being run by a wrong user, check if the app has privileges for all folders and that the app runs as a correct user.

#####Application dies with an error on print
mod_wsgi does not allow any usages of sys.stdout or sys.stderr. You can bypass this by adding this to the .wsgi config file:

WSGIRestrictStdout Off

Or replace the standard out with a different stream in the .wsgi config file:

import sys
sys.stdout = sys.stderr

#####Accessing resources gives IO errors
Is your application a in a single .py file? Convert your application into a package.

[1]:https://github.com/reykjavik-university/2014-T-514-VEFT/blob/master/Week08/Python_environment_install_Linux.md
[2]:http://code.google.com/p/modwsgi/wiki/

###Good tutorials for flask restful:
[http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask](http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask)
Expand Down