PHP

PHP

A discussion among other things directly and indirectly relating to the PHP scripting language

Serving PHP5 with Nginx on Ubuntu 7.10 (Gutsy)

There are lots of tutorials out there to setup PHP with Nginx. I'm probably just retarded, but none of them got me to a working system. A detail left out of one, a configuration file left out of another, etc.

What follows is exactly what I did to get Nginx to serve PHP5 on a fresh Slicehost VPS slice running Ubuntu 7.10. Hopefully it will help someone else.

NOTE: The following commands assume that you have already setup a user account with sudo privileges and are executing the commands as that user, NOT as root.

Update your system and install basic tools required
> sudo aptitude update -y
> sudo locale-gen en_GB.UTF-8
> sudo /usr/sbin/update-locale LANG=en_GB.UTF-8
> sudo aptitude safe-upgrade -y
> sudo aptitude full-upgrade -y
> sudo aptitude install build-essential -y
Install MySQL
> sudo aptitude install mysql-server mysql-client libmysqlclient15-dev -y
Install PHP5
> sudo aptitude install php5-cli php5-cgi php5-mysql php5-xcache -y
Note: Xcache is installed at this point and available for you to setup, but by default is not turned on.

Install Nginx
> sudo aptitude install nginx -y
Go to your IP address and you should now recieve the message "Welcome to nginx!"

FastCGI Parameter Configuration

We will place all of our fastcgi parameters in a single file which we can include in as necessary.
> sudo vim /etc/nginx/fastcgi_params
This will be a new empty file, add the following and save:
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
# fastcgi_param  REDIRECT_STATUS    200;
Nginx Configuration
> sudo vim /etc/nginx/sites-available/default
This is a pre-existing file. Find the part that looks similar to the following and edit it as so and save:
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;
}
We need to remember to restart Nginx so that it picks up our configuration changes.
> sudo /etc/init.d/nginx stop
> sudo /etc/init.d/nginx start
Spawn-fcgi

We still need a script to start our fast cgi processes. We will extract one from Lighttpd.
> mkdir ~/sources
> cd ~/sources
> wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2
> tar -xvjf lighttpd-1.4.18.tar.bz2 
> cd lighttpd-1.4.18
> ./configure
> sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi
Let's get automated!
> sudo touch /usr/bin/php-fastcgi
> sudo vim /usr/bin/php-fastcgi
This is a new empty file, add the following and save:
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
Next...
> sudo touch /etc/init.d/init-fastcgi
> sudo vim /etc/init.d/init-fastcgi
This is also a new empty file, add the following and save:
#!/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 some permissions to make this all work.
> sudo chmod 755 /usr/bin/php-fastcgi
> sudo chmod 755 /etc/init.d/init-fastcgi
Create a Test File
> sudo vim /var/www/nginx-default/index.php
Let's just print out the information page for our PHP installation.
<?php echo phpinfo(); ?>
Start It Up
> /etc/init.d/init-fastcgi start
Now go to your IP address/index.php and you should see the PHP info page displayed.  
Set To Startup Automatically Upon Reboot
> sudo update-rc.d init-fastcgi defaults
You might want to test and make sure that it actually starts up upon reboot...
> sudo reboot
Final Words
If you think I have done anything stupid in the way this is setup, you may very well be right! Please leave a comment with a suggestion on how to improve the setup.
You probably will want to look into configuring Nginx for VirtualHosts, which is not considered in this post.

Credits
Much of this information was taken from the excellent post: How To Install A Complete LEMP (Linux - EnginxX (Nginx HTTP SERVER) - Mysql - PHP) Server (Not LAMP...) On Ubuntu/Debian, which got me about 90% there.
Sponsors
Comments
Be the first to leave a comment!
Add a Comment:
Already a member? Log In
Sponsors
About the Author

7 Kudos
Top Geek Articles
Celebrities on the Phone
Cell phones are to celebrities like bats are to baseball: no one runs too far without them.
Why every guy should buy their girlfriend Wii Fit.
Gratuitous...
Hot Geeks -- The Sexiest Geeky Girls
These girls are gorgeous AND they'll play Warcraft with you. Doesn't get much better than that.
More From Zimbio
Copyright © 2009 - Zimbio, Inc. Some rights reserved.