1.Install PHP5
We will use dotdeb repo for installing the latest version of PHP5 and MySQL Server so first we need to configure apt to use dotdeb repo.
Edit /etc/apt/sources.list :
1 | nano /etc/apt/sources .list
|
Add The following lines to end of it :
1 2 | deb http: //packages .dotdeb.org stable all
deb-src http: //packages .dotdeb.org stable all
|
Press CTRL+X Choose Yes to save the file and Exit.
update apt cache :
Now install PHP5 :
1 | apt-get install php5-cgi php5-mysql
|
Now edit /etc/php5/cgi/php.ini :
1 | nano /etc/php5/cgi/php .ini
|
and change cgi.fix_pathinfo to 1 :
Save File and Exit.
2. Install MySQL Server
1 | apt-get install mysql-server
|
3. Install Lighttpd
We need to install lighttpd because Nginx does not come with a FastCGI package, and there isn’t a standalone package yet. So we are going to install Lighttpd, disable it, and use spawn-fcgi from the Lighttpd package.
1 2 3 | apt-get install lighttpd
/etc/init .d /lighttpd stop
update-rc.d -f lighttpd remove
|
Also remove lighttpd executable file :
4. Setup spawn-fcgi
Since we are going to use spawn-fcgi to handle PHP, we need to set it up to start when our server starts and make an init script so that we can control the processes. For starters, create the init script:
1 | nano /etc/init .d /php-fastcgi
|
and add the following code to it and save it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #! /bin/sh
PATH= /sbin : /usr/sbin : /bin : /usr/bin
DESC= "php-cgi in external FASTCGI mode"
NAME=php-fastcgi
DAEMON= /usr/bin/php-cgi
PIDFILE= /var/run/ $NAME.pid
SCRIPTNAME= /etc/init .d/$NAME
PHP_CONFIG_FILE= /etc/php5/cgi/php .ini
[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/ $NAME ] && . /etc/default/ $NAME
. /lib/init/vars .sh
. /lib/lsb/init-functions
if [ "$START" != "yes" -a "$1" != "stop" ]; then
log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"
exit 0
fi
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
DAEMON_ARGS= "-q -b $FCGI_HOST:$FCGI_PORT -c $PHP_CONFIG_FILE"
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE -- exec $DAEMON -- test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE -- exec $DAEMON \
--background -- make -pidfile --chuid $EXEC_AS_USER --startas $DAEMON -- \
$DAEMON_ARGS \
|| return 2
}
do_stop()
{
start-stop-daemon --stop --quiet --retry=TERM /30/KILL/5 --pidfile $PIDFILE > /dev/null
RETVAL= "$?"
[ "$RETVAL" = 2 ] && return 2
start-stop-daemon --stop --quiet --oknodo --retry=0 /30/KILL/5 -- exec $DAEMON
[ "$?" = 2 ] && return 2
rm -f $PIDFILE
return "$RETVAL"
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;;
*) log_end_msg 1 ;;
esac
;;
*)
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
|
There is no editing needed for the code above. Now we need to make it executable.
1 | chmod +x /etc/init .d /php-fastcgi
|
Now we will create the configuration file for spawn-fcgi:
1 | nano /etc/default/php-fastcgi
|
and add:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | START= yes
EXEC_AS_USER=www-data
FCGI_HOST=localhost
FCGI_PORT=9000
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
|
In this file, you can change the FCGI_PORT, PHP_FCGI_CHILDREN, and PHP_FCGI_MAX_REQUESTS. If you change the port, make sure to note what you change it to because we will need it for later. Now, we want to make sure that spawn-fcgi starts when the server starts:
1 2 | update-rc.d php-fastcgi defaults
/etc/init .d /php-fastcgi restart
|
5. Install Nginx
6. Configure Nginx and your default vhost
For help with configuring Nginx beyond just the default configuration, please refer to the Nginx Wiki. We are now going to configure the default vhost so that we can verify that PHP is working with Nginx.
1 | nano /etc/nginx/sites-available/default
|
In the default vhost, change the following lines to look like this:
1 2 3 4 5 6 7 | server_name _;
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;
}
|
What we did here was changed the server_name to accept all incoming requests, uncommented the location stanza and defined where Nginx needs to look for spawn-fcgi. If you changed the port above in the /etc/default/php-fastcgi file, then you need to change the port on the fastcgi_pass line as well. On the line fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;, you need to change /var/ww/nginx-default to match your web root. On the line include fastcgi_params; you need to add a space between the include and the fastcgi_params as there is a bug in the default configuration and that space was omitted. Once you save the default-vhost, you need to restart Nginx.
1 | /etc/init .d /nginx restart
|
Now we will create an info.php file in your web root:
1 | nano /var/www/nginx-default/info .php
|
Add the following code and save the file:
Now in your web browser, pull up the newly created file (http://your-ip/info.php). You should see information about the version of PHP that you are running.
Note: When I installed Nginx, it did not start automatically, however it was setup to start when the server booted. I have heard stories of users having issues with Nginx starting automatically when the server boots. If Nginx does not start when you boot your server, run the following command which will make it start on boot:
1 | update-rc.d nginx defaults
|
Enjoy!
Reference : ChrisJohnston.org