The Weblog of Ales Loncar
Archive for June, 2010
How to install Nginx with PHP5 (FastCGI) and MySQL support on Ubuntu 10.4 LTS
Jun 26th
I won’t give you a lecture about using Nginx for a perfect web server. But the fact is that Apache is mostly a memory hog, so we are sometimes forced to look for alternative ways to host our websites. I will show you a step to step guide to install all the necessary software to run any sort of PHP application on your local machine. Read on to see how that can be achieved…
First step is updating your Ubuntu Installation
sudo aptitude update sudo aptitude safe-upgrade
This will update the apt package database and install all the latest updates (if there are any). If you see that a new kernel gets installed as part of the updates, you should reboot the system afterwards.
Installing Nginx is the next step. This can be easily done by following the next step (assuming there is no other web server up and running). Everything will be installed by default.
sudo aptitude install nginx
Start nginx afterwards:
sudo /etc/init.d/nginx start
After typing your web server’s hostname into a browser1, you should see the Nginx welcome page.
To make Nginx start at boot time, run
sudo update-rc.d nginx defaults
Next, we need FastCGI deamon to run PHP5 in fcgi mode. First off, Nginx doesn’t have its own internal FastCGI process manager, so we must run the FastCGI service separately. Usually the preferred method was to use spawn-fcgi program provided by the lighttpd web server. That’s not necessary any more, because from version 1.6.0 ahead spawn-fcgi was separated from lighttpd project. To install spawn-fcgi, run the following command.
sudo aptitude install spawn-fcgi
For now, we’re going to leave everything as it is and we’ll move forward with installing MySQL and PHP5.
Let’s install MySQL server and client binaries. The process is quick and easy.
sudo aptitude install mysql-server mysql-client
You will be asked to provide a password for the MySQL root user. After we specify a MySQL root password, the installation will finish. We can try the MySQL client program via command line tool:
mysql -u root -p
Installing PHP5. Now it’s time to install PHP5 via FastCGI. Ubuntu provides a FastCGI-enabled PHP5 package. We’ll install only core packages and add others when needed.
sudo aptitude install php5 php5-cli php5-common php5-suhosin php5-cgi php-pear php5-mysql
We’ve installed everything for running PHP on our computer, so let’s move on to necessary configuration. First, let’s write a script which will spawn FastCGI PHP processes on a unix domain socket, so there is no need for tcp/ip on localhost. Let’s create file /usr/sbin/fastcgi-php
sudo touch /usr/sbin/fastcgi-php
#!/bin/sh
FCGIPHP_BIN_PATH="/usr/bin/php5-cgi"
FCGIPHP_USER="www-data"
FCGIPHP_GROUP="www-data"
FCGIPHP_CHILDREN="2"
FCGIPHP_MAX_REQUEST="1000"
FCGIPHP_SERVER_ADDR="127.0.0.1"
FCGIPHP_SOCKET="/tmp/php-fastcgi.sock"
PIDFILE="/var/run/php5-fcgi.pid"
SPAWN_FCGI="/usr/bin/spawn-fcgi"
FCGIPHP_ENV="SHELL PATH USER"
COMMAND_ARGS="$SPAWN_FCGI -C $FCGIPHP_CHILDREN -s $FCGIPHP_SOCKET -f $FCGIPHP_BIN_PATH -u $FCGIPHP_USER -g $FCGIPHP_GROUP -P $PIDFILE"
export FCGIPHP_MAX_REQUEST
export FCGIPHP_SERVER_ADDR
ALLOWED_ENV="$FCGIPHP_ENV FCGIPHP_MAX_REQUEST FCGIPHP_SERVER_ADDR"
E=""
for i in $ALLOWED_ENV; do
eval "x=\$$i"
E="$E $i=$x"
done
start(){
echo "STARTING PHP-FCGI"
if [ -f $PIDFILE ];then
echo "PHP-FCGI is running already.";
exit 1;
fi
echo $COMMAND_ARGS
env - $E $COMMAND_ARGS
}
stop(){
echo "STOPING PHP-FCGI"
if test -f $PIDFILE;then
killall `basename $FCGIPHP_BIN_PATH`
rm -f $PIDFILE
else
echo "PHP-FCGI is not running"
fi
}
#Main################
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
*)
echo "usage: $0 {start|stop|restart}"
;;
esac
exit 1
and give it execute permissions. After that let’s start the process.
sudo chmod a+x /usr/sbin/fastcgi-php sudo /usr/sbin/fastcgi-php start
Final step, Nginx configuration. To get the web server up and running properly, we need to edit the file /etc/nginx/nginx.conf. We’ll pass all PHP calls to an upstream server called php5 so edit your configuration file to look like this:
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
upstream php5 {
server unix:/tmp/php-fastcgi.sock;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Any further configuration files needed for our applications will be located in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. Let’s add new site which will link to our home public folder ~/Public and we will call it my.public.local. Now create my.public.local.conf in /etc/nginx/sites-available
server {
listen 80;
reset_timedout_connection on;
server_name my.public.local;
root /home/celavi/Public;
client_max_body_size 500m;
autoindex on;
location / {
index index.php;
}
# Handle Static Content Here
location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
access_log off;
gzip off;
expires 30d;
}
location ~* ^.+\.(css|js)$ {
access_log off;
expires 1d;
}
location ~* ^.+\.(pdf|gz|bz2|exe|rar|zip|7z)$ {
gzip off;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass php5;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
and create a symbolic link to /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-availible/my.public.local.conf /etc/nginx/sites-enabled/my.public.local.conf
We need to tell the operating system to map my.public.local to local IP address. Open file /etc/hosts and add this line:
#local maping 127.0.0.1 my.public.local
Let’s restart Nginx
sudo /etc/init.d/nginx restart
In order to test if the execution of PHP is working, create an index.php file in ~/Public folder with the following content:
<?php echo phpinfo();
Open browser and type http://my.public.local. If you did not see the phpinfo, there might be something wrong. In order to track down what went wrong, you can check the Nginx error log:
sudo tail /var/log/nginx/error.log
Remember, if you did change your php.ini you have to restart /usr/sbin/fastcgi-php. Restarting nginx isn’t’ necessary.
Well, that’s it. Now you can start developing your PHP apps.
Screenshots:
And remember, contributions earn you karma. ;)
Related articles by Zemanta
- Matthew Helmke: nginx, php-fastcgi and Ubuntu 10.04 (matthewhelmke.net)
- Nginx is extremely performant (simplechatter.com)
- NGINX + PHP-FPM + APC = Awesome (interfacelab.com)

- http://localhost [↩]
KjeSeRoka means “Where’s your arm?” or what?
Jun 22nd
I’ve received several emails and comments about KjeSeRoka and what the appropriate translation would be in English. I will try to explain it’s meaning for those whose first language is not Slovene.
The most appropriate translation1 would be: “Where’s The Party?”, or in this particular case “Where I can find upcoming rock concerts in Slovenia?”.
Rokat / rokaj
Word rokat/rokaj has common origin in South Slavic languages (Serbian, Croatian, Bosnian) and can have a lot of different meanings.
Let’s look at these examples:
- Reprezentacija Portugala totalno je srokala Sjevernu Koreju sa 7:0 = Portugal’s team has totally crushed North Korea with 7:0
- Ajmo rokat! = Let’s get the party started = Let’s Rock! ©Duke Nukem
- Glazbenici, definicija vaše muzike, šta je to što rokate? = Musicians, the definition of your music, what is it that you play?
- Oko njega su rokale granate a mitraljez koji je cijepao po polju držao ga je zakovanim za zemlju. = While the gradates were falling all around him, the machine gun was keeping him low to the ground.
- Frenkie feat. Baby Dooks – Bruce Lee Rap – … Frenkie će rokat i dalje jer ta ti mašina ne staje … = Frenkie will always rule, because this machine does not stop …

- Edo Maajka – Pare, pare … Edo i Dash, pare, pare, keš, bolje od nas rokat` nikad nemožeš … = Edo and Dash, money, money, cash, you can’t ever rock better than us …

I hope these examples where enough illustrated. :D
And remember, contributions earn you karma. ;)

- more interpretation [↩]























