Python's built-in SimpleHTTPServer
(or http.server
in Python 3) is fantastic for quick local development, but it has a critical limitation:
python -m SimpleHTTPServer 8000 # Python 2
python -m http.server 8000 # Python 3
This server only handles static files (HTML, CSS, JS) and doesn't support server-side processing like PHP. When you try to access a PHP file, it will either download the file or display its raw source code.
Option 1: Use PHP's Built-in Server
For PHP development, the simplest solution is to use PHP's own development server:
php -S localhost:8000
This will handle PHP files properly while still serving static files. You can even specify a router script for advanced URL handling:
php -S localhost:8000 router.php
Option 2: Configure a Proper Local Server
For more robust development, consider installing:
- XAMPP (cross-platform)
- MAMP (Mac)
- WAMP (Windows)
- Docker with PHP/Apache or PHP/Nginx
Option 3: Proxy Approach (Advanced)
If you must use Python alongside PHP, you could set up a proxy:
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
from urllib.request import urlopen
class PHPProxyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('.php'):
php_response = urlopen(f"http://localhost:9000{self.path}")
self.send_response(200)
self.end_headers()
self.wfile.write(php_response.read())
else:
super().do_GET()
# Run PHP server in another terminal: php -S localhost:9000
TCPServer(('', 8000), PHPProxyHandler).serve_forever()
Use Case | Recommended Solution |
---|---|
Quick PHP testing | PHP built-in server |
Static file serving | SimpleHTTPServer |
Full web app development | XAMPP/MAMP/Docker |
Special Python+PHP integration | Proxy solution |
Never use any of these development servers in production. They lack security features and performance optimizations needed for public-facing applications.
Python's built-in SimpleHTTPServer
(or http.server
in Python 3) is fantastic for quick static file serving, but it has no native PHP processing capability. When you run:
python -m SimpleHTTPServer 8000
# or Python 3:
python3 -m http.server 8000
It will only serve raw PHP files as plain text, not execute them. This is because Python's HTTP server modules don't include a PHP interpreter.
For proper PHP development, consider these alternatives:
PHP's Built-in Development Server
The simplest solution is to use PHP's own development server:
php -S localhost:8000
This handles PHP files natively and is available in PHP 5.4+.
Docker-based Solution
For a more production-like environment with minimal setup:
docker run -p 8000:80 -v "$PWD":/var/www/html php:apache
This gives you a full Apache/PHP stack in one command.
If you must use Python's server but need PHP, you can set up a proxy:
- Run PHP's server on port 9000
- Run this Python proxy script on port 8000:
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
from urllib.request import urlopen
class ProxyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('.php'):
resp = urlopen(f"http://localhost:9000{self.path}")
self.send_response(resp.status)
self.end_headers()
self.wfile.write(resp.read())
else:
super().do_GET()
TCPServer(("", 8000), ProxyHandler).serve_forever()
- Never use these solutions in production
- For serious development, consider proper stacks like XAMPP, MAMP, or Laravel Valet
- The proxy method has performance overhead
- PHP's built-in server has some limitations with URL rewriting
For most PHP development needs, using PHP's built-in server or Docker will provide the best combination of simplicity and functionality.