Publish a web application using Python's Flask framework on Raspberry Pi. At that time, SSL is also supported by linking with Apache using WSGI.
This article is based on the assumption that the initial settings of Raspberry Pi, domain acquisition, Apache2 installation, port opening, SSL certificate acquisition, etc. have been completed. If you have not done so, we recommend that you make those settings first.
-Easy home server with raspberry pi web server version --Qiita -Made the Raspberry Pi web server compatible with https --Qiita
Install mod_wsgi with the following command: Please note that if the Python version is 2 or less, the one to be installed is different.
$ sudo apt-get install libapache2-mod-wsgi-py3
If Python version is 2 or less
$ sudo apt-get install libapache2-mod-wsgi
If you don't have the Flask framework installed in Python, install it.
$ sudo pip3 install Flask
The file structure of the Flask application is as follows.
└ var/
└ www/
└ flask/
├ app.py
├ app.wsgi
└ templates/
└ test.html
app.py
#!/usr/bin/ python3
# -*- coding: utf-8 -*-
#Flask framework import
from flask import Flask
#Template engine import
from flask import render_template
#Creating a Flask instance
app = Flask(__name__)
#routing
@app.route('/')
def index():
return render_template('test.html')
#Execution content when the application starts
if __name__ == '__main__':
app.run()
app.wsgi
#!/usr/bin/ python3
# -*- coding:utf-8 -*-
import sys
#Specifying the path
sys.path.insert(0, '/var/www/flask')
from app import app
application = app
test.html
<html>
<body>
<h1>Hello Flask!</h1>
</body>
</html>
Enter the command and check if the app works properly
$ python3 /var/www/flask/app.py
Finally, if you see Running on http://127.0.0.1: <port number>/(Press CTRL + C to quit)
, you are successful.
Open another terminal as it is, enter the following command and check if html can be obtained normally.
$ curl localhost:<port number>
If you can get the contents of test.html, it is successful. Press CTRL + C to quit the app once.
Create the following files in/etc/apache2/sites-available /. Please do it with root privileges.
flask_wsgi.conf
#IfModule is not used for code simplification
#Redirect http communication to https communication
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,END]
</VirtualHost>
#https communication settings
<VirtualHost *:443>
ServerName <Server name>
ServerAdmin <Administrator email address>
DocumentRoot /var/www/flask
WSGIDaemonProcess app user=<username> group=<User group> threads=5
WSGIScriptAlias / /var/www/flask/app.wsgi
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile <SSL certificate fullchain.pem path>
SSLCertificateKeyFile <SSL certificate prevkey.pem path>
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<Directory /var/www/flask/>
WSGIProcessGroup app
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Then enter the following command to enable the config.
$ sudo a2ensite flask_wsgi
Check /etc/apache2/sites-enabled to see if other config files (such as 000-default.conf) are enabled. If the server names are not clearly separated, there is a possibility of conflict, so disable it with the following command.
$ sudo a2dissite <Other config files>
Once you've done that, restart Apache.
$ sudo service apache2 restart
If you can restart without error, open your browser and check. If you can get the contents of test.html normally with your browser, it is successful. Thank you for your hard work.