Diferencia entre revisiones de «Usuario:Lmorillas/desarrollo web servidor/flask/despliegue»
De WikiEducator
(11 revisiones intermedias por el mismo usuario no mostrado) | |||
Línea 1: | Línea 1: | ||
{{MiTitulo|Despliegue de aplicaciones flask}} | {{MiTitulo|Despliegue de aplicaciones flask}} | ||
+ | {{TOC | right}} | ||
+ | == Apache con WSGI == | ||
{{Objetivo|1= | {{Objetivo|1= | ||
− | |||
* http://flask.pocoo.org/docs/deploying/mod_wsgi/ | * http://flask.pocoo.org/docs/deploying/mod_wsgi/ | ||
* https://beagle.whoi.edu/redmine/projects/ibt/wiki/Deploying_Flask_Apps_with_Apache_and_Mod_WSGI | * https://beagle.whoi.edu/redmine/projects/ibt/wiki/Deploying_Flask_Apps_with_Apache_and_Mod_WSGI | ||
+ | |Title=Documentación | ||
+ | |TOCdepth=3}} | ||
− | + | {{Objetivo|1= | |
− | + | |TOCdepth=3 | |
+ | |Title=Pasos}} | ||
+ | |||
+ | ====Instalar mod-wsgi==== | ||
$ sudo apt-get update | $ sudo apt-get update | ||
$ sudo apt-get install libapache2-mod-wsgi | $ sudo apt-get install libapache2-mod-wsgi | ||
− | + | ||
− | + | ==== Configurar usuario ==== | |
− | * Si | + | $ useradd -M flask |
− | + | $ usermod -s /bin/false flask | |
− | + | $ usermod -L flask | |
− | + | $ adduser flask www-data | |
− | + | ||
− | + | ==== lanzador.wsgi ==== | |
− | + | <source lang="python"> | |
+ | #!/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 | ||
+ | </source> | ||
+ | |||
+ | : '''Tiene que tener permisos de ejecución ''' | ||
+ | $ chmod +x lanzador.wsgi | ||
+ | |||
+ | ==== virtual host en apache ==== | ||
<source lang="apache"> | <source lang="apache"> | ||
− | <VirtualHost *> | + | <VirtualHost *:80> |
ServerName example.com | ServerName example.com | ||
− | WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5 | + | WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5 home=/var/wwww/home ... |
− | WSGIScriptAlias / /var/www/yourapplication/ | + | WSGIScriptAlias / /var/www/yourapplication/lanzador.wsgi |
<Directory /var/www/yourapplication> | <Directory /var/www/yourapplication> | ||
WSGIProcessGroup yourapplication | WSGIProcessGroup yourapplication | ||
WSGIApplicationGroup %{GLOBAL} | WSGIApplicationGroup %{GLOBAL} | ||
+ | WSGIScriptReloading On | ||
Order deny,allow | Order deny,allow | ||
Allow from all | Allow from all | ||
Línea 35: | Línea 60: | ||
</VirtualHost> | </VirtualHost> | ||
</source> | </source> | ||
− | |||
+ | $ sudo a2ensite nuevo_sitio | ||
+ | $ sudo service apache2 restart | reload | ||
+ | |||
+ | Si hay errores ... | ||
+ | $ tail -f /var/log/apache/error.log | ||
− | |||
− | |||
+ | ==== con userdir ==== | ||
<source lang="apache"> | <source lang="apache"> | ||
<Directory /home/*/public_html> | <Directory /home/*/public_html> | ||
Línea 49: | Línea 77: | ||
</Directory> | </Directory> | ||
</source> | </source> | ||
− | + | ||
+ | ==== sqlite ==== | ||
+ | * Si usamos '''sqlite''' el archivo tiene que tener permisos de escritura para apache!!! (Y el directorio si apache tiene que crear un archivo) | ||
+ | |||
+ | ==== 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 | ||
+ | |||
+ | <source lang="apache"> | ||
+ | <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> | ||
+ | </source> | ||
+ | |||
+ | ==== login por https ==== | ||
+ | ''Del ejemplo de flaskr''. Uso de '''url_for''': | ||
+ | url_for('<destino>', _external=True, _scheme='http|https')) | ||
+ | |||
+ | <source lang="python"> | ||
+ | @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) | ||
+ | </source> | ||
+ | |||
{{Objetivo| | {{Objetivo| | ||
* https://devcenter.heroku.com/articles/getting-started-with-python | * https://devcenter.heroku.com/articles/getting-started-with-python | ||
− | + | * http://ryaneshea.com/lightweight-python-apps-with-flask-twitter-bootstrap-and-heroku | |
+ | |TOCdepth=2 | ||
|Title=Despliegue en Heroku}} | |Title=Despliegue en Heroku}} |
Última revisión de 06:32 13 dic 2013
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>
sqlite
- Si usamos sqlite el archivo tiene que tener permisos de escritura para apache!!! (Y el directorio si apache tiene que crear un archivo)
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)