Python/Flask: Difference between revisions
| Line 79: | Line 79: | ||
== WSGI == | == WSGI == | ||
=== Apache | === Apache mod_wsgi === | ||
<pre> | <pre> | ||
| Line 110: | Line 110: | ||
Reference: https://flask.palletsprojects.com/en/stable/deploying/mod_wsgi/ | Reference: https://flask.palletsprojects.com/en/stable/deploying/mod_wsgi/ | ||
=== Reverse Proxy === | |||
Tell Flask it is being reverse Proxied: | |||
<pre> | |||
from werkzeug.middleware.proxy_fix import ProxyFix | |||
app.wsgi_app = ProxyFix( | |||
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1 | |||
) | |||
</pre> | |||
reference: https://flask.palletsprojects.com/en/stable/deploying/proxy_fix/ | |||
Apapache Configuration: | |||
<pre> | |||
LoadModule proxy_module modules/mod_proxy.so | |||
LoadModule proxy_http_module modules/mod_proxy_http.so | |||
ProxyPass / http://127.0.0.1:8000/ | |||
RequestHeader set X-Forwarded-Proto http | |||
RequestHeader set X-Forwarded-Prefix / | |||
</pre> | |||
reference: https://flask.palletsprojects.com/en/stable/deploying/apache-httpd/ | |||
=== Systemd Flask Service === | |||
/etc/systemd/system/myproject.service | |||
<pre> | |||
[Unit] | |||
Description=Gunicorn instance to serve myproject | |||
After=network.target | |||
[Service] | |||
User=youruser | |||
WorkingDirectory=/path/to/your/project | |||
Environment="FLASK_ENV=production" | |||
ExecStart=/path/to/your/project/venv/bin/gunicorn -w 4 'app:app' -b 0.0.0.0:8000 | |||
Restart=always | |||
[Install] | |||
WantedBy=multi-user.target | |||
</pre> | |||
sudo systemctl daemon-reload | |||
sudo systemctl start myproject | |||
sudo systemctl enable myproject | |||
Check logs with: | |||
sudo systemctl status myproject | |||
journalctl -u myproject | |||
== keywords == | == keywords == | ||
Revision as of 22:28, 13 December 2025
Flask
Installation
## Linux / macOS mkdir myproject cd myproject # python3 -m venv .flask python3 -m venv .venv . .venv/bin/activate
## Windows mkdir myproject cd myproject py -3 -m venv .venv .venv\Scripts\activate
## Next Common Step # pip install Flask pip3 install Flask
# End deactivate
References:
Quick Start
Flask Hello World
helloworld.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>\n"
Run the server:
flask --app helloworld run
NOTE: As a shortcut, if the file is named app.py or wsgi.py, you don’t have to use --app. See Command Line Interface for more details.
- Meaning, just simply rename helloworld.py to app.py (or wsgi.py) and you can simply run run:
flask run
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Note:
* Running on http://127.0.0.1:5000
Test the server:
curl http://127.0.0.1:5000 <p>Hello, World!</p>
References:
Hello World Headers
$ curl -i http://127.0.0.1:5000 HTTP/1.1 200 OK Server: Werkzeug/3.1.4 Python/3.12.3 Date: Sat, 13 Dec 2025 22:05:42 GMT Content-Type: text/html; charset=utf-8 Content-Length: 20 Connection: close <p>Hello, World!</p>
WSGI
Apache mod_wsgi
$ cd hello-app $ python -m venv .venv $ . .venv/bin/activate $ pip install . # install your application $ pip install mod_wsgi
wsgi.py :
## IIMPORT PATTERN from hello import app application = app ## OR ## FACTORY PATTERN from hello import create_app application = create_app()
Configure mod_wsgi-express and specify the number of worker processes:
mod_wsgi-express start-server wsgi.py --processes 4
Example running mod_wsgi-express as root, to bind to port 80/443, but then drop permissions and binding app as different user:
sudo /home/hello/.venv/bin/mod_wsgi-express start-server /home/hello/wsgi.py \ --user hello --group hello --port 80 --processes 4
Reference: https://flask.palletsprojects.com/en/stable/deploying/mod_wsgi/
Reverse Proxy
Tell Flask it is being reverse Proxied:
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
reference: https://flask.palletsprojects.com/en/stable/deploying/proxy_fix/
Apapache Configuration:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so ProxyPass / http://127.0.0.1:8000/ RequestHeader set X-Forwarded-Proto http RequestHeader set X-Forwarded-Prefix /
reference: https://flask.palletsprojects.com/en/stable/deploying/apache-httpd/
Systemd Flask Service
/etc/systemd/system/myproject.service
[Unit] Description=Gunicorn instance to serve myproject After=network.target [Service] User=youruser WorkingDirectory=/path/to/your/project Environment="FLASK_ENV=production" ExecStart=/path/to/your/project/venv/bin/gunicorn -w 4 'app:app' -b 0.0.0.0:8000 Restart=always [Install] WantedBy=multi-user.target
sudo systemctl daemon-reload sudo systemctl start myproject sudo systemctl enable myproject
Check logs with:
sudo systemctl status myproject journalctl -u myproject