Centos7安装nginx(三)之shell脚本自动化安装nginx

6 篇文章 0 订阅
6 篇文章 0 订阅

一、简介

  • 需求:本人最近在尝试编写k8s自动部署脚本,在有空之余抱着学习的心态完成了nginx的自动部署脚本的编写。希望可以对自动化运维、想要学习shell的小伙伴们提供帮助,也希望大神们可以指出本人渣渣代码中不足。
  • 内容:旨在熟悉shell语法、centos环境配置和nginx的安装流程
  • 环境:
    • 虚拟机+centos7
    • 虚拟机+docker+centos7
  • 目的:为测试shell脚本提供低开销的环境

二、shell脚本介绍

脚本的任务是模仿本人的文章Centos7安装nginx(一)中的步骤,自动的安装nginx。

  1. 脚本内容如下:
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget make
wget http://nginx.org/download/nginx-1.14.0.tar.gz && tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

cfg="--prefix=/usr --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/usr/local/run/nginx/nginx.pid --lock-path=/usr/local/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tem/nginx/client --http-proxy-temp-path=/var/tem/nginx/proxy --http-fastcgi-temp-path=/var/tem/nginx/fcgi --with-http_stub_status_module"

./configure $cfg
make&&make install

sed -i '9apid    /usr/local/run/nginx/nginx.pid;' /usr/local/nginx/nginx.conf
touch /usr/lib/systemd/system/nginx.service

cat <<EOF> /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/run/nginx/nginx.pid
ExecStartPre=/usr/local/sbin/nginx -t -c /usr/local/nginx/nginx.conf
ExecStart=/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/usr/local/sbin/nginx -s reload
ExecStop=/usr/local/sbin/nginx -s stop
ExecQuit=/usr/local/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

groupadd nginx
useradd nginx  -g nginx -p nginx

mkdir /var/tem
mkdir /var/tem/nginx

systemctl daemon-reload
systemctl start nginx
  1. 脚本优化
#!/bin/bash
#nginx install script
#nginx version 1.14.0
#OS centos7
#author: urmsone
#createtime: 2018.11.30

add_user_and_group() {
        echo "add_user and add group start..."
        user="nginx"
        group="nginx"
        groupadd $group

        #create user if not exists
        egrep "^$user" /etc/passwd
        if [ $? -ne 0 ]; then
                # ll /home |grep ${user}
                find /home | grep test_user >&/dev/null
                if [ $? -ne 0 ]; then
                        useradd $user -g $group -p $user
                        echo "user add ${user}"
                else
                        echo "/home/${user} is existed!User creation failed!Please rm the file and try again"
                fi
        fi
        echo "add_user and add group finished..."
}

add_tem_file() {
        echo "add_tem_file start..."
        folder="/var/tem"
        if [[ ! -d "${folder}" ]]; then
                echo "folder:/var/tem/nginx is not existed!"
                mkdir /var/tem
                mkdir /var/tem/nginx
                echo "folder:/var/tem/nginx is created!"
        fi
        echo "add_tem_file finished..."
}

init_env() {
        echo "init_env start..."
        yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget make
        # groupadd nginx
        # useradd nginx -g nginx -p nginx
        add_user_and_group
        # mkdir /var/tem
        # kdir /var/tem/nginx
        add_tem_file
        echo "init_env finished..."
}

install_nginx() {
        echo "install_nginx start..."
        wget http://nginx.org/download/nginx-1.14.0.tar.gz && tar -zxvf nginx-1.14.0.tar.gz
        cd nginx-1.14.0
        cfg="--prefix=/usr --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/usr/local/run/nginx/nginx.pid --lock-path=/usr/local/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tem/nginx/client --http-proxy-temp-path=/var/tem/nginx/proxy --http-fastcgi-temp-path=/var/tem/nginx/fcgi --with-http_stub_status_module"
        ./configure $cfg
        make && make install
        echo "install_nginx finished..."
}

conf_systemd() {
        echo "conf_systemd start..."
        sed -i '9apid    /usr/local/run/nginx/nginx.pid;' /usr/local/nginx/nginx.conf
        touch /usr/lib/systemd/system/nginx.service

        cat <<EOF >/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/run/nginx/nginx.pid
ExecStartPre=/usr/local/sbin/nginx -t -c /usr/local/nginx/nginx.conf
ExecStart=/usr/local/sbin/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/usr/local/sbin/nginx -s reload
ExecStop=/usr/local/sbin/nginx -s stop
ExecQuit=/usr/local/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

        echo "conf_systemd finished..."
}

main() {
        init_env
        install_nginx
        conf_systemd
        systemctl daemon-reload
        systemctl start nginx
}
main
  1. docker容器中使用脚本安装nginx并测试安装情况
    • 在虚拟机上启动/进入容器
      • docker run -d --name nginx2 --privileged=true -p 9090:80 docker.io/centos:7 /usr/sbin/init
      • docker exec -it nginx2 /bin/bash
        启动并进入容器
    • 将脚本复制进容器(建议使用挂载的形式共享脚本,不用每次新开容器都执行复制工作)
      • vi nginx.sh
      • 把上面优化版本的脚本复制进nginx.sh
      • sh nginx.sh(执行脚本)
        运行脚本
        在这里插入图片描述
      • netstat -apn |grep 9090(在虚拟机上查看端口状态)
        查看端口状态
      • docker ps(查看docker运行容器状态)
        docker运行容器的状态
      • ip addr(查看虚拟机的ip)
      • 在宿主机中通过虚拟机的ip+port访问
        测试nginx

三、总结

本文主要介绍使用shell脚本来自动化安装nginx,即可以给正在学习shell脚本的小伙伴提供一个应用demo,也可以熟悉nginx的安装过程。文章可能在编辑过程中,由于个人疏忽、不同版本markdown解析器不兼容等原因导致字符书写错误,导致安装失败。本文属于原创,若有引用请注明出处。若有疑问或错误,欢迎各位指出,可以评论或者跟本人联系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值