Quick Guide to Nginx
Introduction
Like Lighttpd, Nginx
is a faster and leaner alternative to Apache. It can be used as either a regular
web server or a reverse
proxy. This is a short HOWTO on installing Nginx with PHP5 in FastCGI mode
on Ubuntu (10.04).
Setup
From package (Ubuntu)
Install Nginx and check that it works OK before moving on to adding PHP5
to the mix:
- apt-get install nginx
- /etc/init.d/nginx start
- netstat -lpn
- http://nginx-srv/
- /etc/init.d/nginx stop
- update-rc.d nginx defaults
- chown -R www-data.www-data /var/www?
To remove Nginx: apt-get --purge remove nginx ; rm -Rf /var/www/nginx-default/
Note that if you perform multiple install/remove cycles, you might need to
clean the database:
- dpkg --purge $(COLUMNS=200 dpkg -l | grep "^rc" | tr -s '
' | cut -d ' ' -f 2)
From source
- cd /usr/src
- wget -c http://sysoev.ru/nginx/nginx-0.7.67.tar.gz
- tar xzvf ./nginx-0.7.67.tar.gz
- cd nginx-0.7.67
- ./configure
- make
- make install
More information:
Adding PHP5
There are several ways to run PHP5 with Nginx:
We'll use PHP-FPM to run PHP5.
Through packages
- apt-get install php5-cli php5-common php5-suhosin php5-mysql
- apt-get install python-software-properties
- add-apt-repository ppa:brianmercer/php
- apt-get update
- apt-get install php5-fpm php5-cgi
- /etc/init.d/php5-fpm start
- "netstat -nat" and "ps aux", and check that a group of php5-fpm processes are running
as www-data
Tips:
- You can check which version of PHP5-FPM is available through /usr/bin/php5-fpm
-v
- Through /etc/php5/fpm/php5-fpm.conf, PHP5-FPM is configured to listen
to TCP9000 and run as www-data/www-data
- Check the logs in /var/log/php5-fpm.log
Configuring Nginx + PHP5
By default, the followings files in /etc/nginx/ don't need to be modified:
nginx.conf, fastcgi_params. The only file you need to edit for a standard install is /etc/nginx/sites-available/default.
Here's an example:
- server {
- listen 80 default;
- server_name localhost;
- access_log /var/log/nginx/localhost.access.log;
- root /var/www/nginx-default;
-
- location / {
- index index.php index.html;
- }
-
- location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$
{
- access_log off;
- expires 30d;
- }
-
- location ~ \.php$ {
- include fastcgi_params;
-
- fastcgi_split_path_info
^(.+\.php)(.*)$;
- fastcgi_pass backend;
- fastcgi_index index.php;
-
- fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
-
- fastcgi_intercept_errors
on;
- fastcgi_ignore_client_abort
off;
- fastcgi_connect_timeout
60;
- fastcgi_send_timeout
180;
- fastcgi_read_timeout
180;
- fastcgi_buffer_size
128k;
- fastcgi_buffers 4 256k;
- fastcgi_busy_buffers_size
256k;
- fastcgi_temp_file_write_size
256k;
- }
-
- ## Disable viewing .htaccess & .htpassword
- location ~ /\.ht {
- deny all;
- }
- }
-
- upstream backend {
- server 127.0.0.1:9000;
- }
Next, add the usual PHP script to check that it works:
- cd /var/www/nginx-default/; mv index.html index.html.orig
- vi index.php: <?php phpinfo(); ?>
Restart Nginx: /etc/init.d/nginx restart
Aim your browser to the server and check that it works as expected.
Check for errors in /var/log/nginx/error.log.
php.ini
If you make any change to php.ini, remember to /etc/init.d/php5-fpm restart
Does FPM use both INI files below?
# ll /etc/php5/fpm/
conf.d -> ../conf.d/
php5-fpm.conf
php.ini
Should I include this file to use PDO in PHP5 scripts?
# ll /etc/php5/conf.d/
pdo.ini
suhosin.ini
Q&A
Why does lack of trailing slash redirects to localhost?
http://192.168.0.3/punbb -> http://localhost/punbb/
http://192.168.0.3/punbb/ OK
How to change the port on which Nginx is listening?
Updated /etc/nginx/sites-available/default, restarted both PHP5-FPM and Nginx:
redirected to 80 with 301 error
Resources
To read
Third party modules
NginxFullExample
Wordpress
DONE
http://till.klampaeckel.de/blog/archives/30-PHP-performance-III-Running-nginx.html
http://www.sitepoint.com/blogs/2010/05/04/php-with-nginx-is-about-to-become-a-lot-easier/
http://blog.hbis.fr/2009/03/18/nginx-fastcgi-php/