nginx

编译安装

安装pcre库

wget http://lnmpp.googlecode.com/files/pcre-8.01.tar.gz
tar xf pcre-8.01.tar.gz
cd pcre-8.01
./configure
make install

安装nginx-1.0.8.tar.gz

wget http://nginx.org/download/nginx-1.0.8.tar.gz
tar xf nginx-1.0.8.tar.gz
cd nginx-1.0.8

#下载[memc-nginx-module],在站点中我们用这个模块访问flare来存储图片
wget https://nodeload.github.com/agentzh/memc-nginx-module/tarball/v0.13rc1
tar xvf agentzh-memc-nginx-module-v0.13rc1-0-g5b0504b.tar.gz
mv agentzh-memc-nginx-module-5b0504b memc-nginx-module

./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=memc-nginx-module
make -j2
make install

参数配置

fastcgi

创建php-cgi.conf

cat > /usr/local/webserver/nginx/conf/php_fcgi.conf <<'EOF'
fastcgi_pass  unix:/tmp/php_fcgi.sock;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

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_FILENAME    $document_root$fastcgi_script_name;
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  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;
EOF

pathinfo

创建pathinfo.conf

cat > /usr/local/webserver/nginx/conf/pathinfo.conf << 'EOF'
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "(.+?\.php)(/.*)") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
EOF

平滑重启方法

/usr/local/webserver/nginx/sbin/nginx -s reload

nginx.conf

创建nginx.conf

cat > /usr/local/webserver/nginx/conf/nginx.conf <<'EOF'
user  www www;
worker_processes 4;
error_log  /var/log/nginx_error.log  crit;
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;

  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  gzip    on;
  gzip_min_length   1k;
  gzip_buffers   4 8k;
  gzip_http_version  1.1;
  gzip_types   text/plain application/x-javascript text/css  application/xml;
  gzip_disable "MSIE [1-6]\.";

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

  include site/*.conf;
}
EOF

建立站点目录

创建目录

mkdir -pv /data/www
chmod -R 777 /data/www
chown -R www:www /data/www

mkdir -pv /usr/local/webserver/nginx/conf/site
chmod -R 777 /usr/local/webserver/nginx/conf/site

创建默认站点配置文件default.conf

cat > /usr/local/webserver/nginx/conf/site/default.conf <<'EOF'
server
{
    listen       80;
    server_name  default;
    index index.html index.htm index.php;
    root  /data/www;


    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
        }
    }


    location ~ .*\.php[/]?
    {
      include php_fcgi.conf;
               include pathinfo.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }

    access_log /var/log/nginx_access.log;

}
EOF

重启nginx

/usr/local/webserver/nginx/sbin/nginx -s reload

启动|停止|重启 脚本

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/webserver/nginx/conf/nginx.conf
# pidfile:     /usr/local/webserver/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

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

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

nginx="/usr/local/webserver/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/webserver/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

设置开机启动

chkconfig --add nginx
chkconfig nginx on

测试

cat > /data/www/index.php <<'EOF'
<?php
    echo 'Welcome to Nginx!'
EOF

使用curl查看

[root@ecos02 ~]# curl 10.0.0.2
结果应该返回
Welcome to Nginx!

站点数据备份

设置SSH无密码登录

在ecos01上生成key

ssh-keygen -t rsa
一路回车即可

然后执行下面的脚本拷贝key

#/bin/sh

hosts='10.0.0.2 10.0.0.3'
for host in $hosts
do
            ssh root@$host "mkdir ~/.ssh"
            ssh root@$host "echo `cat ~/.ssh/id_rsa.pub` >> ~/.ssh/authorized_keys"
done

同步WEB文件

三台机器都需要安装rsync

rsync手动同步

cat > rsync.sh <<'EOF'
#!/bin/bash

host_list="10.0.0.2 10.0.0.3"
file_list="app config data  demo public themes  index.php  license.txt readme.txt  rpc.txt "
for host_item in $host_list
do
    echo "sync to $host_item";
    for file_item in $file_list
    do
        rsync -a --delete -e ssh /data/www/ecstore/$file_item root@$host_item:/data/www/ecstore
    done
done
echo "all done"
EOF

执行脚本同步

./rsync.sh

自动同步

inotifyrsync.sh

cat > inotifyrsync.sh <<'EOF'
#!/bin/bash
host=10.0.0.3
src=/data/www/xingmei/

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,move,delete,create,attrib $src | while read file
do
  /usr/bin/rsync -avz --delete --progress $src root@$host:$src
done
EOF
后台运行
./inotifyrsync.sh &

参考资料

NGINX文档