How to Run Django and PHP Together on One Apache Server

How do I run Django and PHP together on one Apache server?

I run dozens of mod_wsgi/Django sites, PHP sites, and a Rails site with a single Apache.

It's mostly done using virtual hosts but I have some that are running both on the same domain.

You just need to put your WSGIScriptAlias /... after any other Location/Alias directives.

Lets say, for example, I want to run phpMyAdmin on the same domain as a Django site. The config would look something like this:

Alias /phpmyadmin /full/path/to/phpmyadmin/
<Directory /full/path/to/phpmyadmin>
Options -Indexes
...etc...
</Directory>

WSGIScriptAlias / /full/path/to/django/project/app.wsgi
<Directory /full/path/to/django/project>
Options +ExecCGI
...etc...
</Directory>

Edit:

Your configuration should look something like this:

<VirtualHost *:80>
DocumentRoot "C:/django_proj"
ServerName localhost
WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "C:/web"
ServerName php.localhost
Alias / C:/web
<Directory C:/web>
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>

You don't need those <Directory> directives in http.conf... do all your configuration in the Virtual hosts.

Also, completely get rid of the <Directory /> block.

Run both Django and PHP application in Apache

Few issues, first thing is that the ServerName is just a servername and not a url. The second issue is that you should combine the two VirtualHost entries.

<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName domain.com
Alias /wp /var/www/html/wp
<Directory /var/www/html/wp>
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>

Alias /static /var/www/html/portal/static
<Directory /var/www/html/portal/static>
Require all granted
</Directory>

# this really should be a sub directory of /var/www/html
# if your server config follows symlinks, just make a symlink
<Directory /home/ubuntu/portal/portal>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIDaemonProcess portal python-path=/home/ubuntu/portal:/home/ubuntu/portal/env/lib/python2.7/site-packages
WSGIProcessGroup portal
WSGIScriptAlias / /home/ubuntu/portal/portal/wsgi.py
</VirtualHost>

Django and PHP together in server with single ip and port only

I think this should just work:

<VirtualHost *:8081>
WSGIScriptAlias /app/ /var/www/abc/index.wsgi

Alias /static/ /var/www/abc/static/
<Location "/static/">
Options -Indexes
</Location>
LogLevel warn
CustomLog /var/log/apache2/access.log combined

DocumentRoot /var/www
</VirtualHost>

That makes addresses starting with /app/ to be served from a wsgi handler, addresses staring with /static/ from /var/www/abc/static/ directory, and everything else is served form /var/www.

There is however a huge security issue with your setup. You should not keep your Django project in a folder that is inside a DocumentRoot folder. You are making all your source code and settings (including database passwords and cookie signing secret keys!) accessible to anyone. Move the Django project away from /var/www immediately.

Django/mod_wsgi and PHP as Virtual Hosts on same Apache Server using MAMP

A couple of problems to start with:

  1. ServerName is mean to specify the host name not a URL path.
  2. You should never set DocumentRoot to be where your Django site source code is.

Deploy a Django site and a PHP site on the same server with Apache and mod_wsgi

Using apache´s virtualhosts, here I put an example of something similar in a server of mine, in which I have a djangp app in the main domain and a joomla in a subdomain. Both files are located in /etc/apache2/sites-enabled

Joomla´s apache conf file (named /etc/apache2/sites-enabled/manual.domain.com):

NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin dsanabria@domain.com
ServerName manual.domain.com

DocumentRoot "/home/ubuntu/manual/"

<Directory /home/ubuntu/manual/>
Order deny,allow
Allow from all
</Directory>

ErrorLog /var/log/apache2/manual.domain-error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug

CustomLog /var/log/apache2/manual.domain-access.log combined

</VirtualHost>

And the django app (named /etc/apache2/sites-enabled/www.domain.co):

NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin diego@diegue.us
ServerName domain.co
ServerAlias machete.anotherdomain.com
Alias /admin/media/ /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/
Alias /media/ /home/ubuntu/webapps/machete/machete/media/
Alias /static/ /home/ubuntu/webapps/machete/machete/collected/

<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/grappelli/media/>
Order deny,allow
Allow from all
</Directory>

<Directory /home/ubuntu/webapps/machete/lib/python2.7/site-packages/django/contrib/admin/media/ >
Order deny,allow
Allow from all
</Directory>

<Directory /home/ubuntu/webapps/machete/machete/media/>
Order deny,allow
Allow from all
</Directory>

<Directory /home/ubuntu/webapps/machete/machete/collected/>
Order deny,allow
Allow from all
</Directory>

WSGIScriptReloading On
WSGIDaemonProcess machete python-path=/home/ubuntu/webapps/machete/lib/python2.7/site-packages
WSGIProcessGroup machete
WSGIApplicationGroup machete
WSGIPassAuthorization On

WSGIScriptAlias / /home/ubuntu/webapps/machete/machete/machete/wsgi.py
ErrorLog /var/log/apache2/machete-error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug

CustomLog /var/log/apache2/machete-access.log combined

</VirtualHost>

The first, tells to apache, that if the user gets to manual.domain.com, just response with a php application (joomla). The second file says to apache, that if the user calls the server with www.domain.com response with a python wsgy, (django).

This is in a ubuntu server, redhat/centos/fedora locates the folder sites-enabled in another location that I can´t remember, but anyway you can use virtualhosts.

Generraly, I avoid to mess with the httpd.conf file and prefer use virtualhosts.

PHP and Django on same Apache server

You should remove the trailing slash from /dj/, turning it in to a file path and not a directory path

The / makes a big difference. It essentially makes it a directory mapping:

Where the target is a directory-path, URLs with a case-sensitive
(%-decoded) path beginning with URL-path will be mapped to scripts
contained in the indicated directory
.

However, if you remove the trailing /:

Where the target is a file-path, URLs with a case-sensitive
(%-decoded) path beginning with URL-path will be mapped to the script
defined by the file-path
.

Once you remove the /, all paths are send to the wsgi.py handler (because its mapping to a file, not a directory), and thus django's url machinery can start working.

If you keep the /, then Apache is trying to find a file /dj/products in the directory path.

Host both PHP/JS and Django applications with Apache on Windows

WSGIScriptAlias /  "d:\projects\dashboard\dashboard\wsgi_windows.py"

will also catch calls to "d:/projects" - so if you want to avoid that, you need to change to something like

WSGIScriptAlias /my_wsgi_app/  "d:\projects\dashboard\dashboard\wsgi_windows.py"

If you want to avoid that the user can see that, you can use a rewrite rule for certain paths.



Related Topics



Leave a reply



Submit