PHP via apache on the server with nginx and django
October 27, 2009 | linux, other, python, toolsauthor: Karol Zielinski | comments: 5 | views: 2801
Tags: administration, apache, django, nginx, php, server
I already described how to configure WordPress on nginx + lighttpd + FastCGI + php (via spawn-fcgi), as well as how to configure php-fpm on server with nginx. Now… I will try to describe alternative way to serve php scripts on the machine with python apps (django) and nginx installed – via installed apache.
Ok, we have apache already installed, so we don’t need to install anything else (like php-fpm or spawn-fcgi) to serve php scripts. We can use installed apache. I prefer to use php-fpm instead of this (because of its performance), however… sometimes it’s good to know that we can “go this way”, too.
So…
I assume that you have apache and nginx already installed.
Now, we need to install all necessary packages (such as php5 for apache):
apt-get install libapache2-mod-php5
if you need something more – install it right now.
Next, we need to create VirtualHost in our apache.
vim /etc/apache2/sites-available/site.com
and add there:
<VirtualHost 127.0.0.1:80>
ServerName www.site.com
ServerAlias site.com
DocumentRoot /opt/php-projects/site.com
<Directory /opt/php-projects/site.com/>
Order deny,allow
Allow from all
</Directory>
LogLevel warn
ErrorLog /var/log/site-apache_error.log
CustomLog /var/log/site-apache_access.log combined
</VirtualHost>
now, we need to add this VirtualHost to our apache configuration.
ln -s /etc/apache2/sites-available/site.com /etc/apache2/sites-enabled/
vim /etc/nginx/sites-available/apache-site.com
and add there:
server {
listen YOUR_IP:80;
server_name site.com;
rewrite ^(.*) http://www.site.com permanent;
}
server {
listen YOUR_IP:80;
server_name www.site.com;
access_log /var/log/site-nginx-access.log;
error_log /var/log/site-nginx-error.log;
location / {
proxy_pass http://127.0.0.1:80/;
include /etc/nginx/proxy.conf;
}
}
how should /etc/nginx/proxy.conf looks like – I desribed earlier.
now add it to nginx:
ln -s /etc/nginx/sites-available/apache-site.com /etc/nginx/sites-enabled/
/etc/init.d/apache2 restart /etc/init.d/nginx restart
Check ‘site.com’ in your web browser. You should see your php script served by apache.
The end.
Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.
October 27, 2009, 4:31 pm
I don’t get it. You are running both Apache and nginx on the same port (80)? I think it shouldn’t work.
October 28, 2009, 2:21 am
It works.
You just need to have:
NameVirtualHost 127.0.0.1:80
Listen 127.0.0.1:80
in ‘/etc/apache2/ports.conf’.
and ’127.0.0.1:80′ in your ‘VirtualHost’ like above.
after that in ‘/etc/nginx/sites-enabled/site.com’ you need to have ‘listen YOUR_IP:80′ instead of ‘listen 80′.
October 29, 2009, 5:44 am
Ah OK, I understand it now. So nginx is listening on eth0 device and Apache on loopback device.
October 29, 2009, 5:54 am
That’s right.
November 26, 2009, 4:19 am
[...] I moved from php on apache + nginx to php-fpm + nginx and I forgot about short form of PHP’s open tag. I did some reasearch [...]