This post is not about optimization: it only describe a sure and fast way to get all those 3 components talk to each other. This article will help you bootstrap a minimal setup, something that I wouldn’t recommend for a production server without serious tweaking (to get both high performances and security).
First, we’ll get all our packages from an up-to-date DotDeb repository . If this is not already done, add those repositories to aptitude:
1$ echo "deb https://packages.dotdeb.org squeeze all" > /etc/apt/sources.list.d/squeeze-dotdeb.list
2$ gpg --keyserver keys.gnupg.net --recv-key 89DF5277
3$ gpg -a --export 89DF5277 | apt-key add -
4$ aptitude update
Now we can install the whole stack:
1$ aptitude install nginx
2$ aptitude install php5-fpm php5-mysql php5-gd php5-curl
3$ aptitude install mysql-server
FYI, here is the list of versions I installed:
-
Nginx 1.0.2
-
PHP 5.3.6
-
MySQL 5.1.57
As a way to test that our setup is working, we’ll serve a simple PHP file:
1$ mkdir -p /var/www/example.com/
2$ cd /var/www/example.com/
3$ echo "
4<?php phpinfo(); ?>
5" > ./index.php
6$ chown -R www-data:www-data /var/www
Now let’s create a minimal Nginx configuration file for this site:
1$ touch /etc/nginx/sites-available/example.com
In this brand new file, put the following directives:
server {
server_name example.com;
include /etc/nginx/php.conf;
root /var/www/example.com/;
location / {
root /var/www/example.com/;
access_log on;
}
}
This will only work if you’ve updated your DNS with an
A
record having
example.com
redirecting to the IP address of your Nginx server.
Now it’s time to create the
/etc/nginx/php.conf
file referenced in the Nginx configuration above. This file is where I put the generic setup making the bridge between Nginx and PHP-FPM. Here is what it should contain:
index index.php index.html index.htm;
location ~ \.php$ {
# Zero-day exploit defense.
# https://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won't get hacked.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
# As explained in https://kbeezie.com/view/php-self-path-nginx/ some fastcgi_param are missing from fastcgi_params.
# Keep these parameters for compatibility with old PHP scripts using them.
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Some default config
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;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_pass 127.0.0.1:9000;
}
Finally you can activate the site configuration and restart the whole stack:
1$ ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
2$ /etc/init.d/mysql restart
3$ /etc/init.d/php5-fpm restart
4$ /etc/init.d/nginx restart
If everything’s OK on your DNS, pointing your browser to
https://example.com
will show you the famous page produced by
phpinfo()
:
Note that MySQL doesn’t need any special attention to make it work out of the box. But again, if you plan to use it in production, its configuration needs special care, as for Nginx and PHP.
9 archived comments
Comments are closed. These were posted on Disqus, and are archived here.
-
Hi, I've followed your directions in this post and the follow up post. I get a 404 error on the
index.phpfile, though. I created anindex.htmfile to test and that one works fine.I'm using Debian 6 - fresh install. I've restarted
php5-fpmandnginxmultiple times.Any hints as to what would cause this?
-
Nevermind I got it working by using the updated
sites-availablefile.Thanks for the posts; excellent information!
-
Of course now when I try to add additional sites I am getting "Access to this web page is currently unavailable." on the new sites, but nothing shows up in any logs. The site config files look identical... I don't get it!
-
And it's resolved. Now I feel bad for cluttering this page up with my comments. I had the wrong IP address in my A records (the numbers look nearly identical at a glance).
-
@Gavin: no problem. Your mistakes can help others ! :)
-
-
-
-
i have the same error 404 ... and the config looks identicall ... mhhh
-
Yah, had the same problem here. Make sure that the
rootdirective is in yourserverblock and NOT in yourlocationblock. Check the nginx pitfalls: http://wiki.nginx.org/Pitfalls.-
Hey Kevin , Thanks a lot for the guide , it works great but there's a small mistake the directives file for
example.cominsites-availablefolder should be like thisserver { server_name example.com; root /var/www/example.com/; include /etc/nginx/php.conf; location / { root /var/www/example.com/; access_log on; } }
ie. the
rootdirective should also be inserverpart else it shows 404 , rest everything is working superb , please update it in your post to prevent 404's for future readers of your post
-
-