To host both PHP and Django sites on the same Apache server, you just need to configure Apache to handle each type of application differently.
Here’s how it works in practice:
π§ Key Idea
Apache can serve:
-
PHP via
mod_phpor PHP-FPM -
Django via a WSGI interface (usually
mod_wsgi)
Each site gets its own virtual host configuration.
π§± Typical Setup
1. PHP Site
Handled normally with PHP enabled:
<VirtualHost *:80>
ServerName php-site.com
DocumentRoot /var/www/php-site
<Directory /var/www/php-site>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
2. Django Site
Uses mod_wsgi to run the Django app:
<VirtualHost *:80>
ServerName django-site.com
WSGIDaemonProcess django_site python-home=/var/www/django-site/venv python-path=/var/www/django-site
WSGIProcessGroup django_site
WSGIScriptAlias / /var/www/django-site/project/wsgi.py
<Directory /var/www/django-site/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static /var/www/django-site/static
<Directory /var/www/django-site/static>
Require all granted
</Directory>
</VirtualHost>
⚠️ Things to Watch Out For
1. Port / Domain Separation
-
Use different domains (recommended), e.g.:
-
example.com→ PHP -
app.example.com→ Django
-
- Or different ports (less common)
2. Python Environment
- Always use a virtual environment for Django
- Keep dependencies isolated from system Python
3. Performance Considerations
- PHP runs inside Apache (or via PHP-FPM)
-
Django runs as a separate WSGI process
➡️ They won’t conflict, but you need enough memory for both
4. Static Files
- Apache can serve static files for both apps
-
Django static files should be collected (
collectstatic)
✅ Alternative (Often Better)
Many modern setups use:
- Apache or Nginx as a reverse proxy
-
Django served via:
-
gunicornoruwsgi
-
- PHP via PHP-FPM
This gives better scalability and separation.
π§ Bottom Line
Yes—you can run both on the same Apache server by:
- Using VirtualHosts
- Enabling both PHP support and mod_wsgi
Host Php/Django On Ubuntu
On Ubuntu this is very straightforward. I’ll walk you through a clean, practical setup where Apache hosts both a PHP site and a Django app.
π§° 1. Install Required Packages
sudo apt update
sudo apt install apache2 php libapache2-mod-php python3 python3-venv python3-pip libapache2-mod-wsgi-py3
Enable Apache modules:
sudo a2enmod wsgi
sudo a2enmod rewrite
sudo systemctl restart apache2
π 2. Set Up Your PHP Site
Create a directory:
sudo mkdir -p /var/www/php-site
sudo chown -R $USER:$USER /var/www/php-site
Test PHP:
echo "<?php phpinfo(); ?>" > /var/www/php-site/index.php
Create config:
sudo nano /etc/apache2/sites-available/php-site.conf
<VirtualHost *:80>
ServerName php.local
DocumentRoot /var/www/php-site
<Directory /var/www/php-site>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Enable it:
sudo a2ensite php-site
π 3. Set Up Your Django App
Create project
mkdir -p /var/www/django-site
cd /var/www/django-site
python3 -m venv venv
source venv/bin/activate
pip install django
django-admin startproject project .
Test locally:
python manage.py runserver
Configure WSGI + Apache
sudo nano /etc/apache2/sites-available/django-site.conf
<VirtualHost *:80>
ServerName django.local
WSGIDaemonProcess django_site python-home=/var/www/django-site/venv python-path=/var/www/django-site
WSGIProcessGroup django_site
WSGIScriptAlias / /var/www/django-site/project/wsgi.py
<Directory /var/www/django-site/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static /var/www/django-site/static
<Directory /var/www/django-site/static>
Require all granted
</Directory>
</VirtualHost>
π¦ 4. Prepare Django for Production
Edit settings:
ALLOWED_HOSTS = ["django.local"]
STATIC_ROOT = "/var/www/django-site/static"
Then:
python manage.py collectstatic
π 5. Enable the Django Site
sudo a2ensite django-site
sudo systemctl reload apache2
π§ͺ 6. Local Testing (Important)
Edit your hosts file:
sudo nano /etc/hosts
Add:
127.0.0.1 php.local
127.0.0.1 django.local
⚠️ Common Gotchas
π Permissions
sudo chown -R www-data:www-data /var/www/django-site
π Static files not loading?
Make sure:
-
collectstaticwas run -
Apache
Alias /staticis correct
π§ mod_wsgi vs modern setup
This works well, but many production setups prefer:
- Apache → reverse proxy
- Django → Gunicorn
- PHP → PHP-FPM
More scalable, but slightly more complex.
✅ Final Result
You’ll have:
-
http://php.local→ PHP site -
http://django.local→ Django app
Running on the same Apache server without conflict.
No comments:
Post a Comment