info home general windows unix linux debian mac os apache nginx iphone

Recent Articles

How to rename multiple files in Unix or Linux

Convert docx to doc in Mac OS X Snow Leopard or Leopard

HISTORY shell variable management in Unix/Linux

How to eject CD/DVD from your MacBook or iMac

Play Prince of Persia on Ubuntu Linux using DOSBox

Tags

Configuring Nginx to forward PHP requests to Apache mod_php

« Configuring Nginx | Nginx with Perl CGI »

We will install Apache 2.2 and run it on port 8080 so it can receive PHP requests from Nginx.

Install Apache and mod_php

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils
sudo aptitude install libapache2-mod-php5 php5 php5-common php5-curl php5-dev php5-gd \
php5-imagick php5-mcrypt php5-mhash php5-mysql php5-sqlite php5-xmlrpc php5-xsl

Edit /etc/apache2/apache2.conf and add this line at the very bottom:

ServerName imacster.com

Edit /etc/apache2/ports.conf and change the port number from 80 to 8080

Listen 8080

Restart Apache

sudo /etc/init.d/apache2 restart

Now, configure Nginx to forward requests from .php scripts to Apache.

Edit /etc/nginx/nginx.conf and add these lines:

include /etc/nginx/apachemod.conf;

Create a file /etc/nginx/apachemod.conf and add these in the file:

location ~ .*\.(php)$ {
    proxy_pass         http://127.0.0.1:8080;
    proxy_redirect     off;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    client_max_body_size       10m;
    client_body_buffer_size    128k;

    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;

    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}

Restart Nginx:

sudo /etc/init.d/nginx restart

See if Nginx serves PHP pages. Create a simple PHP script called info.php with these contents:

<?php
phpinfo();
?>

And navigate to http://imacster.com/info.php to see if it works.

Alternative to mod_php

  1. You can also run a FastCGI server and make Nginx serve PHP requests through FastCGI.
  2. Another alternative is making Nginx serve PHP pages by running PHP-FPM instead of spawn-fcgi. I have no tried it, but apparently PHP-FPM is more stable than spawn-fcgi.

« Configuring Nginx | Nginx with Perl CGI »

Last Update: July 2009