Diegiame PHP 5.2.x – 5.5.x versijas

Taigi, nespėjame apšilti kojų su PHP 5.4 versija, o jau php.net galima pamatyti 5.5.0 alpha versijas. Žinoma visada knieti sužinoti, o kaip gi mūsų rašomos programos veiks su naujausia PHP versija. Tiesiog viską griauti ir keliauti prie naujos versijos gali ne kiekvienas. Turime senų projektų kurie netgi neveikia su PHP 5.3, arba gali veikti, tik jie apleisti ir laiko skirti atnaujinimams nebėra tikslo. Vienintelė išeitis – paleisti kelias versijas viename serveryje. Žinoma visas čia paminėtas versijas diegtis production serveryje – būtų tikrai neprotinga, bet va development serveryje jas turėti tikrai verta. Galų gale sudaromos sąlygos su tuo serveriu dirbantiems programuotojams išsibandyti naujas funkcijas ir patobulinimus. Iš karto perspėsiu, kad mano siūlomas variantas naudoja NGinx web serverį ir PHP-FPM (išskyrus 5.2.x), tad kaip tai padaryti su daugelio naudojamu Apache web serveriu – net neįsivaizduoju, žinantys kaip NGinx šioje situacijoje pakeisti į Apache – laukiami komentaruose. Be jokios abejonės – norint turėti kelias skirtingas PHP versijas viename serveryje – teks kompiliuoti rankomis. Jei to nesate anksčiau darę – bus puiki proga žengti žingsnį pirmyn.

Imame švarią ubuntu 12.04.1 LTS 64bit ubuntu server versiją, virtualiai mašinai priskiriame kiek įmanoma daugiau CPU resursų, kad greičiau vyktų kompiliavimas ir kimbame į darbą. Pirmiausia reikia įdiegi reikiamus paketus:

apt-get install gcc libxml2-dev libcurl4-openssl-dev libbz2-dev libltdl-dev libpng-dev libjpeg-dev libmcrypt-dev libmhash-dev mcrypt libmysqlclient-dev libpspell-dev libtidy-dev libxslt-dev libpcre3-dev autoconf nano libfreetype6-dev

PHP 5.2.x

Viską ko reikia jau turime tad imamės PHP, pradedame nuo žemiausios versijos, tai yra PHP 5.2.17, kaip žinia palaikymas 5.2.x serijai yra nutrauktas ir didesnės versijos tikriausia šioje serijoje nebebus. Taip pat ši versija neturi FPM palaikymo, tad reikia pasinaudoti spawn-fcgi paketu. Jį įsidiegiame atskirai (sąmoningai nerašiau šio paketo prie aukščiau pateiktos eilutės, kad neįdiegtumėte ko nereikia, jei visgi nusprendėte šią versiją praleisti, kad beje yra labai rekomenduojama :)). Šią versiją asmeniškai diegiau vien norėdamas pasitikrinti ar pavyks ir teko sugaišti daug laiko, kol pavyko paruošti šias kelias eilutes:

apt-get install spawn-fcgi;
wget http://museum.php.net/php5/php-5.2.17.tar.gz;
tar -zxvf php-5.2.17.tar.gz;
cd php-5.2.17;
./configure --prefix=/opt/php-5.2.17 --with-config-file-path=/etc/php52/cli --with-config-file-scan-dir=/etc/php52/php.d --enable-fastcgi --enable-force-cgi-redirect --with-libdir=lib/x86_64-linux-gnu --with-mysql --with-mysqli --enable-mbstring --disable-debug --with-gd --disable-rpath --with-bz2 --with-curl --with-gettext --with-iconv --with-mcrypt --with-pcre-regex --with-zlib --with-pdo-mysql --with-pdo-sqlite --enable-ftp --enable-soap --enable-calendar --enable-bcmath --enable-ftp --enable-sockets --enable-wddx --enable-zip --with-curl --with-mhash --with-xsl --with-pear --with-pspell --with-tidy;
make -j 6;
make install;
mkdir -p /etc/php52/cgi;
mkdir -p /etc/php52/cli;
cp php.ini-recommended /etc/php52/cgi/php.ini;
cp php.ini-recommended /etc/php52/cli/php.ini;
ln -s /opt/php-5.2.17 /opt/php52;

Sukuriame failą /etc/init.d/php-fpm-52 su tokiu turiniu:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          spawn-fcgi-php
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop php-cgi using spawn-fcgi
# Description:       Start/stop php-cgi using spawn-fcgi
### END INIT INFO
 
# Author: Richard Laskey <me@rlaskey.org>
# for more information, please visit http://rlaskey.org/
 
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="php-cgi via spawn-fcgi"
NAME=php-fpm-52
PIDFILE=/var/run/$NAME.pid
DAEMON=/usr/bin/spawn-fcgi
 
# Specify the requests limit for a php instance (prevent memory leaks)
PHP_FCGI_MAX_REQUESTS=500
export PHP_FCGI_MAX_REQUESTS;
 
# you can change the arguments for spawn-fcgi here:
    # -C 3 implies three children processes
    # -a 127.0.0.1 binds to the loopback device
    # -p XYZ sets the the listening port to XYZ
    # -u and -g set the user/group the process runs as
    # -f is deprecated; -- <command> is the preferred syntax
DAEMON_ARGS="-C 2 -a 127.0.0.1 -p 9000 -u www-data -g www-data -P $PIDFILE -- /opt/php52/bin/php-cgi $PHP_ARGS"
SCRIPTNAME=/etc/init.d/$NAME
 
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
 
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
 
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
 
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
 
#
# Function that starts the daemon/service
#
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    # --exec has been replaced by --startas
    start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
    # Add code here, if necessary, that waits for the process to be ready
    # to handle requests from services started subsequently which depend
    # on this one.  As a last resort, sleep for some time.
}
 
#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    # removed --name $NAME since the running process is not spawn-fcgi
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    # commenting out this line; probably won't find processes via --exec
    # start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    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
    ;;
  status)
       # added -p $PIDFILE, can't find process otherwise
       status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  restart|force-reload)
    # removed  "$NAME", was repetitive
    log_daemon_msg "Restarting $DESC"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
            0) log_end_msg 0 ;;
            1) log_end_msg 1 ;; # Old process is still running
            *) log_end_msg 1 ;; # Failed to start
        esac
        ;;
      *)
        # Failed to stop
        log_end_msg 1
        ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
    ;;
esac
 
:

Ir finalinis:

chmod +x /etc/init.d/php-fpm-52;
/etc/init.d/php-fpm-52 start;
update-rc.d php-fpm-52 defaults; # jei norite paleisti automatiškai po serverio restart

Aišku PHP 5.2.17 nepalaiko fpm, bet taip paleidimo skriptą aš pavadinau dėl bendros tvarkos. Taigi iš init.d skripto matome, kad šią PHP versiją paleidžiame ant 9000 porto. To mums prireiks pabaigoje konfigūruojant NGinx web serverį.

PHP 5.3.x

Ši versija turi jau PHP-FPM palaikymą, todėl bus kiek „švariau“.

wget http://lt.php.net/get/php-5.3.21.tar.gz/from/this/mirror;
tar -zxvf mirror;
cd php-5.3.21;
./configure --prefix=/opt/php-5.3.21 --with-config-file-path=/etc/php53/cli --with-config-file-scan-dir=/etc/php53/php.d --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=lib64 --with-mysql=mysqlnd --with-mysqli --enable-mbstring --disable-debug --disable-rpath --with-bz2 --with-curl --with-gettext --with-iconv --with-openssl --with-gd --with-mcrypt --with-pcre-regex --with-zlib --with-pdo-mysql --with-pdo-sqlite --enable-ftp --enable-soap --enable-calendar --enable-bcmath --enable-ftp --enable-gd-native-ttf --enable-sockets --enable-wddx --enable-zip --with-curl --with-mhash --with-xsl --with-pear --with-pspell --with-tidy --enable-mysqlnd --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-png-dir=/usr/lib/x86_64-linux-gnu --with-freetype-dir=/usr/lib/x86_64-linux-gnu;
make -j 6;
make install;
ln -s /opt/php-5.3.21 /opt/php53;
mkdir -p /etc/php53/fpm;
mkdir -p /etc/php53/cli;
cp php.ini-production /etc/php53/fpm/php.ini;
cp php.ini-production /etc/php53/cli/php.ini;

/etc/init.d/php-fpm-53

 
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/opt/php53
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=/etc/php53/fpm/fpm.conf
php_fpm_PID=/var/run/php-fpm-53.pid
 
php_opts="--fpm-config $php_fpm_CONF -c /etc/php53/fpm/php.ini"
 
wait_for_pid () {
    try=0
    while test $try -lt 35 ; do
        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;
            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac
        echo -n .
        try=`expr $try + 1`
        sleep 1
    done
}
case "$1" in
    start)
        echo -n "Starting php-fpm "
        $php_fpm_BIN $php_opts
        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi
        wait_for_pid created $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    stop)
        echo -n "Gracefully shutting down php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -QUIT `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
    force-quit)
        echo -n "Terminating php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -TERM `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    reload)
        echo -n "Reload service php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -USR2 `cat $php_fpm_PID`
        echo " done"
    ;;
    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload}"
        exit 1
    ;;
esac

/etc/php53/fpm/fpm.conf

[global]
pid = /var/run/php-fpm-53.pid
error_log = /var/log/php-fpm-53.log
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9001
pm = static
pm.max_children = 3
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

Ir finalinis

chmod +x /etc/init.d/php-fpm-53;
/etc/init.d/php-fpm-53 start;
update-rc.d php-fpm-53 defaults;

Šiai versijai duodame portą 9001

PHP 5.4.x

Šios versijos diegimas beveik niekuo nesiskiria nuo 5.3.x versijos

wget http://lt.php.net/get/php-5.4.11.tar.gz/from/this/mirror;
tar -zxvf mirror;
cd php-5.4.11;
./configure --prefix=/opt/php-5.4.11 --with-config-file-path=/etc/php54/cli --with-config-file-scan-dir=/etc/php54/php.d --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=lib64 --with-mysql --with-mysqli --enable-mbstring --disable-debug --disable-rpath --with-bz2 --with-curl --with-gettext --with-iconv --with-openssl --with-gd --with-mcrypt --with-pcre-regex --with-zlib --with-pdo-mysql --with-pdo-sqlite --enable-ftp --enable-soap --enable-calendar --enable-bcmath --enable-ftp --enable-gd-native-ttf --enable-sockets --enable-wddx --enable-zip --with-curl --with-mhash --with-xsl --with-pear --with-pspell --with-tidy --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-png-dir=/usr/lib/x86_64-linux-gnu --with-freetype-dir=/usr/lib/x86_64-linux-gnu;
make -j 6;
make install;
ln -s /opt/php-5.4.11 /opt/php54;
mkdir -p /etc/php54/fpm;
mkdir -p /etc/php54/cli;
cp php.ini-production /etc/php54/fpm/php.ini;
cp php.ini-production /etc/php54/cli/php.ini;

/etc/init.d/php-fpm-54

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/opt/php54
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=/etc/php54/fpm/fpm.conf
php_fpm_PID=/var/run/php-fpm-54.pid
 
php_opts="--fpm-config $php_fpm_CONF -c /etc/php54/fpm/php.ini"
 
wait_for_pid () {
    try=0
    while test $try -lt 35 ; do
        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;
            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac
        echo -n .
        try=`expr $try + 1`
        sleep 1
    done
}
case "$1" in
    start)
        echo -n "Starting php-fpm "
        $php_fpm_BIN $php_opts
        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi
        wait_for_pid created $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    stop)
        echo -n "Gracefully shutting down php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -QUIT `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
    force-quit)
        echo -n "Terminating php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -TERM `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    reload)
        echo -n "Reload service php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -USR2 `cat $php_fpm_PID`
        echo " done"
    ;;
    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload}"
        exit 1
    ;;
esac

/etc/php54/fpm/fpm.conf

[global]
pid = /var/run/php-fpm-54.pid
error_log = /var/log/php-fpm-54.log
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9003
pm = static
pm.max_children = 3
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

Ir finalinis

chmod +x /etc/init.d/php-fpm-54;
/etc/init.d/php-fpm-54 start;
update-rc.d php-fpm-54 defaults;

PHP 5.5.x

Žinoma šiuo metu turime tik alpha4 versiją, bet o kodėl gi neįdiegus, jei viską tai viską :) Visi naudojami ./configure parametrai tokie patys kaip 5.4 versijos, bet pats asmeniškai dar nesu dorai išbandęs tai negaliu tiksliai pasakyti ar viskas veikia kaip ir turi.

wget http://downloads.php.net/dsp/php-5.5.0alpha4.tar.gz;
tar -zxvf php-5.5.0alpha4.tar.gz;
cd php-5.5.0alpha4;
./configure --prefix=/opt/php-5.5.0a4 --with-config-file-path=/etc/php55/cli --with-config-file-scan-dir=/etc/php54/php.d --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=lib64 --with-mysql --with-mysqli --enable-mbstring --disable-debug --disable-rpath --with-bz2 --with-curl --with-gettext --with-iconv --with-openssl --with-gd --with-mcrypt --with-pcre-regex --with-zlib --with-pdo-mysql --with-pdo-sqlite --enable-ftp --enable-soap --enable-calendar --enable-bcmath --enable-ftp --enable-gd-native-ttf --enable-sockets --enable-wddx --enable-zip --with-curl --with-mhash --with-xsl --with-pear --with-pspell --with-tidy --with-jpeg-dir=/usr/lib/x86_64-linux-gnu --with-png-dir=/usr/lib/x86_64-linux-gnu --with-freetype-dir=/usr/lib/x86_64-linux-gnu;
make -j 6;
make install;
ln -s /opt/php-5.5.0a4 /opt/php55;
mkdir -p /etc/php55/fpm;
mkdir -p /etc/php55/cli;
cp php.ini-production /etc/php55/fpm/php.ini;
cp php.ini-production /etc/php55/cli/php.ini;

/etc/init.d/php-fpm-55

#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/opt/php55
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=/etc/php55/fpm/fpm.conf
php_fpm_PID=/var/run/php-fpm-55.pid
 
php_opts="--fpm-config $php_fpm_CONF -c /etc/php55/fpm/php.ini"
 
wait_for_pid () {
    try=0
    while test $try -lt 35 ; do
        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;
            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac
        echo -n .
        try=`expr $try + 1`
        sleep 1
    done
}
case "$1" in
    start)
        echo -n "Starting php-fpm "
        $php_fpm_BIN $php_opts
        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi
        wait_for_pid created $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    stop)
        echo -n "Gracefully shutting down php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -QUIT `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
    force-quit)
        echo -n "Terminating php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -TERM `cat $php_fpm_PID`
        wait_for_pid removed $php_fpm_PID
        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
    restart)
        $0 stop
        $0 start
    ;;
    reload)
        echo -n "Reload service php-fpm "
        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi
        kill -USR2 `cat $php_fpm_PID`
        echo " done"
    ;;
    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload}"
        exit 1
    ;;
esac

/etc/php55/fpm/fpm.conf

 
[global]
pid = /var/run/php-fpm-55.pid
error_log = /var/log/php-fpm-55.log
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9003
pm = static
pm.max_children = 3
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

Ir finalinis

chmod +x /etc/init.d/php-fpm-55;
/etc/init.d/php-fpm-55 start;
update-rc.d php-fpm-55 defaults;

Nginx

wget http://nginx.org/download/nginx-1.2.6.tar.gz;
tar -zxvf nginx-1.2.6.tar.gz;
cd nginx-1.2.6;
./configure --prefix=/opt/nginx-1.2.6 --user=www-data --group=www-data --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid;
make -j 6;
make install;
ln -s /opt/nginx-1.2.6 /opt/nginx;

/etc/init.d/nginx

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO
PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
    . /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
        return 0
    else
        $DAEMON -t $DAEMON_OPTS
        return $?
    fi
}
case "$1" in
    start)
        echo -n "Starting $DESC: "
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;
    restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON || true
        sleep 1
        test_nginx_config
        # Check if the ULIMIT is set in /etc/default/nginx
        if [ -n "$ULIMIT" ]; then
            # Set the ulimits
            ulimit $ULIMIT
        fi
        start-stop-daemon --start --quiet --pidfile \
            /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
        ;;
    reload)
        echo -n "Reloading $DESC configuration: "
        test_nginx_config
        start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
            --exec $DAEMON || true
        echo "$NAME."
        ;;
    configtest|testconfig)
        echo -n "Testing $DESC configuration: "
        if test_nginx_config; then
            echo "$NAME."
        else
            exit $?
        fi
        ;;
    status)
        status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
        exit 1
        ;;
esac
exit 0

Dar truputis:

 
chmod +x /etc/init.d/nginx;
update-rc.d nginx defaults;
mkdir /etc/nginx;
mkdir /etc/nginx/sites-available;
mkdir /etc/nginx/sites-enabled;
ln -s /opt/nginx/conf/nginx.conf /etc/nginx/nginx.conf;
ln -s /opt/nginx/conf/fastcgi_params /etc/nginx/fastcgi_params;

/etc/nginx/nginx.conf

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;
events {
        worker_connections 768;
}
http {
        index index.php index.htm index.html;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_names_hash_max_size 1024;
 
        include /opt/nginx/conf/mime.types;
        default_type application/octet-stream;
        log_format global '$request_time | '
                '"$host $request" $status '
                ' $remote_addr ';
        access_log /var/log/nginx/access.log global;
        error_log /var/log/nginx/error.log;
        proxy_buffer_size   128k;
        proxy_buffers   4 256k;
        proxy_busy_buffers_size   256k;
        gzip on;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 1;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        include /etc/nginx/sites-enabled/*;
 
        upstream php52 {
            server 127.0.0.1:9000;
        }
        upstream php53 {
            server 127.0.0.1:9001;
        }
        upstream php54 {
            server 127.0.0.1:9002;
        }
        upstream php55 {
            server 127.0.0.1:9003;
        }
}

/etc/nginx/fastcgi_params

 
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  HTTPS              $https if_not_empty;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
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;
fastcgi_intercept_errors        on;
fastcgi_ignore_client_abort     off;
fastcgi_connect_timeout 180;
fastcgi_send_timeout 360;
fastcgi_read_timeout 360;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

Testavimas

Taigi turime keturias PHP versijas besiklausančias skirtingų portų

  • 9000 – PHP 5.2
  • 9001 – PHP 5.3
  • 9002 – PHP 5.4
  • 9003 – PHP 5.5

Nginx configūracijoje nurodėme šiuos keturis upstream’us, tad nieko nelaukę susikuriame paprastą PHP failą:
/var/www/test.php

<?php
phpinfo();

Tuomet nurodome NGinx, kaip ir ką krauti faile /etc/nginx/sites-available/default

server {
    listen 80;
    server_name _; # domenas arba IP, cia kaip norite
    root /var/www;
 
    location /php52 {
        fastcgi_pass php52;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root/test.php;
        break;
    }
 
    location /php53 {
        fastcgi_pass php53;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root/test.php;
        break;
    }
 
    location /php54 {
        fastcgi_pass php54;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root/test.php;
        break;
    }
 
    location /php55 {
        fastcgi_pass php55;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root/test.php;
        break;
    }
}

Ir finalinis

ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default;
/etc/init.d/nginx restart;

Dar sukuriame /var/www/index.html failą demonstracijai

<frameset cols="50%,50%" rows="50%,50%">
  <frame src="/php52">
  <frame src="/php53">
  <frame src="/php54">
  <frame src="/php55">
</frameset>

Ir galite drąsiai tai rodyti draugams :)
php

Jei susiduriate su mysql problemomis, tuomet reikia pasitvarkyti reikiamus php.ini failus:

mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock

Taip pat nepamirškite įsidiegti akseleratorių, kaip APC ar pan. Tik man PHP 5.4.x su APC kartais atsisakydavo veikti naudojant tam tikrus skriptus, tad teko APC pakeisti eAccelerator kuris iki šiol problemų nekelia. Taip pat galite kol kas net nebandyti diegti akseleratorių į PHP 5.5.x, reiks dar palaukti…

Geros darbo savaitės pradžios!

Žygimantas

Lead developer at Soundest. Zend Certified Engineer.

More Posts - Website

Žymos: ,

Parašykite komentarą

El. pašto adresas nebus skelbiamas. Būtini laukeliai pažymėti *

Galite naudoti šias HTML žymas ir atributus: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>