In the previous post, we wrote an HTTP server script that has one drawback – it doesn’t start automatically and doesn’t work as a service.
Here are the steps to configure it as a service in Linux.
- Script: We already have a startup script named web-server.py. Save it in the directory where it will be permanently stored. I pasted it in the path /root.
- Execution permissions: In the previous post, we already granted execution permissions, but it’s worth mentioning here as well::
1 |
chmod +x web-server.py |
- Initialization script: Debian uses systemd. So, go to the /etc/systemd/system/ directory and create a file named web-server.service. You can choose a different name for your service 🙂 An example configuration is provided below
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[Unit] Description=Description of service After=network.target [Service] ExecStart=/root/web-server.py WorkingDirectory=/root/ Restart=on-failure User=username #I used root user account Group=usergroup # for the root user you can remove this line [Install] WantedBy=multi-user.target |
- Starting the service: After creating the service, you need to start the script and add it to the systemd system.
1 2 |
sudo systemctl start web-server sudo systemctl enable web-server |
After completing these steps, your Python-based server should start automatically upon machine restart.