nginx 설치 및 셋팅

0. nginx


사용자 삽입 이미지
nginx('엔진엑스'라고 읽는다. 러시아어가 기본이니 정확한 발음은 모르겠다)는 작고 파워풀한 웹엔진의 능력과 간결한 셋팅은 유명하다.
테스트 해본결과 static 파일의 경우 apache나 lighttpd에 비해 월등한 성능을 보인다. 그러나 fastcgi+php쪽으로는 속도가 apache 보다 못하다.
proxy. cache or memcached 기능으로 다양한 조합을 하면 각각 상황에 알맞는 재미있는 구성이 가능할것 같다.

그래서 내가 주로 쓰는 centos, ubunt에 정리겸 설치 및 설정 방법을 정리해봤다.

1. centos 5.4 - install nginx

centos에는 배포본이 아직 없는듯 (0점대 버젼이라 그런가...)...암튼 그래서 구글링을 했다.

기본 깔아야 하는 패키지들은 다음과 같다
yum install gcc pcre-devel bzip2-devel openssl-devel

그리고 직접 소스를 받아서 컴파일 한다. 컴파일 옵션 참조: http://www.mman.pe.kr/?p=53
wget http://nginx.org/download/nginx-0.7.64.tar.gz
tar xvfz nginx-0.7.64.tar.gz
cd nginx-0.7.64
./configure \
--sbin-path=/usr/sbin \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-cc-opt="-I /usr/include/pcre"
make
make install

그리고 깔린 결과는 다음과 같다
  + using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1 library is not used
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/sbin"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/tmp/nginx/client/"
nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"

init script는 http://wiki.nginx.org 에 있다
http://wiki.nginx.org/RedHatNginxInitScript 에서 redhat쪽 init script 를 다운받아서
cp nginx /etc/init.d/
chmod 755 /etc/init.d/nginx
/usr/sbin/groupadd nginx
/usr/sbin/useradd -g nginx -s /sbin/nologin -c "Nginx" -M nginx
mkdir /var/tmp/nginx/
# chkconfig --add nginx
# chkconfig nginx on

/etc/nginx/nginx.conf를 수정한다
user  nginx nginx;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
server {
listen 80;
server_name localhost;
location / {
root /var/www/html
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

하면 완료.

일단 문제가 있는데, initscript로 restart가 잘 안된다. worker 프로세스가 죽는 시간이 좀 걸리는데, 그상태에서 start하는게 문제가 되는 듯하다. 근데 nginx는 꼭 restart로 서버를 리스타팅 할 필요가 없다

ps aux|egrep (PID|nginx) # 여기서 master [pid]를 알아낸후 
kill -HUP [pid]

이렇게 하면 무정지로 서버 리스타팅이 가능하다.

2. centos 5.4 - install nginx + php(with spawn-fcgi)

먼저 php패키지를 인스톨한다.(이외 필요한 php패키지 및 php-cache 프로그램은 알맞게 install)
yum install php php-dev

nginx의 경우 spawn-cgi를 같이 사용한다.
소스는 여기(http://redmine.lighttpd.net/projects/sp ··· %2Fbuild)에 있다.
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar xvfz spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi-1.6.3
./configure --prefix=/usr
make
make install

init 스크립트는 다음 참조 : http://bash.cyberciti.biz/web-server/rh ··· cript%2F
vi /etc/init.d/php-cgi
#!/bin/sh
#
# php-cgi - php-fastcgi swaping via spawn-fcgi
#
# chkconfig: - 85 15
# description: Run php-cgi as app server
# processname: php-cgi
# config: /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile: /var/run/php_cgi.pid
# Note: See how to use this script :
# http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx
server_group=nginx
server_childs=5
pidfile="/var/run/php_cgi.pid"

# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi

start() {
[ -x $php_cgi ] || exit 1
[ -x $spawnfcgi ] || exit 2
echo -n $"Starting $prog: "
daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
retval=$?
echo
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} $prog -QUIT
retval=$?
echo
[ -f ${pidfile} ] && /bin/rm -f ${pidfile}
return $retval
}

restart(){
stop
sleep 2
start
}

rh_status(){
status -p ${pidfile} $prog
}

case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
rh_status;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
esac

chmod +x /etc/init.d/php-cgi
vi /etc/nginx/nginx.conf
# server 섹션안에 다음 구문을 추가한다
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}


3. ubuntu 9.10 - nginx

유분투는 당연히 nginx 패키지를 가지고 있다.
apt-get install nginx


4. ubuntu 9.10 - nginx + php

apt-get install php5 php5-cgi spawn-fcgi

다음 셋팅으로 init script 만든다 참조: http://chrisjohnston.org/2009/setting-u ··· untu-904
vi /etc/init.d/php-fastcgi
#! /bin/sh
### BEGIN INIT INFO
# Provides: php-fastcgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop php-cgi in external FASTCGI mode
# Description: Start and stop php-cgi in external FASTCGI mode
### END INIT INFO

# Author: Kurt Zankl <[EMAIL PROTECTED]>

# Do NOT "set -e"

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

# 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.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# If the daemon is not enabled, give the user a warning and then exit,
# unless we are stopping the daemon
if [ "$START" != "yes" -a "$1" != "stop" ]; then
log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"
exit 0
fi

# Process configuration
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
DAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT -c $PHP_CONFIG_FILE"

do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
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()
{
# 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
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON
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.
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
;;
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 ;; # 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|restart|force-reload}" >&2
exit 3
;;
esac

init script 설정 파일 추가한다
chmod +x /etc/init.d/php-fastcgi
vi /etc/default/php-fastcgi
START=yes

# Which user runs PHP? (default: www-data)

EXEC_AS_USER=www-data

# Host and TCP port for FASTCGI-Listener (default: localhost:9000)

FCGI_HOST=localhost
FCGI_PORT=9000

# Environment variables, which are processed by PHP

PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000

기본 default vhost 설정에 추가
vi /etc/nginx/sites-available/default
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;
}

이러면 설정 완료
크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by 엽기민원

2010/01/20 01:23 2010/01/20 01:23
Response
No Trackback , No Comment
RSS :
http://yupmin.com/rss/response/175

Trackback URL : http://yupmin.com/trackback/175

Leave a comment
[로그인][오픈아이디란?]
« Previous : 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : ... 109 : Next »

블로그 이미지

엽기민원의 옴팡진 공간

- 엽기민원

Notices

Archives

Calendar

«   2010/03   »
  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      

Site Stats

Total hits:
12728
Today:
80
Yesterday:
86