Nginx and PHP on Ubuntu Lucid
To do some PHP development (read experimenting) on my laptop I wanted to get a web-server with PHP running on it. Since this is a laptop with limited resources, I decided to try Nginx instead of Apache. Nginx is known to use less memory as compared to Apache. I am hoping this will keep the laptop still usable while the web-server is still running in the background.
This is not an exhaustive how-to, but this is how I installed all that is needed to get my first PHP page up.
Let me begin by mentioning that this laptop currently runs the development build of Ubuntu Lucid (which will become Ubuntu 10.04 LTS sometime in April 2010).
To begin, I installed following packages from Ubuntu Lucid repositories:
% sudo aptitude install nginx php5-cgi lighttpd
Although I am not using lighttpd, the package is required because it provides the spawn-fcgi script.
Nginx supports FastGI natively. PHP is served as a FastCGI process.
Modify /etc/php5/cgi/php.ini to include the following line:
cgi.fix_pathinfo=1
Setting this configuration makes sure that PHP sets path information of the executing script in a way that conforms with CGI specifications. The line may already exist in the file, but commented. Just un-comment it. This is an optional step (at least in Lucid version of PHP) since the default value of cgi.fix_pathinfo is 1.
To spawn FastCGI add the following line to /etc/rc.local every time the computer starts.
/usr/bin/spawn-fcgi \
-a 127.0.0.1 -p 9000 -u www-data -g www-data \
-f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
This script (spawn-fcgi) was installed via the lighttpd package.
Finally, I configured a website in Nginx and enabled PHP via FastCGI. This configuration would typically reside in /etc/nginx/sites-available/<website_name>:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
root /var/www/blog;
index index.php;
fastcgi_index index.php;
#
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
When all is set up properly, you can test it with the phpinfo() command. It should look something like the following:
All this, mostly a note to self. But hope it helps someone out there.


Recent Comments