Przedstawię dziś jak zainstalować i skonfigurować serwer nginx z obsługą php5
Pierwsze co to instalujemy sam serwer.
apt-get install nginx
Cały config serwera jest w /etc/nginx
Poniżej zamieszczam moją zawartość pliku /etc/nginx/sites-available/default. Wartość zmiennej root (u mnie /var/www/) należy zmienić na katalog, który ma być udostępniany przez serwer.
server {
listen 80;
server_name localhost;
root /var/www;
index index.php index.html index.htm;
access_log /var/log/nginx/localhost.access.log;
location = / {
autoindex on;
}
location / {
autoindex on;
}
## obsługa PHP na serwerze FastCGI
location ~ \.php($|/) {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param PATH_INFO $path_info;
}
# wyłączenie logowania obrazków
# i nagłówek Expires
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
}
Po czym restartujemy serwer
/etc/init.d/nginx restart
Instalujemy interfejs CGI dla interpretera PHP (php5-cgi) i starter serwera FCGI (spawn-fcgi).
apt-get install php5-cgi spawn-fcgi
Teraz tworzymy skrypt dzięki któremu CGI będzie startować z systemem. (wartość zmiennej CHDIR należy zmienić na wartość dyrektywy root z konfiguracji nginxa)
nano /etc/init.d/php5-cgi
#!/bin/bash
NAME="PHP5-CGI"
SPAWNFCGI="/usr/bin/spawn-fcgi"
PHPFCGI="/usr/bin/php5-cgi"
FCGIPORT="9000"
FCGIADDR="127.0.0.1"
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=1000
ALLOWED_ENV="PATH USER"
PIDFILE="/var/run/php5-cgi.pid"
USER="www-data"
CHDIR="/var/www"
## no config below this line
SCRIPTNAME=$0
COMMAND=$1
USAGE="Usage: $SCRIPTNAME {start|stop|restart|status}"
DESC="$NAME at $FCGIADDR:$FCGIPORT"
if [ -z "$PHP_FCGI_CHILDREN" ]; then
PHP_FCGI_CHILDREN=5
fi
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN"
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS"
ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS"
PID=`cat $PIDFILE`
function err {
echo $@ >2
}
function die {
err $@
exit 1
}
function is_daemon_running {
test -n "$PID" && test -n "`ps h -p $PID`" && return 0
return 1
}
function daemon_start {
if is_daemon_running; then
err "Process already running: $PID"
return 1
fi
EX="$SPAWNFCGI -d $CHDIR -u $USER -a $FCGIADDR -p $FCGIPORT -f $PHPFCGI -P $PIDFILE"
E=""
for i in $ALLOWED_ENV; do
E="$E $i=${!i}"
done
nohup env - $E sh -c "$EX" &> /dev/null &
return $?
}
function daemon_stop {
if [ -z "$PID" ]; then
err "No pid found in pidfile: $PIDFILE"
return 1
elif [ -z "`ps h $PID`" ]; then
err "Process not running: $PID"
return 1
fi
kill $PID
return $?
}
RETVAL=0
function save_retval {
RETVAL=$1
log_end_msg $1
}
. /lib/lsb/init-functions
case "$COMMAND" in
start)
log_daemon_msg "Starting $DESC"
daemon_start
save_retval $?
;;
stop)
log_daemon_msg "Stopping $DESC"
daemon_stop
save_retval $?
;;
restart)
log_daemon_msg "Restarting $DESC"
daemon_stop
sleep 1
daemon_start
save_retval $?
;;
status)
if is_daemon_running; then
log_daemon_msg "$DESC running"
save_retval 0
else
log_daemon_msg "$DESC not running"
save_retval 1
fi
;;
*)
die $USAGE
;;
esac
exit $RETVAL
Powyższy skrypt zapewnia poprawny start i stop serwera FCGI oraz kontrolę zmiennych środowiskowych. Konfiguracja obejmuje nazwę hosta i port, ilość procesów interpretera PHP oraz ilość obsługiwanych zapytań przed restartem procesu. W przypadku, gdy potrzebny będzie drugi serwer PHP5-CGI na tej samej maszynie, wystarczy sklonować ten skrypt konfigurując na inny host lub port.
Jeżeli chcemy by startowało to razem z systemem wydajemy polecenie
update-rc.d php5-cgi defaults



