I have a task to add certificates to my Flask server. The server is responsive with HTTP requests.
Does anybody have experience how to set up Flask HTTPS server?
I generated private and public keys, with the certbot tool. Information about this tool are here: https://certbot.eff.org/instructions?ws=webproduct&os=snap
In summary, certbot
allows you to create private and public keys encoded as .pem
files. Which are then used for encrypting and decrypting the traffic which is exchanged between client and server.
I use the waitress library for serving the content to the clients.
This is the line which does this job:
serve(app, host='0.0.0.0', port=80)
Does anybody know how to transition from here to the state where somebody can access my page with HTTPS protocol? In other words, do HTTPS handshake and exchange the data?
I tried with this line:
serve(app, host='0.0.0.0', port=5000, url_scheme='https',
certfile='path/to/certfile.pem', keyfile='path/to/keyfile.pem')
Which I found this line with perplexity:
https://www.perplexity.ai/search/run-flask-https-server-jPPLZHcERamQ5YBqFlVIBQ
Try configuring Nginx
-
Run Flask at http://localhost:5000
-
Create config file in
/etc/nginx/sites-available/yourdomain
server {
server_name yourdomain.com;
listen 80;
location / {
proxy_pass http://localhost:5000;
}
}
-
Create symlink
sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled/
-
Install and run Certbot with
sudo certbot --nginx -d yourdomain.com
command -
Reload Nginx
sudo systemctl restart nginx.service