Keep Your Python HTTP Server Running: Auto-Start and Crash Recovery Using Linux systemd
In this article, we’ll walk you through the process of setting up a Python HTTP server that starts automatically on boot and restarts if it crashes. This is perfect for serving a static HTML site and ensuring it stays online 24/7. Follow these simple steps, and you’ll never have to worry about manually starting your server again!
Why Use Python’s HTTP Server?
Python’s built-in HTTP server is a quick and easy way to serve static files. It’s perfect for development and small projects. However, by default, it won’t start automatically if your system reboots or if the server crashes. That’s where this guide comes in handy.
What You’ll Learn:
- How to create a systemd service to manage your Python HTTP server.
- How to ensure your server starts on boot.
- How to automatically restart your server if it crashes.
Prerequisites:
- A Linux system (we’ll be using Ubuntu in this example).
- Basic knowledge of the command line.
- Python installed on your system (Python 3 recommended).
Step 1: Organize Your Static Site
First, let’s organize your static site files. Create a directory and place your HTML, CSS, JavaScript, and other assets inside.
mkdir my_static_site
cd my_static_site
touch index.html styles.css script.js
Add some content to index.html
to test:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Static Site</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to My Static Site</h1>
<p>This is a simple static site served with Python HTTP server.</p>
<script src="script.js"></script>
</body>
</html>
Step 2: Start the Python HTTP Server
Navigate to your site directory and start the server:
cd /path/to/my_static_site
python3 -m http.server 8000
Open your browser and go to http://localhost:8000
. You should see your site!
Step 3: Create a systemd Service
Now, let’s create a systemd service to manage our server. This will make sure the server starts on boot and restarts if it crashes.
Create a new service file:
sudo nano /etc/systemd/system/static-site.service
Add the following content to the file:
[Unit]
Description=Python HTTP server for static site
After=network.target
[Service]
ExecStart=/usr/bin/python3 -m http.server 8000 --directory /path/to/your/static/site
WorkingDirectory=/path/to/your/static/site
Restart=always
RestartSec=10
User=your-username
Group=your-group
[Install]
WantedBy=multi-user.target
Replace /path/to/your/static/site
with the actual path to your site directory. Replace your-username
and your-group
with your actual username and group.
Step 4: Reload systemd
Reload systemd to recognize the new service:
sudo systemctl daemon-reload
Step 5: Enable the Service
Enable the service to start on boot:
sudo systemctl enable static-site.service
Step 6: Start the Service
Start the service immediately:
sudo systemctl start static-site.service
Step 7: Verify the Service
Check the status of your service to make sure it’s running:
sudo systemctl status static-site.service
You should see something like this:
● static-site.service - Python HTTP server for static site
Loaded: loaded (/etc/systemd/system/static-site.service; enabled; vendor preset: enabled)
Active: active (running) since [date]; [time]
Conclusion
Congratulations! You’ve set up a Python HTTP server that starts automatically on boot and restarts if it crashes. This setup ensures your static site stays online without manual intervention. Perfect for small projects and development environments!