User Tools

Site Tools


Sidebar


Tags Cloud
nginx_php_fastcgi

Совмещение Nginx и PHP (fast cgi)

  • Ставим nginx стандартными средствами OS
  • Качаем последнюю версию PHP с http://php.net, складываем в /opt/src
  • Тянем патчик с http://php-fpm.anight.org/download.html
  • Распаковываем и патчим исходники рнр.
  • В директории с пропатченными исходниками создаем файлик MyConfig.cgi.sh со следующим содержимым:
#!/bin/sh
CFLAGS="-O2" ./configure  --prefix=/opt/php-x.x.x-fcgi \
  --enable-calendar \
  --enable-sysvsem \
  --enable-sysvshm \
  --enable-sysvmsg \
  --enable-ftp \
  --with-mcrypt \
  --with-mysql \
  --with-mysqli \
  --enable-bcmath \
  --with-bz2 \
  --with-gd \
  --with-jpeg-dir \
  --with-zlib-dir \
  --with-curl \
  --enable-sockets \
  --with-iconv \
  --enable-mbstring \
  --enable-mbregex \
  --enable-ctype \
  --enable-shmop \
  --enable-wddx \
  --with-imap \
  --with-freetype-dir \
  --with-xmlrpc \
  --enable-dbase \
  --with-mime-magic \
  --with-mhash \
  --with-gettext \
  --with-imap-ssl \
  --with-pear \
  --with-kerberos \
  --with-openssl \
  --with-snmp \
  --with-config-file-path=/opt/etc/phpX \
  --enable-fastcgi \
  --enable-force-cgi-redirect \
  --enable-fpm
  • Собираем командами
# sh MyConfig.cgi.sh
# make
# make install
  • Создаем в /opt/scripts скрипт fcgi.sh со следующим содержимым:
#!/bin/bash
# ABSOLUTE path to the PHP binary
PHPFCGI="/opt/php-x.x.x-fcgi/bin/php-cgi"
# tcp-port to bind on
FCGIPORT="8888"
# IP to bind on
FCGIADDR="127.0.0.1"
# number of PHP children to spawn
PHP_FCGI_CHILDREN=5
# number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000
# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"
# if this script is run as root switch to the following user
USERID=www-data
################## no config below this line
if test x$PHP_FCGI_CHILDREN = x; 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"
if test x$UID = x0; then
  EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"
else
  EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
fi
echo $EX
# copy the allowed environment variables
E=
for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done
# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &
  • За что отвечают параметры, думается мне, можно понять и по комментариям, так что добавляем скрипт в автозагрузку методом OS, для которой проводится установка.
  • Шаблон виртуального хоста в nginx будет выглядеть таким образом:
server {
    listen       1.2.3.4:80;
    server_name  domain.com www.domain.com;
    access_log  /dev/null;
    # max POST size in bytes
    client_max_body_size 1000000000;
    location / {
        root   /home/clients/ftp_login/domains/domain.com/html;
        index  index.html index.htm index.php;
    }
    location ~ \.php$ {
       # Commented block - for accelerated POST - nginx should be 0.5.9+
       # fastcgi_pass_request_body off;
       # client_body_in_file_only clean;
       # fastcgi_param  REQUEST_BODY_FILE  $request_body_file;
       fastcgi_pass   127.0.0.1:8888;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /home/clients/ftp_login/domains/domain.com/html$fastcgi_script_name;
       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  REDIRECT_STATUS  200;
    }
    # hotlink protection
    location ~* ^.+\.(gif|jpg|mpg|mp3|mpeg|avi)$ {
       valid_referers none blocked domain.com www.domain.com;
       if ($invalid_referer) {
          return   403;
       }
    }
}
  • Запускаем скрипт и nginx.
nginx_php_fastcgi.txt · Last modified: 2013/12/15 16:48 by kyxap