Configuring Nginx to forward PHP requests to Apache mod_php

Published on July 03, 2009

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:


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

Alternative to mod_php

  • You can also run a FastCGI server and make Nginx serve PHP requests through FastCGI.
  • 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

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on July 03, 2009