Despliegue de aplicaciones flask
De WikiEducator
< Usuario:Lmorillas | desarrollo web servidor | flask
Revisión a fecha de 06:31 13 dic 2013; Lmorillas (Discusión | contribuciones)
Apache con WSGI
Documentación
* http://flask.pocoo.org/docs/deploying/mod_wsgi/ |
Pasos
Instalar mod-wsgi
$ sudo apt-get update $ sudo apt-get install libapache2-mod-wsgi
Configurar usuario
$ useradd -M flask $ usermod -s /bin/false flask $ usermod -L flask $ adduser flask www-data
lanzador.wsgi
#!/usr/bin/env python # -*- coding: utf-8 -*- # Si usamos virtualenv activate_this = '/ruta/al/env/bin/activate_this.py' execfile(activate_this, dict(__file__=activate_this)) # Si no está instalada la app: para poder importar import sys sys.path.insert(0, '/ruta/a/la/aplicacion') from <miaplicacion> import app as application
- Tiene que tener permisos de ejecución
$ chmod +x lanzador.wsgi
virtual host en apache
<VirtualHost *:80> ServerName example.com WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5 home=/var/wwww/home ... WSGIScriptAlias / /var/www/yourapplication/lanzador.wsgi <Directory /var/www/yourapplication> WSGIProcessGroup yourapplication WSGIApplicationGroup %{GLOBAL} WSGIScriptReloading On Order deny,allow Allow from all </Directory> </VirtualHost>
$ sudo a2ensite nuevo_sitio $ sudo service apache2 restart | reload
Si hay errores ...
$ tail -f /var/log/apache/error.log
con userdir
<Directory /home/*/public_html> Options Indexes FollowSymLinks MultiViews ExecCGI AddHandler wsgi-script .wsgi Order allow,deny Allow from all </Directory>
https ??
- http://www.subdimension.co.uk/2012/11/10/flask_modwsgi_ssl_oh_my.html
- http://flask.pocoo.org/snippets/93/
- http://stackoverflow.com/questions/4893432/ssl-on-apache2-with-wsgi
- http://www.nanotutoriales.com/como-crear-un-certificado-ssl-de-firma-propia-con-openssl-y-apache-http-server
$ sudo a2enmod ssl
<VirtualHost *:443> ServerName flaskr WSGIScriptAlias / /home/lm/tmp/flaskr/flaskr.wsgi SSLEngine On SSLCertificateFile /home/lm/tmp/flaskr/crt/server.crt SSLCertificateKeyFile /home/lm/tmp/flaskr/crt/server.key #SSLCertificateChainFile /path/to/sub.class1.server.ca.pem <Directory /home/lm/tmp/flaskr> WSGIProcessGroup flaskr WSGIApplicationGroup %{GLOBAL} WSGIScriptReloading On #Order deny,allow #Allow from all Require all granted </Directory> </VirtualHost>
login por https
Del ejemplo de flaskr. Uso de url_for:
url_for('<destino>', _external=True, _scheme='http|https'))
@app.route('/login', methods=['GET', 'POST']) def login(): if not request.is_secure: return redirect(url_for('login', _external=True, _scheme='https')) error = None if request.method == 'POST': if request.form['username'] != app.config['USERNAME']: error = 'Invalid username' elif request.form['password'] != app.config['PASSWORD']: error = 'Invalid password' else: session['logged_in'] = True flash('You were logged in') return redirect(url_for('show_entries', _external=True, _scheme='http')) return render_template('login.html', error=error)