2014-08-24 18:20 UTC
I've been using NGINX as my primary web server for many years now and I've really come to like it's simplicity and flexibility. This is a short writeup on howto get NGINX with PHP support running under Debian Wheezy.
At first, I recommend getting NGINX directly from nginx.org to make sure to have the latest version with all the eventual security patches and features applied. The NGINX team are kind enough to provide pre-built binaries for Debian so we do not have to build this from source and future updating will be simple ("apt-get upgrade").
NGINX does not contain a built-in PHP module and relies on an external FastCGI server. I'm going to install two Debian packages (and their dependencies) to the system, nginx and php5-fpm. FPM stands for FastCGI Process Manager and php5-fpm provides a PHP FastCGI server with a socket interface that NGINX can use to execute PHP scripts.
We first need to add the nginx.org Debian Wheezy repository and its corresponding key. Create the following APT source file.
/etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/debian/ wheezy nginx
deb-src http://nginx.org/packages/debian/ wheezy nginx
Then download and add the nginx.org key to you key storage.
wget http://nginx.org/keys/nginx_signing.key
apt-key add nginx_signing.key
After this we are ready to begin installation.
apt-get update
apt-get install nginx php5-fpm
Debian systems have a pre-defined user called "www-data" meant to be used for web servers and the like. The php5-fpm package is run under that user by default and I recommend changing NGINX to do the same to make it simple. Change the following parameter in nginx.conf.
/etc/nginx/nginx.conf
user www-data;
Next we need to NGINX to redirect all *.php files through the PHP-FPM server. A modified version of the NGINX default configuration file can be seen below.
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
location / {
index index.php index.html index.htm;
}
# custom error page(s)
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The php5-fpm package is by default configured to listen on a unix socket, /var/run/php5-fpm.sock, and that socket is called by NGINX using the fastcgi configuration above. The location{} expression ensures that only *.php files are passed to the PHP-FPM server.
Restart nginx and php5-fpm to apply the changes.
/etc/init.d/php5-fpm restart
/etc/init.d/nginx restart
Optional: For PHP-FPM I recommend activating error logging to file.
/etc/php5/fpm/php.ini
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
Create the log file and set the correct owner.
touch /var/log/php_errors.log
chown www-data:www-data /var/log/php_errors.log
/etc/init.d/php5-fpm restart
That's it. You should now have a basic working NGINX+PHP installation.
Write a comment