shell一键部署nginx服务

shell一键部署nginx服务

所用工具包: nginx-1.12.0.tar.gz
使用前将工具包拖到/opt目录下即可
yum源可自动配置

############配置yum源###########
#!/bin/bash
echo -e "\033[31m =====正在验证当前为仅主机还是NAT模式===== \033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\033[31m 检测当前为NAT模式,为您配置在线yum源 \033[0m"
mkdir -p /etc/yum.repos.d/repo.bak

mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null

yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\033[31m 在线源已配置完成 \033[0m"

else
echo -e "\033[31m 检测当前为仅主机模式,为您配置本地yum源 \033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak

mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null

echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null

df -h | grep "/mnt" 
if [ $? -ne 0 ];then
echo -e "\033[31m 检测当前为仅主机模式,但光盘未连接! \033[0m"
else
echo -e "\033[31m 本地yum源已配置完成 \033[0m"
fi
fi

continue

#################安装nginx服务###############
read -p "请输入本机IP地址(例:192.168.10.10):"  ip
read -p "设置nginx域名(例:www.abc.com):"  www

#1.关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

#2.安装依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

#3.创建运行用户
useradd -M -s /sbin/nologin nginx

#4.编译安装Nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz || tar zxvf nginx-1.12.2.tar.gz
cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

#5.编译安装
make -j 2 && make install


#6.设置软连接让系统识别nginx的操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

#7.检查配置文件是否配置正确

nginx -t && netstat -natp | grep :80
if [ $? -eq 0 ];then

 echo "配置文件正确,正在启动服务"
#启动服务
  nginx

#重启服务
  else
    pid=`cat /usr/local/nginx/logs/nginx.pid` &> /dev/null
    kill  $pid
    nginx  
    echo "端口已被占用,正在重启服务" 
fi


#7.添加 Nginx 系统服务

echo 
'[Unit]
Description=The Apache HTTP Server			
After=network.target						
[Service]
Type=forking							
PIDFile=/usr/local/httpd/logs/httpd.pid		
ExecStart=/usr/local/bin/apachectl $OPTIONS	
ExecReload=/bin/kill -HUP $MAINPID			
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/httpd.service

#8.加权限
chmod 754 /lib/systemd/system/nginx.service


netstat -anpt | grep :80

#9.添加到系统服务

cd /etc/init.d/
echo '#!/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=$(basename $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' > /etc/init.d/nginx 
cd /etc/init.d
chmod 755 /etc/init.d/nginx
chkconfig --add nginx 

#10.设置服务启动
systemctl restart nginx.service
systemctl enable nginx.service

#11.浏览器访问
echo "$ip $www" >> /etc/hosts
echo -e "\033[31m Nginx安装完成!\033[0m"
echo -e "\033[31m 可访问Nginx页面为:$ip \033[0m"
echo -e "\033[31m 可访问Nginx页面为:$www \033[0m"

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值