#!/bin/bash
tempPath="/data0/temp/"
mkdir ${tempPath}
cpuNum=$(cat /proc/cpuinfo | grep processor | wc -l)
function downloadFile(){
    saveFilename=$1
    downloadUrl=$2
    if [[ ! -f ${saveFilename} ]];then
        wget -O ${saveFilename} "${downloadUrl}"
        if [[ ! -f ${saveFilename} ]];then
            echo "${saveFilename} download fail url=>${saveFilename}"
            exit
        fi
    fi
}
#############################################################安装nginx##########################################################
groupadd -f www
useradd -g www www

cd ${tempPath}
downloadFile openssl-1.0.2p.tar.gz https://www.openssl.org/source/openssl-1.0.2p.tar.gz
tar xzf openssl-1.0.2p.tar.gz
opensslDir=${tempPath}/openssl-1.0.2p
cd openssl-1.0.2p
./config --prefix=/usr && make -j ${cpuNum} && make install


cd ${tempPath}
downloadFile  jemalloc-5.1.0.tar.bz2  https://github.com/jemalloc/jemalloc/releases/download/5.1.0/jemalloc-5.1.0.tar.bz2
tar jxf jemalloc-5.1.0.tar.bz2
jemallocDir=${tempPath}/jemalloc-5.1.0

cd ${tempPath}
downloadFile tengine-2.2.3.tar.gz http://tengine.taobao.org/download/tengine-2.2.3.tar.gz
tar xzf tengine-2.2.3.tar.gz && cd tengine-2.2.3
./configure --prefix=/usr/local/nginx \
--user=www --group=www \
--with-jemalloc \
--with-ld-opt=-lrt \
--with-openssl=${opensslDir} \
--with-jemalloc=${jemallocDir} \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-pcre
make -j ${cpuNum} && make install

mkdir -p /usr/local/nginx/conf/vhost

#############################################################nginx 主要配置文件##########################################################

(cat << EOF
user  www www;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile    102400;
error_log   /data0/logs/nginx_error.log;
pid         logs/nginx.pid;

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

http {
        include                         mime.types;
        default_type                    application/octet-stream;
        charset                         utf-8;
        server_names_hash_bucket_size   256;
        client_header_buffer_size       256k;
        large_client_header_buffers     4 256k;
        client_header_timeout           300s;
        client_body_timeout             300s;
        client_max_body_size            100m;
        server_name_in_redirect         off;
        log_format main '\$remote_addr - \$remote_user [\$time_local] \$request \$status \$body_bytes_sent \$host \$upstream_addr \$upstream_response_time \$http_user_agent \$http_referer \$gzip_ratio';
        access_log                      off;
        sendfile                        on;
        tcp_nopush                      on;
        tcp_nodelay                     on;
        server_tokens                   off;
        keepalive_timeout               300;
        fastcgi_connect_timeout         300;
        fastcgi_send_timeout            300;
        fastcgi_read_timeout            300;
        fastcgi_buffer_size             128k;
        fastcgi_buffers                 256 64k;
        fastcgi_busy_buffers_size       128k;
        fastcgi_temp_file_write_size    128k;

        gzip                            on;
        gzip_min_length                 5k;
        gzip_buffers                    96 8k;
        gzip_comp_level                 5;
        gzip_vary                       on;
        gzip_static                     on;
        gzip_http_version               1.1;
        gzip_disable                    "MSIE [1-6]\." "Mozilla/4\.0[678]";
        gzip_types                      text/xml text/plain text/css application/javascript application/x-javascript  application/rss+xml image/jpeg image/gif image/png application/json;

        open_file_cache                 max=200000 inactive=20s;
        open_file_cache_valid           30s;
        open_file_cache_min_uses        2;
        open_file_cache_errors          on;
        underscores_in_headers          on; 

        upstream unix_tmp_php_cgi_sock {
                server unix:/dev/shm/php_fpm_1.sock;
                server unix:/dev/shm/php_fpm_2.sock;
        }
        include vhost/*.conf;
}

EOF
) > /usr/local/nginx/conf/nginx.conf



#############################################################nginx api配置文件##########################################################
(cat << EOF
server {
        listen 80;
        server_name api.foundao.com;
        root  /data0/dk/api/;
        access_log /data0/logs/api.access.log  main buffer=64k flush=15s;
	    location / {
                index  index.php index.html;
        }
	    location ~ \.php\$ {
                fastcgi_pass  unix_tmp_php_cgi_sock;
                fastcgi_index  index.php;
                include        fastcgi.conf;
        }
}
EOF
) > /usr/local/nginx/conf/vhost/default_api.conf


#############################################################nginx cms配置文件##########################################################
(cat << EOF
server {
        listen 80;
        server_name cms.foundao.com;
        root  /data0/dk/cms;

        access_log  /data0/logs/cms.access.log  main buffer=64k flush=15s;

        location / {
                index  index.php index.html;
        }

        location ~ \.php\$ {
                fastcgi_pass  unix_tmp_php_cgi_sock;
                fastcgi_index  index.php;
                include        fastcgi.conf;
        }

        location /cms/{
                index index.php;
                if (!-e \$request_filename) {
                    rewrite ^/(.*)$ /cms/index.php?\$1 last;
                    break;
                }
        }
}
EOF
) > /usr/local/nginx/conf/vhost/default_cms.conf

#############################################################nginx cdn配置文件##########################################################
(cat << EOF
server {
        listen 80;
        server_name cdn.foundao.com;
        root  /data0/dk/cdn;
	    access_log  /data0/logs/cdn.access.log  main buffer=64k flush=15s;
        add_header 'Access-Control-Allow-Origin' '*';

        location / {
                index index.html;
        }

        location ~* ^.+.(gif|jpg|jpeg|png|mp4|flv|mov|avi|mp3|ts)\$ {
        	  expires 300s;
        }

        location ~* ^.+.(html|js|css|m3u8)\$ {
              expires  30s;
        }
}
EOF
) > /usr/local/nginx/conf/vhost/default_cdn.conf



#############################################################nginx 启动配置文件##########################################################
(cat << EOF
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=\$PATH/sbin/\${NAME}
CONFIGFILE=\$PATH/conf/\${NAME}.conf
PIDFILE=\$PATH/logs/\${NAME}.pid
SCRIPTNAME=/etc/init.d/\${NAME}
set -e
[ -x "\$DAEMON" ] || exit 0
do_start() {
    \${DAEMON} -c \${CONFIGFILE} || echo -n "nginx already running"
}
do_stop() {
    \${DAEMON} -s stop || echo -n "nginx not running"
}
do_reload() {
    \${DAEMON} -s reload || echo -n "nginx can't reload"
}

case "\$1" in
    start)
        echo -n "Starting \$DESC: \$NAME"
        do_start
        echo "."
    ;;
    stop)
        echo -n "Stopping \$DESC: \$NAME"
        do_stop
        echo "."
    ;;
    reload|graceful)
        echo -n "Reloading \$DESC configuration..."
        do_reload
        echo "."
    ;;
    restart)
        echo -n "Restarting \$DESC: \$NAME"
        do_stop
        do_start
        echo "."
    ;;
    *)
        echo "Usage: \$SCRIPTNAME {start|stop|reload|restart}" >&2
        exit 3
    ;;
esac

exit 0
EOF
) > /etc/init.d/nginx

chmod o+rw /etc/init.d/nginx
chkconfig nginx on