一、环境

    系统:CentOS6.4x64最小化安装

    IP:    192.168.3.48

二、安装基础软件

[root@lnmp-test ~]# yum install make gcc gcc-c++ openssl-devel -y

三、创建运行nginx的用户www

[root@lnmp-test ~]# groupadd www
[root@lnmp-test ~]# useradd -s /sbin/nologin -g www www

四、安装pcre

#先下载软件
[root@lnmp-test ~]# wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz/download
[root@lnmp-test ~]# tar xf pcre-8.30.tar.gz 
[root@lnmp-test ~]# cd pcre-8.30
[root@lnmp-test pcre-8.30]# ./configure 
[root@lnmp-test pcre-8.30]# make && make install
[root@lnmp-test pcre-8.30]# ldconfig

五、安装nginx1.6.0

    安装nginx

#下载nginx软件包
[root@lnmp-test ~]# wget http://mirrors.sohu.com/nginx/nginx-1.6.0.tar.gz 
[root@lnmp-test ~]# tar xf nginx-1.6.0.tar.gz 
[root@lnmp-test ~]# cd nginx-1.6.0 
[root@lnmp-test nginx-1.6.0]# ./configure --user=www --group=www --prefix=/usr/local/nginx-1.6.0 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
[root@lnmp-test nginx-1.6.0]# make && make install

#配置软链接
[root@lnmp-test nginx-1.6.0]# ln -s /usr/local/nginx-1.6.0/ /usr/local/nginx
[root@lnmp-test nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

    修改nginx.conf文件为以下内容

user  www www;

worker_processes auto;

error_log  /usr/local/nginx/logs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
	{
		use epoll;
		worker_connections 51200;
		multi_accept on;
	}

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 50m;

		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 256k;

		gzip on;
		gzip_min_length  1k;
		gzip_buffers     4 16k;
		gzip_http_version 1.0;
		gzip_comp_level 2;
		gzip_types       text/plain application/x-javascript text/css application/xml;
		gzip_vary on;
		gzip_proxied        expired no-cache no-store private auth;
		gzip_disable        "MSIE [1-6]\.";

		#limit_conn_zone $binary_remote_addr zone=perip:10m;
		##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

		server_tokens off;
		#log format
		log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';

server
	{
		listen 80 default;
		#listen [::]:80 default ipv6only=on;
		server_name www.myweb.com;
		index index.html index.htm index.php;
		root  /var/www/default;

		#error_page   404   /404.html;
		location ~ [^/]\.php(/|$)
			{
				# comment try_files $uri =404; to enable pathinfo
				try_files $uri =404;
				fastcgi_pass  unix:/tmp/php-cgi.sock;
				fastcgi_index index.php;
				include fastcgi.conf;
				#include pathinfo.conf;
			}

		location /nginx_status {
			stub_status on;
			access_log   off;
		}

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

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

		access_log  /var/www/wwwlogs/access.log  access;
	}
include vhost/*.conf;
}

    创建nginx启动脚本

[root@lnmp-test nginx-1.6.0]# vim /etc/init.d/nginx
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
	start)
		echo -n "Starting $NAME... "

		if netstat -tnpl | grep -q nginx;then
	        echo "$NAME (pid `pidof $NAME`) already running."
	        exit 1
		fi

		$NGINX_BIN -c $CONFIGFILE

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Stoping $NAME... "

		if ! netstat -tnpl | grep -q nginx; then
			echo "$NAME is not running."
			exit 1
		fi

		$NGINX_BIN -s stop

		if [ "$?" != 0 ] ; then
			echo " failed. Use force-quit"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if netstat -tnpl | grep -q nginx; then
			PID=`pidof nginx`
			echo "$NAME (pid $PID) is running..."
		else
			echo "$NAME is stopped"
			exit 0
		fi
	;;

	force-quit)
		echo -n "Terminating $NAME... "

		if ! netstat -tnpl | grep -q nginx; then
			echo "$NAME is not running."
			exit 1
		fi

		kill `pidof $NAME`

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$SCRIPTNAME stop
		sleep 1
		$SCRIPTNAME start
	;;

	reload)

		echo -n "Reload service $NAME... "

		if netstat -tnpl | grep -q nginx; then
			$NGINX_BIN -s reload
			echo " done"
		else
			echo "$NAME is not running, can't reload."
			exit 1
		fi
	;;

	configtest)

		echo -n "Test $NAME configure files... "

		$NGINX_BIN -t
	;;

	*)
		echo "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"
		exit 1
	;;

esac

#添加脚本执行权限
[root@lnmp-test nginx-1.6.0]# chmod +x /etc/init.d/nginx 
[root@lnmp-test nginx-1.6.0]# echo "/usr/local/lib/" >>/etc/ld.so.conf
[root@lnmp-test nginx-1.6.0]# ldconfig

#添加nginx配置文件中需要的目录
[root@lnmp-test nginx-1.6.0]# mkdir -p /var/www/default
[root@lnmp-test nginx-1.6.0]# chmod +w /var/www/default
[root@lnmp-test nginx-1.6.0]# mkdir -p /var/www/wwwlogs
[root@lnmp-test nginx-1.6.0]# chmod 777 /var/www/wwwlogs
[root@lnmp-test nginx-1.6.0]# chown -R www:www /var/www/default

#重新启动nginx
[root@lnmp-test nginx-1.6.0]# /etc/init.d/nginx start
Starting nginx...  done
[root@lnmp-test nginx-1.6.0]# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      29754/nginx         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      22280/sshd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1318/master         
tcp        0     52 192.168.3.48:22             192.168.3.2:7461            ESTABLISHED 1473/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      22280/sshd          
tcp        0      0 ::1:25                      :::*                        LISTEN      1318/master

    验证结果

#创建站点根目录文件
[root@lnmp-test nginx-1.6.0]# echo "welcome to nginx" >> /var/www/default/index.html
#编辑/etc/hosts文件,添加,并测试结果
[root@lnmp-test nginx-1.6.0]# curl www.myweb.com
welcome to nginx

#添加nginx到开机自动启动
[root@lnmp-test ~]# chkconfig --add nginx
[root@lnmp-test ~]# chkconfig nginx on


下面是后来写的安装脚本

#!/bin/bash

cur_dir=$(pwd)
NGINXVERSION='nginx-1.6.0'
export LANG=zh_CN.UTF-8

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

create_nginx_conf(){
cat >>/usr/local/nginx/conf/nginx.conf<<EOF
user  www www;
 
worker_processes auto;
 
error_log  /usr/local/nginx/logs/nginx_error.log  crit;
 
pid        /usr/local/nginx/logs/nginx.pid;
 
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
 
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
 
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 50m;
 
        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 256k;
 
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied        expired no-cache no-store private auth;
        gzip_disable        "MSIE [1-6]\.";
 
        #limit_conn_zone \$binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
 
        server_tokens off;
        #log format
        log_format  access  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
             '\$status \$body_bytes_sent "\$http_referer" '
             '"\$http_user_agent" \$http_x_forwarded_for';
 
server
    {
        listen 80 default;
        #listen [::]:80 default ipv6only=on;
        server_name www.myweb.com;
        index index.html index.htm index.php;
        root  /var/www/default;
 
        #error_page   404   /404.html;
        location ~ [^/]\.php(/|$)
            {
                # comment try_files \$uri =404; to enable pathinfo
                try_files \$uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #include pathinfo.conf;
            }
 
        location /nginx_status {
            stub_status on;
            access_log   off;
        }
 
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)\$
            {
                expires      30d;
            }
 
        location ~ .*\.(js|css)?\$
            {
                expires      12h;
            }
 
        access_log  /var/www/wwwlogs/access.log  access;
    }
include vhost/*.conf;
}
EOF
}

create_nginx_init(){
cat >>/etc/init.d/nginx<<EOF
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/\$NAME
CONFIGFILE=/usr/local/nginx/conf/\$NAME.conf
PIDFILE=/usr/local/nginx/logs/\$NAME.pid
SCRIPTNAME=/etc/init.d/\$NAME
 
case "\$1" in
    start)
        echo -n "Starting \$NAME... "
 
        if netstat -tnpl | grep -q nginx;then
            echo "\$NAME (pid \`pidof \$NAME\`) already running."
            exit 1
        fi
 
        \$NGINX_BIN -c \$CONFIGFILE
 
        if [ "\$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    stop)
        echo -n "Stoping \$NAME... "
 
        if ! netstat -tnpl | grep -q nginx; then
            echo "\$NAME is not running."
            exit 1
        fi
 
        \$NGINX_BIN -s stop
 
        if [ "\$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    status)
        if netstat -tnpl | grep -q nginx; then
            PID=\`pidof nginx\`
            echo "\$NAME (pid \$PID) is running..."
        else
            echo "\$NAME is stopped"
            exit 0
        fi
    ;;
 
    force-quit)
        echo -n "Terminating \$NAME... "
 
        if ! netstat -tnpl | grep -q nginx; then
            echo "\$NAME is not running."
            exit 1
        fi
 
        kill \`pidof \$NAME\`
 
        if [ "\$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
 
    restart)
        \$SCRIPTNAME stop
        sleep 1
        \$SCRIPTNAME start
    ;;
 
    reload)
 
        echo -n "Reload service \$NAME... "
 
        if netstat -tnpl | grep -q nginx; then
            \$NGINX_BIN -s reload
            echo " done"
        else
            echo "\$NAME is not running, can't reload."
            exit 1
        fi
    ;;
 
    configtest)
 
        echo -n "Test \$NAME configure files... "
 
        \$NGINX_BIN -t
    ;;
 
    *)
        echo "Usage: \$SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
    ;;
 
esac
EOF
}

#install_nginx
install_nginx(){
        cd $cur_dir
        yum install make gcc gcc-c++ openssl-devel -y
        #add user www for nginx
        id www &>/dev/null
        if [ $? -ne 0 ];then
                groupadd www
                useradd -s /sbin/nologin -g www www
        fi
        wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz/download
        if [ $? -ne 0 ];then
                echo "download pcre package is fail"
                exit $?
        fi
        tar xf  pcre-8.30.tar.gz
        cd pcre-8.30
        ./configure
        make && make install
        if [ $? -eq 0 ];then
                echo "install pcre is successful!!!"
        else
                echo "install pcre is fail!!!"
                exit $?
        fi
        echo "/usr/local/lib/" >>/etc/ld.so.conf
        ldconfig
        #download nginx package
        cd $cur_dir
        wget http://mirrors.sohu.com/nginx/$NGINXVERSION.tar.gz
        if [ $? -ne 0 ];then
                echo "download nginx is fail!!!"
                exit $?
        fi
        tar xf $NGINXVERSION.tar.gz
        cd $NGINXVERSION
         ./configure --user=www --group=www --prefix=/usr/local/$NGINXVERSION --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 
         make && make install
        if [ $? -ne 0 ];then
                echo "install nginx fail!!!"
                exit $?
        fi 
        #links
        ln -s /usr/local/$NGINXVERSION /usr/local/nginx
        ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
        mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
        #create file nginx.conf
        create_nginx_conf 
        mkdir -p /var/www/default
        chmod +w /var/www/default
        mkdir -p /var/www/wwwlogs
        chmod 777 /var/www/wwwlogs 
        chown -R www:www /var/www/default
        cp /usr/local/nginx/html/index.html /var/www/default/index.html 
        #create start scripts for nginx
        create_nginx_init
        chmod +x /etc/init.d/nginx
        chkconfig --add nginx
        chkconfig nginx on
        /etc/init.d/nginx start
        if [ $? -eq 0 ];then
                action "start nginx" /bin/true
                echo "+---------------------------------+"
                echo "+------nginx  install done--------+"
                echo "+---------------------------------+"
        fi
}

install_nginx