MySQL, nginx and PHP on Ubuntu 8.04
The nginx webserver is a lightweight alternative to apache and is perfect for running on a vps where resources are scarce. First we need to install our packages.
sudo apt-get install mysql-server mysql-client libmysqlclient15-dev
I am using apt-get instead of aptitude here so we don’t get a lot of unwanted dependencies installed. The MySQL install will prompt you for a password for the root user. Remember this is not the same as the root login for your server and is just for master access to the MySQL databases.
sudo aptitude install php5-cli php5-cgi php5-mysql php5-xcache sudo aptitude install nginx
Configure fastcgi
To enable php with nginx we need to edit the nginx configuration file to use fastcgi.
sudo nano /etc/nginx/sites-available/default
On my install the necessary lines were already present and just needed to be uncommented.
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
While you are editing the config file you should change the server name to match your domain. The file fastcgi_params was installed as part of nginx.
Spawn-fcgi
Next we need a way to spawn our fastcgi processes as needed. The webserver Lighttpd comes with a script to do that which we can use. Download the source and build it to get the binary that we need.
mkdir ~/sources cd ~/sources wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.bz2 tar jxvf lighttpd-1.4.19.tar.bz2
I found that the configure script complained about some missing dependencies so install those first.
sudo aptitude install libpcre3-dev libbz2-dev
You can probably make it without them but having development headers is useful if you plan to do much building from source. Change to the directory you just unpacked and build the binary with configure and then make.
lighttpd-1.4.19 ./configure make
All we need is the spawn-fcgi binary.
sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi
Now we need a simple script to run it.
sudo nano /usr/bin/php-fastcgi
Copy the following into that file.
#!/bin/sh /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
We can also use another script to control the startup.
sudo nano /etc/init.d/init-fastcgi
Copy the following into that file.
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php
RETVAL=$?
;;
restart)
killall -9 php
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
We need to change the permissions on these two scripts so they are executable.
sudo chmod 755 /usr/bin/php-fastcgi sudo chmod 755 /etc/init.d/init-fastcgi
Startup our scripts
Now we can start our fastcgi script and add it to our init script so it starts automatically on boot.
sudo /etc/init.d/init-fastcgi start sudo update-rc.d init-fastcgi defaults
Don’t forget to start the webserver.
sudo /etc/init.d/nginx start
If you want to test php is working, just create a simple php file.
sudo nano /var/www/nginx-default/test.php
Just one line should do it.
<?php echo phpinfo(); ?>
Open this page in your browser and check that php is working as it should.
Now that we have a working ‘LEMP’ stack we can put up some content. My next post will describe how I installed Wordpress.
References:
http://jit.nuance9.com/2008/01/serving-php5-with-nginx-on-ubuntu-710.html
http://articles.slicehost.com/2007/9/10/ubuntu-lts-mysql-and-ror













8th June 2008 at 15:48
Hi,
I’m running into some trouble following your tutorial:
[code]A83 ~/sources: ./configure
-bash: ./configure: No such file or directory[/code]
I’ve followed the tutorial exactly up to that point. Any idea what I should do?
Thanks in advance!
8th June 2008 at 16:02
Ahh, I figured out that I had to enter the lighttpd directory, so now ./configure and make worked!
But I ran into another problem, when I try to start nginx I get this error:
A83 /: sudo /etc/init.d/nginx start
Starting nginx: 2008/06/08 14:01:27 [emerg] 22231#0: unknown directive “includefastcgi_params” in /etc/nginx/sites-enabled/default:40
8th June 2008 at 16:07
Ok, this is starting to become silly. Saw that my nginx config file and yours didn’t looked exactly the same. Copied yours and made nginx start!
But it seams like there are more to this:
When I goto my IP the welcome to nginx text is showing. But if I turn my browser to test.php I get this text:
The page you are looking for is temporarily unavailable.
Please try again later.
8th June 2008 at 16:09
Yes you need to change to the correct directory - I’ve edited the tutorial to make that clearer.
Line 40 in my default file reads:
include /etc/nginx/fastcgi_params;
8th June 2008 at 16:24
Ok this could be several things -
Check first that php cgi process is running correctly.
ps aux
You should see several processes /usr/bin/php5-cgi under COMMAND.
If not then check again that these scripts are correct:
/usr/bin/php-fastcgi
/etc/init.d/init-fastcgi
If you do have the php5-cgi processes then check the nginx config files again to make sure that it is routing requests for .php files correctly.
Finally check again test.php has correct syntax and is in the correct directory - also that the user permissions are correct if you aren’t running nginx as root.
I’m not sure right now what else could be the problem.
8th June 2008 at 23:09
Thanks for the assistance!
I ran ps aux but didn’t found any processes so I redid that part of the tutorial so now they’re there.
I’ve set up a vhost to my domain (following this tutorial: http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts). And my index.html works fine, but if I try to run the test.php I’ve uploaded in the same directory I get dialog to download the file instead of opening it. Can I state that somewhere in the nginx config what file extentions it shall run?
The file is here: http://a83.se/test.php
Thanks a lot in advance!
8th June 2008 at 23:23
I would check again this part of the nginx config for your domain that begins:
location ~ \.php$ {
This is what matches urls with .php extension and passes them to fastcgi - in your case this is not happening for some reason.
2nd July 2008 at 13:23
Hi,
I followed yours fantastic guide but when I call test.php file the reponse is:
No input file specified.
Why ? Can you help me ?
Test yourself
http://209.20.69.80/test.php
2nd July 2008 at 16:45
This error means php is not getting the file for some reason.
Check the SCRIPT_FILENAME param in your nginx configuration file has the correct path for your install:
/var/www/nginx-default$fastcgi_script_name;
Failing that it could be a permissions problem - the spawn-fcgi process runs as user ‘www-data’ in my example, so check that the files are readable.
8th July 2008 at 16:45
Hi,
I tried and tried, notyhing:
1) I set permission on spawn-fcgi by “sudo chmod 755 /usr/bin/php-fastcgi”
2) I found that post http://forum.slicehost.com/comments.php?DiscussionID=1259 that has solved by add “-g www-data” into /usr/bin/php-fastcgi … from “/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi” to “/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi”
3) I reboot slice …
Nothing
I think I have to change web server ….
30th July 2008 at 21:38
[...] Aber Abhilfe wurde da schnell gefunden: [...]
21st September 2008 at 00:51
When i want to start fastcgi by using “sudo /etc/init.d/init-fastcgi start” then i get “/usr/bin/php-fastcgi: 2: /usr/bin/spawn-fcgi: Permission denied” what is wrong , pls Hlp.
28th September 2008 at 22:55
Hi Herman, sorry I don’t know what could be causing that, maybe your web server is running a different user to spawn-fcgi?
4th January 2009 at 05:48
[...] Sumber : MySQL, nginx and PHP on Ubuntu 8.04 [...]
1st April 2009 at 12:52
There ist a bug on your init script:
wrong two lines with:
killall -9 php
right:
killall -9 /usr/bin/php5-cgi
14th June 2009 at 19:12
[...] Nginx is excellent at serving static files and also supports reverse proxying. Like Lighttpd, I was able to get Nginx up and running in a few minutes. However, it does not come up with default FastCGI support - so getting PHP/MySQL working was a little tricky. It involves spawning a FastCGI process manually. I found this tutorial helpful. [...]
30th June 2009 at 01:08
How to install Nginx with PHP as FastCGI you can find here
http://www.linuxspace.org/archives/1576 on the Ubuntu 9.04
I used it for my blog
4th July 2009 at 08:26
[...] PHP/MySQL working was a little tricky. It involves spawning a FastCGI process manually. I found this tutorial helpful. LiteSpeed LiteSpeed, Web Server is the leading high-performance, high-scalability web server. It [...]
18th August 2009 at 07:16
You can follow this one exactly the same - no need for any changes someone mentioned above.
One thing: keep in mind if you are working with Ubuntu Server with command line by default, you should install the followings:
sudo apt-get install make
sudo apt-get install build-essential
nano is not a good editor IMO. Use VIM if possible.
sudo apt-get install vim-full
Press I key to insert, press Esc to exit to normal mode, then :wq to save and quit or :w just to save, or :q just to quit
search online :)
21st October 2009 at 11:41
[...] Nginx is excellent at serving static files and also supports reverse proxying. Like Lighttpd, I was able to get Nginx up and running in a few minutes. However, it does not come up with default FastCGI support – so getting PHP/MySQL working was a little tricky. It involves spawning a FastCGI process manually. I found this tutorial helpful. [...]
12th December 2009 at 12:38
Nice article i’ll put on my blog too with your site reference.
11th March 2010 at 18:20
[...] References:http://jit.nuance9.com/2008/01/serving-php5-with-nginx-on-ubuntu-710.htmlhttp://articles.slicehost.com/2007/9/10/ubuntu-lts-mysql-and-ror http://bc-dev.net/2008/05/07/mysql-nginx-and-php-on-ubuntu-804/ [...]