nginx + PHP under Debian (+ W3TC)

nginx is a fast web server that can also be used as a reverse proxy or for streaming Flash videos. Originally developed for the requirements of a Russian search engine, more and more web projects are also relying on nginx. Unlike the well-known Apache web server, the configuration of nginx under Debian is not quite as simple - files still have to be adapted by hand. This article describes the basic changes needed to get PHP5 running under Debian with Xcache as FastCGI integration. The settings shown should always be adapted to your environment.

Installation
The following command will install the appropriate packages:

apt-get install php5-cgi php5-xcache php5-mysql nginx

In our case the mysql server is located on a different server, otherwise the corresponding package would have to be installed additionally. The next step is to set up the nginx, where we keep the Debian standard. With the command

/etc/init.d/nginx start

it is checked if the configuration works up to here and the web server starts correctly.

Configuration

If this is the case, some adjustments to the PHP settings for the respective host must still be made (the default host is configured in "/etc/nginx/sites-available/default").

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; #php runs on localhost, port 9000
#fastcgi_index index.php; # is not required for this configuration
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; # defines the directory
include /etc/nginx/fastcgi_params;
}

The fastcgi_params, which is created by Debian under /etc/nginx, also needs some changes. The following lines must be commented out:

#fastcgi_param SCRIPT_NAME $document_root$fastcgi_script_name;

Now php-cgi can be started alone to see if everything works:

php-cgi -b 127.0.0.1:9000

A php-info file in /var/www should now return information about installing PHP. If there is the message "no input file specified" there is still something wrong with the directories. If everything works, the php-cgi startup script can be downloaded from Nginx Wiki and settings on the XCache etc. should be made. Upload directories should also be protected as described in the Wiki, so that PHP cannot be executed there.

Soon we will continue here with redirects under Nginx and plain text URLs with WordPress (as this blog already uses them).

For WordPress, there is a really good plugin called W3TC, which provides/activates different mechanisms that speed up the delivery of WordPress significantly. This plugin also works with nginx, but the rewrite configuration has to be adjusted (W3TC provides information about this). "Minify" for CSS and JS has caused some graphics on this page to not load - troubleshooting is still going on, but until then there is still a significant speed advantage. Apache Bench shows corresponding values, which look quite good for a vServer:

Complete requests: 1500
Requests per second: 91.20 [#/sec] (mean)
Time per request: 10,965 [ms] (mean)
Transfer rate: 4084.89 [Kbytes/sec] received

I will see in the next few days whether it can be further optimized.

Leave a Comment

Your email address will not be published.

en_USEnglish
Scroll to Top