Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 139 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

![Python](https://img.shields.io/badge/python-3.x-blue.svg)
![License](https://img.shields.io/badge/license-MIT-green.svg)
![Platform](https://img.shields.io/badge/platform-linux-lightgrey.svg)
![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macOS-lightgrey.svg)

A lightweight, Python-based **Custom HTTP Server** that runs as a Linux systemd service. This project is ideal for serving static files, logs, test results, or building RESTful APIs with no external dependencies.
A lightweight, Python-based **Custom HTTP Server** that runs as a **Linux systemd service** or a **macOS `launchd` agent**. Ideal for serving static files, logs, test results, or internal documentation.
This is a minimal yet powerful **custom HTTP server** written in pure Python. It includes MIME-type awareness and seamless integration with **systemd (Linux)** and **launchd (macOS)** for automatic startup on boot.

This is a minimal yet powerful **custom HTTP server** written in pure Python. It includes MIME-type awareness and seamless systemd integration for automatic startup.
---

## ✅ Features

## Features
- **Pure Python** (no external dependencies)
- Serves any local directory
- MIME-type aware (e.g., `.log`, `.tap`, `.xml`, `.html`, `.pdf`, `.md`, etc.)
- Works as:
- A **systemd service** on Linux
- A **launchd agent** on macOS
- Runs on startup
- Configurable via a simple config or plist

- Written in **pure Python** using `http.server` (Python 3.x)
- Serves any directory (configured via `/etc/custom-http-server.conf`)
- Auto-starts on boot
- MIME types for `.log`, `.tap`, `.xml`, `.html`
---

## Installation
## 🐧 Linux Installation (Systemd Service)

```bash
git clone https://github.com/kumarmuthu/custom-http-server.git
Expand All @@ -26,134 +33,208 @@ sudo ./install.sh

---

* To allow users to easily update the **port** and **path** used by the custom HTTP server, you’ve already provided a config file (`/etc/custom-http-server.conf`). Here's how they can update the **port** specifically:

## Step-by-Step: Update Port
### 🔧 Configure Port and Directory

1. **Open the config file:**
Edit the config file:

```bash
sudo vi /etc/custom-http-server.conf
```

2. **Change the value of `SERVE_PORT`:**

You're currently using port `80`, so your config will look like:
Example:

```ini
# Path to serve (change this to your target directory)
SERVE_PATH=/root
SERVE_PORT=80
```

> ℹ️ **Note:** Port `80` is the default HTTP port and requires the service to run as **root**. This is already handled by the systemd unit (`User=root`), so no additional configuration is required for privileged port access.

> For non-root ports like `8080` or `8000`, you can freely change the `SERVE_PORT` without needing elevated permissions.
* Port `80` requires **root** privileges (already handled by systemd).
* You can use any non-privileged port like `8080`, `8000`, etc., without root.

3. **Save and exit**

4. **Restart the service to apply changes:**
Restart to apply changes:

```bash
sudo systemctl restart custom-http-server
```

---

### Verify the Port

You can check whether the server is now running on port 80:
### ✅ Verify

```bash
sudo lsof -i :80
```

Or access it via:

```
http://<your-ip>/
sudo systemctl status custom-http-server
sudo lsof -i :80 # Or your configured port
```

---

Check service status to confirm it's running properly:

```bash
sudo systemctl status custom-http-server
```

## Logs
---

### 📜 Logs

```bash
sudo journalctl -u custom-http-server -f
```

---

## Open Port 80 for Web Access
### 🔓 Allow Port in Firewall

### On **Rocky Linux** (uses `firewalld`):
#### On **Rocky Linux**:

```bash
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload
```

**Verify the port is open:**
#### On **Ubuntu**:

```bash
sudo firewall-cmd --list-ports
sudo ufw allow 80/tcp
sudo ufw reload
```

---

### On **Ubuntu** (typically uses `ufw`):
### ❌ Uninstall (Linux)

```bash
sudo ufw allow 80/tcp
sudo ufw reload
cd custom-http-server/custom-http-server
sudo ./uninstall.sh
```

---

## 🍏 macOS Installation (launchd Agent)

```bash
git clone https://github.com/kumarmuthu/custom-http-server.git
cd custom-http-server/custom-http-server
chmod +x install.sh uninstall.sh macos-launchd-setup.sh
sudo ./install.sh -path /Users/<username> -port 8080
```

**Verify UFW status and rules:**
* `--path` → Directory to serve
* `--port` → HTTP port to use (e.g., 8080)

The script installs a `launchd` service and autostarts it.

---

### ✅ Verify & Logs (macOS)

Check if service is running:

```bash
sudo ufw status
launchctl list | grep custom_httpserver
```

Or:
Watch logs in real time:

```bash
sudo ufw show
watch "cat /tmp/custom_httpserver.log"
```

> **Note:** `firewalld` is not installed by default on Ubuntu. It usually uses `ufw` (Uncomplicated Firewall).
Example `.log` log:

```
muthukumar@muthukumar custom-http-server % cat /tmp/custom_httpserver.log
#########Script Start#########
Resolved serve path:/root /root
Observed exception is: Invalid directory: /root
##########Script End##########
#########Script Start#########
Resolved serve path:/U /U
Observed exception is: Invalid directory: /U
##########Script End##########
#########Script Start#########
Resolved serve path:/Users /Users
Observed exception is: [Errno 48] Address already in use
##########Script End##########
#########Script Start#########
Resolved serve path:/Users/muthukumar /Users/muthukumar
Observed exception is: [Errno 48] Address already in use
##########Script End##########
muthukumar@muthukumar custom-http-server %
```

Or **error** logs:

```bash
watch "cat /tmp/custom_httpserver.err"
```

Example `.err` log:

```
muthukumar@muthukumar custom-http-server % cat /tmp/custom_httpserver.err
10.138.237.56 - - [21/Jun/2025 02:15:19] "GET /Documents/ HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:15:20] "GET /Documents/ HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:15:21] "GET /Documents/Screenshot%202024-12-06%20at%202.21.59%E2%80%AFPM.png HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2025 02:23:27] "GET / HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:23:31] "GET /Documents/ HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:23:32] "GET /Documents/AMD/ HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:23:35] "GET /Documents/AMD/Ubuntu%20VM%20OS%20install.txt HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:29:02] "GET /Documents/AMD/Ubuntu%20VM%20OS%20install.txt HTTP/1.1" 304 -
10.138.237.56 - - [21/Jun/2025 02:29:05] "GET /Documents/AMD/Nutanix_Int_session.txt HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:36:41] "GET /Documents/AMD/Notes5.1.txt HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:42:41] "GET /Documents/ HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:42:43] "GET /Documents/Nutanix/ HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:42:45] "GET /Documents/Nutanix/Notes5.txt HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 02:45:10] "GET /Documents/Nutanix/Install_java.txt HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 03:12:54] "GET / HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 03:12:54] code 404, message File not found
10.138.237.56 - - [21/Jun/2025 03:12:54] "GET /favicon.ico HTTP/1.1" 404 -
10.138.237.56 - - [21/Jun/2025 03:42:44] "GET / HTTP/1.1" 200 -
10.138.237.56 - - [21/Jun/2025 03:42:47] "GET /Documents/ HTTP/1.1" 200 -
muthukumar@muthukumar custom-http-server %
```

---

## Uninstall:
To completely remove the HTTP server:
```bash
cd custom-http-server/custom-http-server
sudo ./uninstall.sh
```
### ❌ Uninstall (macOS)

Manually remove the background service:

```bash
launchctl unload ~/Library/LaunchAgents/com.custom_http_server.plist
rm -f ~/Library/LaunchAgents/com.custom_http_server.plist
launchctl list | grep custom_httpserver # ✅ Should return nothing
```

Or use the provided uninstallation script:

```bash
cd custom-http-server/custom-http-server
sudo ./uninstall.sh
```

---

## ✅ Final Notes
- Built entirely in **Python 3** (no external dependencies)
- Users can easily change the served directory in `/etc/custom-http-server.conf`.
- The Python file and config get copied to `/opt` and `/etc`, making the systemd unit generic.

* Works on **Linux (systemd)** and **macOS (launchd)**
* Built with **pure Python 3**
* No external dependencies
* Easily configurable

---

## License

This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
You are free to use, modify, and distribute it with attribution.

---

## Author

Developed and maintained by [Muthukumar S]
Developed and maintained by \[Muthukumar S]
GitHub: [https://github.com/kumarmuthu/](https://github.com/kumarmuthu/)

---
Loading
Loading