Skipole WSGI generator.
Following the previous article "Simple serving for a Raspberry Pi" - this page suggests some variations.
You may not want to use root to run the web server, particularly in a teaching situation, however root is generally needed to serve on ports less than 1024. If you don't mind serving, on say port 8000, then you could run Waitress as a normal user.
It is also convenient to load Python software from Pypi into a virtual environment rather than using apt-get. This allows the latest software to be used, and by deleting a user directory you can remove all the software installed.
The following example assumes a user 'student' and a project called 'myproj.py' held within '/home/student/projectfiles'
In his home directory (/home/student) the user creates the virtual environment 'myenv'.
python3 -m venv myenv
Then activate the environment, and install skipole and waitress:
source myenv/bin/activate
pip install waitress
pip install skipole
deactivate
If not already done, copy the skipole project to /home/student/projectfiles
The project Python file myproj.py needs to be edited, remove skilift, skiadmin and the development server and add the lines:
from waitress import serve serve(application, host='0.0.0.0', port=8000)
So 'application' is served on all interfaces with port 8000. The owner of all files remains 'student'.
myproj.py needs to know where to find skipole and waitress, so alter the very first line of the file to the shebang:
#!/home/student/myenv/bin/python3
and make the file executable with:
chmod 744 ~/projectfiles/myproj.py
This allows the user to run myproj.py directly, and it will call on the correct version of Python, skipole and waitress.
From the users home, he only needs to type:
projectfiles/myproj.py
And his web service should be served on port 8000
To run on startup, the pi root user needs to create a service for the student, this is shown below:
As root, or using sudo a lot, create a file:
/lib/systemd/system/myproj.service
containing the following:
[Unit] Description=My project description After=multi-user.target [Service] Type=idle ExecStart=/home/student/projectfiles/myproj.py User=student WorkingDirectory=/home/student/projectfiles Restart=on-failure # Connects standard output to /dev/null StandardOutput=null # Connects standard error to journal StandardError=journal [Install] WantedBy=multi-user.target
Enable the service
systemctl daemon-reload
systemctl enable myproj.service
systemctl start myproj
This starts /home/student/projectfiles/myproj.py, and it will also start on boot up.