DEV Community

Cover image for How to Host a Full PHP Web Server for FREE in 2026 (LAMP Stack on Hugging Face)
Aman Kumar
Aman Kumar

Posted on

How to Host a Full PHP Web Server for FREE in 2026 (LAMP Stack on Hugging Face)

Let’s be real—setting up a traditional VPS or paying monthly for shared hosting just to run some personal PHP projects, APIs, or lightweight apps can be a hassle. As developers, we want to focus on hands-on implementation and real-world logic, not server administration bills.

I wanted a complete, production-like PHP environment that I could deploy anywhere for free.

Since platforms like Hugging Face Spaces offer free Docker hosting (primarily meant for AI Python apps), I decided to push the limits. I built a single-container Alpine LAMP stack that runs perfectly on Hugging Face, complete with MariaDB, PHP 8.x (FPM), Apache (MPM Event), and even an integrated Web Terminal.

Here is how you can set it up for yourself. 🚀

📦 What's Inside the Box?

Instead of the standard "one process per container" Docker rule, I engineered a lightweight Alpine image that handles everything gracefully using tini and a custom supervisor script.

  • 🐘 PHP 8.x (FPM): High-performance runtime with OPcache.
  • 🌐 Apache (MPM Event): High-concurrency setup with mod_rewrite.
  • 🗄️ MariaDB: Database engine + phpMyAdmin pre-configured.
  • 📁 Custom File Manager & Web Terminal: Direct browser-based control.
  • 🎬 Media Ready: FFmpeg and ImageMagick pre-installed.

You can check out the full source code and Dockerfile here:
👉 GitHub Repo: free-web-hosting (Drop a ⭐ if you find it useful!)


☁️ How to Deploy on Hugging Face Spaces (For Free)

Step 1: Create the Space

  1. Go to Hugging Face Spaces and click Create new Space.
  2. Give it a name (e.g., my-php-server).
  3. Select Docker as the SDK and choose Public visibility.
  4. Click Create.

Step 2: Upload the Dockerfile

You don't need to upload your entire website code yet. Just grab the Dockerfile from my GitHub repository and upload it directly into your Space's Files tab.

Step 3: Set Your Environment Variables

Go to Settings → Variables and Secrets and configure your database and paths. This keeps your server secure from the public:

  • MYSQL_USER = admin
  • MYSQL_PASSWORD = super-secret-password
  • MYSQL_DATABASE = myapp
  • SQL_PATH = hidden-db-panel (Changes phpMyAdmin URL)
  • FILES_PATH = hidden-file-panel (Changes File Manager URL)

Step 4: Add Persistent Storage (Crucial!)

Hugging Face containers are ephemeral—they reset on rebuilds. To save your databases and website files:

  1. Go to Settings → Persistent Storage.
  2. Add a mount path at /data.
  3. Set Visibility to Private.

Boom! Your space will build, and you now have a live PHP server.


🔀 The Magic Trick: Custom Domains via Cloudflare

Hugging Face Spaces use iframe embedding on their main site, which causes terrible "login loops" with phpMyAdmin and blocks cookies if you try to CNAME it directly.

To fix this, we can use a Cloudflare Worker to proxy the domain, rewrite the headers, and fix the SameSite cookie issues.

Just create a free Cloudflare Worker, attach your custom domain, and paste this routing logic:

export default {
  async fetch(request) {
    try {
      const url = new URL(request.url);
      // Replace with your HF Space hostname
      const backendHost = "your-space.hf.space"; 
      const backendUrl = new URL(request.url);

      backendUrl.hostname = backendHost;
      backendUrl.protocol = "https:";

      const newHeaders = new Headers(request.headers);
      newHeaders.set("X-Forwarded-Host", url.hostname);
      newHeaders.set("X-Forwarded-Proto", "https");

      // ... [Full Worker Script available in the GitHub Repo README] ...

      return new Response(response.body, {
        status: response.status,
        statusText: response.statusText,
        headers,
      });
    } catch (err) {
      return new Response(`Worker Error: ${err.message}`, { status: 500 });
    }
  },
};
Enter fullscreen mode Exit fullscreen mode

(You can find the complete, copy-pasteable Cloudflare Worker code in the repository's README.)

🛠️ Uploading Your Code

Once your server is up, just navigate to https://your-domain.com/hidden-file-panel (or whatever you set FILES_PATH to).
Use the built-in File Manager to upload your .zip project directly to /var/www/localhost/htdocs, extract it using the Web Terminal, and you are live!

Final Thoughts

Owning your infrastructure shouldn't require an expensive monthly subscription. With a bit of Docker magic and Cloudflare routing, you can host robust, real-world applications entirely for free.

If this guide helped you out, I’d love it if you could star the GitHub repository. Feel free to fork it, modify it, and build something awesome.

Happy coding!


P.S. Looking for a modern, real-time messaging experience? Also visit Talkrush.

Top comments (0)