centos 一键安装配置nginx脚本

centos 一键安装配置nginx脚本

installNginx.ssh

用vi或则vim编辑 installNginx.ssh

#!/bin/bash
# author:kwin
# Email:[email protected]

src="/usr/local/src/"
cd $src

#找到指定进程,并杀死
#findPortKill 80
findPortKill (){
  processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`
  for i in $processe
    do
#  echo "Kill the $1 process [ $i ]"
  kill -9 $i
    done
}

#将命令所在目录添加到系统参数PATH中,方便调用
addToPATH(){

bin=${1}

echo $PATH|grep ${bin} >/dev/null
if [ $? -ne 0 ]; then

echo "export PATH=\$PATH:${bin}">>/etc/profile
fi
}



nginxConf(){

cp /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak
sed -i '35,79c  include  ../site/*.conf;' /usr/local/nginx/conf/nginx.conf
mkdir /usr/local/nginx/site

cat>/usr/local/nginx/site/default.conf<80;
        server_name  localhost;

        gzip on; #开启gizip
        gzip_buffers 32 4K;#压缩在内存中缓冲32块? 每块4K
        gzip_comp_level 6 ;#压缩级别 推荐6
        gzip_min_length 4000;#开始压缩的最小长度4bit
        gzip_types text/css text/xml apploation/x-javascript;#只对CSS、XML、HTML、JS文件进行压缩

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        root   /var/www;

        location / {
          #   try_files \$uri \$uri/ /index.php?\$query_string;
            index  index.html index.htm index.php;

        #如果是jpg、jpeg、gif、png、js、css则缓存一天
        if (\$fastcgi_script_name ~* \.[jpg|jpeg|gif|png|js|css] ) {
            expires 1d;
            }
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php\$ { 

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index index.php; 
            fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
            include        fastcgi_params; 

        } 

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php\$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    } 
EOF
}


addNginxService(){
cat>/etc/init.d/nginx<#!/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/nginx/conf/nginx.conf  
# pidfile:     /usr/local/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/nginx/sbin/nginx"  
prog="Nginx"  

NGINX_CONF_FILE="/usr/local/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  
EOF

 chmod 755 /etc/init.d/nginx 
 chkconfig --add nginx
 chkconfig --level 2345 nginx on
}

#安装nginx
installNginx(){

yum -y  install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perlddd

fileName="nginx-1.9.9"
package="${fileName}.tar.gz"
installDir="/usr/local/nginx"

if test ! -f ${package}
then
wget http://nginx.org/download/${package}
fi


tar zxvf $package

cd $fileName
./configure --prefix=${installDir}
make && make install
echo "安装完成"


nginxConf


#如果出现错误 找到80占用的进程并杀死,再重启
#如果还有问题 请自行调试配置文件
/usr/local/nginx/sbin/nginx 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 80
/usr/local/nginx/sbin/nginx
fi

#sleep : 默认以秒为单位。
#usleep : 默认以微秒为单位。
#1s = 1000ms = 1000000us

usleep 100000

pid=`cat /usr/local/nginx/logs/nginx.pid`

echo "nginx 已经启动,进程号为${pid}"

bin="${installDir}/sbin"

#将命令所在目录添加到系统参数PATH中,方便调用
addToPATH ${bin}


#设置开机启动
addNginxService
}

installNginx

赋予installNginx.ssh文件可执行限权

# chmod +x installNginx.ssh

执行安装

./installNginx.ssh

基本命令

service nginx [start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest]

虚拟主机管理

在安装目录(//usr/local/nginx/)里的site目录中有一个default.conf默认虚拟主机管理模版,后续拓展虚拟主机可以直接复制default.conf文件,文件名格式为 *.conf,只要后缀为.conf就能解析

你可能感兴趣的:(liunx,nginx)