如何在同一Web服务器部署Python应用与PHP/WordPress并共用域名?
Great question! Running WordPress (PHP) and Python apps (Flask, Django, eel, WebSockets) side-by-side on the same domain is totally feasible—especially since you’re okay with separate server processes. Reverse proxying is the go-to solution here, and it works with almost every mainstream hosting provider. Let’s break this down step by step:
The idea is simple: use a primary web server (Nginx or Apache, which most hosts support) to handle all incoming requests to your domain. It’ll route requests to /wp/ to your WordPress installation, and forward requests to /py/ to your Python app’s server (running on a local port like 8000). This keeps both processes separate but working in sync.
Option 1: Nginx as the Primary Reverse Proxy
Nginx is lightweight and excellent at reverse proxying—ideal for this setup.
Step 1: Set Up WordPress on /wp/
- Install WordPress in a subdirectory of your web root (e.g.,
/var/www/html/wp/for self-hosted, orpublic_html/wp/for cPanel hosts). - In WordPress admin, go to Settings > General and set both
Site URLandHome URLtohttps://yourdomain.com/wp/to avoid path conflicts.
Step 2: Run Your Python App on a Local Port
Never use Flask/Django’s built-in runserver in production—use a stable process manager instead:
- Flask:
gunicorn -w 4 -b 127.0.0.1:8000 app:app(adjustapp:appto match your app’s entry point) - Django:
gunicorn myproject.wsgi:application -b 127.0.0.1:8000 - eel: Start your app with
eel.start('index.html', port=8000, host='127.0.0.1') - WebSockets (e.g., FastAPI): Bind to
127.0.0.1:8000as usual—Nginx can handle WebSocket upgrades with extra config.
Step 3: Configure Nginx to Route Requests
Edit your Nginx site config (e.g., /etc/nginx/sites-available/yourdomain.conf) with this setup:
server { listen 80; server_name yourdomain.com www.yourdomain.com; return 301 https://$server_name$request_uri; # Redirect HTTP to HTTPS } server { listen 443 ssl; server_name yourdomain.com www.yourdomain.com; # SSL cert paths (use Let's Encrypt or your host's certs) ssl_certificate /path/to/your/cert.pem; ssl_certificate_key /path/to/your/key.pem; # Handle WordPress requests at /wp/ location /wp/ { root /var/www/html; index index.php index.html; try_files $uri $uri/ /wp/index.php?$args; # Pass PHP requests to PHP-FPM location ~ \.php$ { fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Adjust PHP version as needed fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } # Proxy /py/ requests to your Python app location /py/ { proxy_pass http://127.0.0.1:8000/; # Trailing slash preserves path structure proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
- Test the config with
nginx -t, then restart Nginx:systemctl restart nginx
Option 2: Apache as the Primary Reverse Proxy
Apache is common on cPanel and shared hosts. Here’s how to set it up:
Step 1: Set Up WordPress on /wp/
Same as the Nginx setup—install in /wp/ and update WordPress’s site URLs.
Step 2: Run Your Python App on a Local Port
Use gunicorn or uWSGI to bind to 127.0.0.1:8000 (same as above).
Step 3: Enable Required Apache Modules
Enable proxy modules (ask your host if you can’t access the terminal):
a2enmod proxy proxy_http proxy_wstunnel rewrite ssl systemctl restart apache2
Step 4: Configure Routing
For cPanel/Shared Hosting (.htaccess):
Add this to your public_html/.htaccess file:
# WordPress rules for /wp/ RewriteEngine On RewriteBase /wp/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wp/index.php [L] # Proxy /py/ to Python app ProxyPass /py/ http://127.0.0.1:8000/ ProxyPassReverse /py/ http://127.0.0.1:8000/ # WebSocket support ProxyPass /py/ws/ ws://127.0.0.1:8000/ws/ ProxyPassReverse /py/ws/ ws://127.0.0.1:8000/ws/
For Self-Hosted Apache (Virtual Host):
Edit your virtual host config:
<VirtualHost *:443> ServerName yourdomain.com SSLEngine on SSLCertificateFile /path/to/cert.pem SSLCertificateKeyFile /path/to/key.pem # WordPress routing Alias /wp/ /var/www/html/wp/ <Directory /var/www/html/wp/> Options FollowSymLinks AllowOverride All Require all granted </Directory> # Python app proxy ProxyPass /py/ http://127.0.0.1:8000/ ProxyPassReverse /py/ http://127.0.0.1:8000/ # WebSocket proxy ProxyPass /py/ws/ ws://127.0.0.1:8000/ws/ ProxyPassReverse /py/ws/ ws://127.0.0.1:8000/ws/ </VirtualHost>
- Use Production-Grade Python Servers: Avoid Flask/Django’s built-in servers—
gunicornoruWSGIare much more stable for production. - Fix Python App Paths:
- Flask: Set
APPLICATION_ROOT='/py'in your app config, or use a blueprint with the/pyprefix. - Django: Add
FORCE_SCRIPT_NAME = '/py'to yoursettings.pyfile.
- Flask: Set
- Keep Python Apps Running: Use
systemdorsupervisorto auto-start and restart your Python app. Examplesystemdservice file (/etc/systemd/system/python-app.service):
Enable it with[Unit] Description=Python Web App After=network.target [Service] User=www-data WorkingDirectory=/var/www/python-app ExecStart=/usr/bin/gunicorn -w 4 -b 127.0.0.1:8000 app:app Restart=always [Install] WantedBy=multi-user.targetsystemctl enable python-app.serviceand start withsystemctl start python-app.service. - Host-Specific Notes:
- cPanel: If you can’t enable modules yourself, ask your host to turn on
proxy,proxy_http, andproxy_wstunnel. - Shared Hosting: Some hosts restrict reverse proxying—look for providers like SiteGround that support both PHP and Python deployments.
- Docker (Optional): If your host allows Docker, containerize both apps and use Nginx as a proxy container for clean isolation.
- cPanel: If you can’t enable modules yourself, ask your host to turn on
- 404s in Python App: Ensure the trailing slash in
proxy_passis present (e.g.,http://127.0.0.1:8000/instead ofhttp://127.0.0.1:8000). This preserves the path after/py/. - WordPress Path Issues: Double-check WordPress’s
Site URLandHome URLare set tohttps://yourdomain.com/wp/. - WebSocket Failures: Verify
proxy_wstunnelis enabled in Apache, or that the WebSocket headers are added in Nginx.
内容的提问来源于stack exchange,提问作者BinaryMonkey




