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

I can currently run either Django through mod_wsgi or PHP on my Apache server.
My Django projects run at: http://localhost and source is at C:/django_proj
My PHP projects run at: http://php.localhost and source is at C:/web
If I turn both on, php.localhost and localhost go to the Django project. I've already set them up through Apache virtual hosts.
Here are some relevant lines in httpd.conf:
DocumentRoot "C:/web"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<Directory "C:/web">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "C:/django_proj">
Order allow,deny
Allow from all
</Directory>
Include "C:/django_proj/apache/apache_django_wsgi.conf"
The relevant lines in apache_django_wsgi.conf is:
WSGIScriptAlias / "C:/django_proj/apache/proj.wsgi"
<Directory "C:/django_proj/apache">
Order allow,deny
Allow from all
</Directory>
Inside httpd-vhosts.conf:
<Directory C:/web>
Order Deny,Allow
Allow from all
</Directory>
<Directory C:/django_proj>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:/django_proj"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/web"
ServerName php.localhost
</VirtualHost>
My PHP project is current inaccessible. Does anyone have any ideas what I'm missing?

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.

Your WSGIScriptAlias / ... directive is telling Apache that everything request starting with "/" should get fed through Django's WSGI handler. If you changed that to read WSGIScriptAlias /django-proj/ ... instead, only requests starting with "/django-proj" would get passed into Django.
An alternative would be to start setting up virtual hosts for each project. This way you could configure Apache to put each project at the / of it's own domain, and you wouldn't need to worry about the configuration for one project affecting one of your other projects.

I had the same problem.
Try removing this block <Directory /> in httpd-conf.
Include httpd-vhost.conf and and try puting my WSGIScriptAlias / "/somewhere/file.wsgi" in virtual host section of httpd-vhosts which listens to port 80.

I would like to add that if you are using Apache ProxyPass, it's possible to deny certain URL patterns so that it falls to mod_php.
ProxyPass /wordpress !
<Location /wordpress>
Require all granted
</Location>

Related

Apache+mod_php so slow

I have a problem setting up my Apache server with mod_php. I have a website whose index.php page is generated in ~200ms. But Apache serves it in about 1.5sec. Why is this slowdown taking place?
If needed, my /etc/apache2/ports.conf file is:
Listen 80
<VirtualHost *:80>
ServerName mysite.local
ServerAlias www.mysite.local
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:9999/
ProxyPassReverse / http://localhost:9999/
</VirtualHost>
My file /etc/apache2/sites-available/mysite.local.conf is:
Listen 9999
User max
Group max
<VirtualHost *:9999>
ServerName 127.0.0.1:9999
DocumentRoot /home/max/www/mysite.local/www/html/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/max/www/mysite.local/www>
Options FollowSymLinks
AllowOverride All
#Order allow,deny
#Allow from all
Require all granted
</Directory>
ErrorLog /home/max/www/mysite.local/logs/error.log
LogLevel warn
CustomLog /home/max/www/mysite.local/logs/access.log combined
</VirtualHost>
My main question is why is this so slow and how can make it faster? I have tested putting the index.php content into a plain html file, and it is served in ~10ms. so the problem is probably with mod_php? Thanks in advance.
A reason could be that apache checks for .htaccess files recursively within each directory of the root for each request. apache is used very widely but not very fast or even optimized.
I definitely would suggest an nginx server combined with php-fpm. The configuration is very straight forward and if you're familiar with apache, you should not have problems using nginx. How To: https://tecadmin.net/setup-nginx-php-fpm-on-ubuntu-20-04/

How to run Django and Php on one Apache2?

I want to run my Django app and mediawiki at same domain on one Apache2.But I find there is a problem hard to solve.
In my config, localhost and localhost/mediawiki can normal access, but when I access to localhost/dblog(It's my Django app's directory), browser shows "Bad Request (400)".
I trid to google and another related questions at stackoverflow, but still can't solve this problem.
Here is my Apache2 config:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
ServerName localhost
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www>
Options ALl
AllowOverride All
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess dblog python-path=/var/www/dblog:/usr/lib/python2.7/site-packages
WSGIProcessGroup dblog
WSGIScriptAlias /dblog /var/www/dblog/dblog/wsgi.py
<Directory /var/www/dblog>
Options +ExecCGI
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
</VirtualHost>
Thank you!

Serve documents from alternative document root

I would like Apache to serve documents from c:/Apache24/htdocs and d:/www resp on my Windows local machine. In httpd-vhosts.conf I'm trying:
<VirtualHost *:80>
DocumentRoot "c:/Apache24/htdocs"
ServerName test.localhost
Options Indexes FollowSymLinks
<Directory "D:/www">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
and in hosts
127.0.0.1 test.localhost
but browsing to http://test.localhost results in a list of directories under C:/htdocs being displayed?
Am I getting any closer with this:
Alias c:/Apache24/htdocs D:/www
<Directory c:/Apache24/htdocs>
Require all granted
</Directory>
If so, where do I add it? N.B. I want to store and serve web documents from D:/ since C:/ is getting rather full now.
check you have row: NameVirtualHost *:80 before your virtual host. That should usually be there already, but check it.
Change your host to point just into one folder like:
<VirtualHost *:80>
DocumentRoot "D:/www"
ServerName localserver
Options Indexes FollowSymLinks
<Directory "D:/www">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
Create symbolic link from your c:/Apache24/htdocs to for example D:/www/apache folder.
Then you can access to your c:/Apache24/htdocs folder from url:
localserver/apache
Read more about symlinks:
http://en.wikipedia.org/wiki/NTFS_symbolic_link
Another option is to create two urls (domains) and virtualhosts like (add this after above):
<VirtualHost *:80>
DocumentRoot "c:/Apache24/htdocs"
ServerName other.localserver
Options Indexes FollowSymLinks
<Directory "c:/Apache24/htdocs">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
and in your hosts:
127.0.0.1 localserver other.localserver
On Windows 7 with WAMP installed on C:\Apache24 and serving documents out of C:\Apache24\htdocs you may want to serve documents from the D drive instead, since C is getting full. Go to: C:\Apache24\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "D:/www/apache/site1"
ServerName site1.dev
<Directory "D:/www/apache/site1">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/www/apache/site2"
ServerName site2.dev
<Directory "D:/www/apache/site2">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
And edit: C:\Windows\System32\drivers\etc\hosts
127.0.0.1 site1.dev
127.0.0.1 site2.dev
You should now be able to serve (locally) you're web documents from D:\www\apache with no need for sym links.

Apache Gives me 403 Error when trying to access "/" on server

When I try to change the web root path to any other folder it give me a 403 error (Forbidden), The default path is /Library/WebServer/Documents, but since this rather annoying for me, I changed it too /Users/Xero/WebServer
Permissions I got the permissions from /Library/WebServer/Documents by right clicking and selecting Get info (http://i.imgur.com/f21D7z0.png What it looks like)
So I changed it to be the exact same for /Users/Xero/WebServer. But it give me a 403 error still.
Attempt to fix
So, I looked it up on Google, and I tried these solutions to fix my problem:
http://www.cyberciti.biz/faq/apache-403-forbidden-error-and-solution/
Error message "Forbidden You don't have permission to access / on this server" (Stack Overflow)
But they didn't help.
Other Notes
Remember I'm using the preinstalled Apache on OS X Snow leopard. And, my virtual host isn't local, so it's not part or the hosts file issue (It's hosted off my DNS, and my IP as a backup, neither work)
My Virtual Hosts:
<VirtualHost *:80> ( Edited 2 )
DocumentRoot "/Users/Xero/WebServer"
</VirtualHost>
<VirtualHost *:80>
Options Indexes FollowSymLinks Includes ExecCGI
DocumentRoot "/Users/Xero/WebServer"
ServerName wrks.tk
ErrorLog "/Logs/Workarea/wrks-err"
CustomLog "/Logs/Workarea/wrks-acc" common
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml
<Directory "/Users/Xero/WebServer">
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Xero/WebServer"
ServerName 209.169.203.53
ErrorLog "/Logs/Workarea/ip"
CustomLog "/Logs/Workarea/ip-acc" common
<Directory "/Users/Xero/WebServer">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Also, I have my httpd.conf User and Group setup like this:
User _www
Group _www
Responses to Answers
Edits
So, I fixed the path's to the document's, but it still giving me the same error, 403.
I checked my FireWall settings and it's off. I also checked my `Sharing Preferences`, `File Sharing` and `Web Sharing` are on.
I edited the duplicate of `DirectoryIndex Home.php Home.html` in the `wrks.tk` VirtualHost, but still, I'm getting the same error. (See Other Notes)
Added more too Other Notes.
Fixed directory, but still didn't fix problem.
Added another to Answer Responses.
Tried a computer restart, did not work.
I put a `index.html` in the folder, still got the same 403 error, I tried accessing `/index.html`, still, 403.
What file exactly are you getting all of this from?
<VirtualHost *:80> ( Edited 2 )
DocumentRoot "/Users/Xero/WebServer"
</VirtualHost>
<VirtualHost *:80>
Options Indexes FollowSymLinks Includes ExecCGI
DocumentRoot "/Users/Xero/WebServer"
ServerName wrks.tk
ErrorLog "/Logs/Workarea/wrks-err"
CustomLog "/Logs/Workarea/wrks-acc" common
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml
<Directory "/Users/Xero/WebServer">
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/Xero/WebServer"
ServerName 209.169.203.53
ErrorLog "/Logs/Workarea/ip"
CustomLog "/Logs/Workarea/ip-acc" common
<Directory "/Users/Xero/WebServer">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
There are more than one file for those settings. If you have apache, then take a look in the sites-enabled and sites-available folders.
Change the bottom part of your httpd.conf file to:
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
Make a new file index.html and put the following in it:
<!DOCTYPE html>
<html>
<body>
<h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body>
</html>
Then place the index.html file into the empty directory.
create a file in /etc/apache2/users/ called yourusername.conf (yourusername being the account short name, e.g. hulu – it's usually the name of your home folder in /Users) with the following contents:
<Directory "/Users/yourusername/Sites/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

Virtualhost Apache, Rails and PHP

I am trying to have a Rails application to coexist on one server along with a PHP page. I am using Apache installed through Passenger.
So, I have created a virtualhost file for my php page, and it is as such:
<VirtualHost *:80>
ServerAdmin mail#mail.com
ServerName subsite.site.com
DocumentRoot "/var/www/subsite"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/subsite">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
My problem is that this mysite.site.com keep being redirected to the main rails site. Any idea?

Categories