Install PHP and NGINX on Ubuntu
posted by Steve on
Nginx and PHP make a high performance combination on Ubuntu; here's how to get the system up and running.
Apache is a great webserver, but it may pack along more than you need. If all you need is a simple webserver with php then use nginx.
I've used this method to install on ubuntu 8.10, 9.04, 9.10, 10.4 and 10.10
Here are the steps to get it on your system.
- Install nginx:
sudo apt-get install nginx
- Install spawn-fcgi:
sudo apt-get install spawn-fcgi
- Install php :
sudo apt-get install php5-cgi
- Create a startup script for php:
sudo vi /etc/init.d/php-fastcgi
- paste the following in that file
#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000
PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0
start() {
echo -n "Starting PHP FastCGI: "
start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
RETVAL=$?
echo "$PHP_CGI_NAME."
}
stop() {
echo -n "Stopping PHP FastCGI: "
killall -q -w -u $USER $PHP_CGI
RETVAL=$?
echo "$PHP_CGI_NAME."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
- Make the file executable:
sudo chmod 755 /etc/init.d/php-fastcgi
- Try it:
sudo /etc/init.d/php-fastcgi start
- Make the script run at boot:
sudo update-rc.d php-fastcgi defaults
- Add this in your nginx script (usually in sites-enabled):
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 fastcgi_params;
}
- Restart nginx:
sudo service nginx restart
Now create a php test file and test this:
vi phptest.html
put this in it:
<?php
echo "hello"
phpinfo();
?>
Browse over to phptest.html and see if you get the php info page. If so, you are successful.