将来的你会感谢现在努力的自己,骚年,趁年轻多努力学习 ------ Jasper_boy

shell脚本安装nginx

#!/bin/bash

N_url="http://nginx.org/download/nginx-1.8.0.tar.gz"
N_pack=`echo $N_url|awk -F '/' '{print $5}'`
N_upack=`echo $N_url|awk -F '/' '{print $5}'|awk -F '.' '{print $1"."$2"."$3}'`
N_dir="/usr/local/nginx"

function nginx (){
    if [ `whoami` != "root" ];then
        echo "installtion this package needs root user."
        exit1
    fi

    yum -y install openssl-devel pcre-devel zlib-devel
  [ ! $? -eq 0 ] && exit 
    wget -c $N_url
  [ ! $? -eq 0 ] && exit 
    tar zxf $N_pack
    cd $N_upack
    useradd -M -s /sbin/nologin www
    ./configure --prefix=$N_dir --user=www --group=www --with-http_stub_status_module --with-http_ssl_module 
    [ ! $? -eq 0 ] && exit 
    make -j`grep processor /proc/cpuinfo | wc -l`  && make install
    mkdir $N_dir/conf/vhost    
    mkdir /data/www -p
}

function config (){

cat > $N_dir/conf/nginx.conf <<'EOF'
user  www www;
worker_processes auto;
error_log  /usr/local/nginx/logs/error.log  crit;
pid        /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;

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

http 
{
  include       mime.types;
  default_type  application/octet-stream;
  charset utf-8;
  server_tokens off; 
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 60;
  client_header_timeout 10; 
  client_body_timeout 10; 
  client_header_buffer_size 4k;
  server_names_hash_bucket_size 128;
  large_client_header_buffers 4 32k;
  client_max_body_size 100m;
  send_timeout 10;
  reset_timedout_connection on;
  open_file_cache max=102400 inactive=20s;
  open_file_cache_min_uses 1;
  open_file_cache_valid 30s;
  
  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 16k;
  gzip_http_version   1.1;
  gzip_vary on;
  gzip_comp_level 2;
  gzip_disable "MSIE [1-6]\.";
  gzip_proxied any; 
  gzip_types       text/plain application/x-javascript text/css application/xml;
 
  log_format  access  '$remote_addr - $remote_user [$time_local] "$request"'
              '$status $body_bytes_sent "$http_referer"'
              '"$http_user_agent" $http_x_forwarded_for';
  access_log  /usr/local/nginx/logs/access.log  access;

  include vhost/*.conf;
}
EOF

cat > $N_dir/conf/vhost/test.conf << EOF
server
  {
    listen       80;
    server_name  localhost;
    index index.html index.htm index.php;
    root  /data/www;

    location /status {
        stub_status on;
        access_log   off;
    }
}
EOF

cat > /data/www/index.html << EOF
<html>
<head>
<title> Nginx web server </title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
</body>
</html>
EOF

cat >> /etc/security/limits.conf <<'EOF'
*     soft     nofile     102400
*     hard     nofile     102400
*     soft     nproc     102400
*     hard     nproc     102400
EOF

ulimit -SHn 102400

sed 's@1024@102400@g' /etc/security/limits.d/90-nproc.conf

cat > /etc/sysctl.conf <<'EOF'
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
EOF

cat > /etc/init.d/nginx <<'EOF'
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /var/run/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/nginx/sbin/nginx"
prog=$(basename $nginx)
  
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  
lockfile=/var/lock/subsys/nginx
  
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed "s/[^*]*--user=\([^ ]*\).*/\1/g" -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep "configure arguments:"`
   for opt in $options; do
       if [ `echo $opt | grep ".*-temp-path"` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
  
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
    sleep 1
    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        
EOF
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
echo -e "\033[37m =========  nginx install success  =========="
echo -e "\033[37m =========  Please reboot system   =========="
}
nginx 
config

 

posted @ 2017-02-23 18:03  Jasper_boy  阅读(995)  评论(0编辑  收藏  举报