Best affordable and stable method to host Python backend service with PHP frontend?

I’m working on a PHP web application that needs to communicate with a Python backend service. The Python part acts like a background service that listens on port 8765 and handles HTTP requests locally. My PHP frontend connects to it using:

$result = curl_request("http://localhost:8765/api?query=motor+control");

The Python service uses HTTPServer and looks something like:

class RequestHandler(BaseHTTPRequestHandler):
   def do_GET(self):
      if self.path.startswith("/api"):
         self.send_response(200)
         
         # process request here
         
         self.wfile.write(response_data)

This setup works perfectly on our development machines. Now I need to move it to production and I’m considering these steps:

  1. convert the Python service to FastCGI
  2. purchase a budget VPS hosting plan
  3. configure Apache to manage both PHP and Python processes via FastCGI

The Python service should remain internal only and not be accessible from outside. I need a robust solution to ensure it stays running. We’re expecting around 800 requests daily initially, so one instance should handle the load fine.

Could this work on regular shared hosting instead of VPS? Does my deployment strategy make sense for this use case?

have you tried supervisor instead of systemd for process management? it’s gr8 at keeping python services running and handles restarts smoothly. also, why not just use flask with gunicorn? might be simpler than your current httpserver setup for production.

Skip FastCGI completely. I deployed something similar using Docker containers on a basic VPS and it worked perfectly. Your HTTP server approach is way more maintainable than FastCGI here. Just deploy both services in separate containers with Docker Compose, bind your Python service to 127.0.0.1:8765, and use Nginx as a reverse proxy for the PHP frontend while keeping Python internal. For process management, systemd does the job - create a simple service file that auto-restarts your Python service if it crashes. This’ll cost you $5-10/month on DigitalOcean or Linode. Shared hosting won’t work since you need persistent background processes and custom port binding, which most shared hosts don’t allow. 800 daily requests is nothing load-wise, so focus on keeping deployment simple rather than optimizing performance.

Shared hosting won’t work for this - most providers block long-running processes and custom ports. Just grab a cheap VPS and use systemd to keep your Python script running. Way easier than converting to FastCGI. Though if you want something more robust, check out uWSGI - it handles restarts better than the basic HTTP server and plays nice with nginx.