Shell脚本完成web服务器的建设

一,要求

1,安装httpd软件

2,定义防火墙规则

3,设置selinux为强制模式

4,web服务器的端口为8080

5,web服务器的内容目录为  /www/8080

6,所有操作开机有效

7,所有操作必须进行判断

8,在合适的实际输出合适的信息,如果有日志就更好了

二,具体实施

vim_repo() {
touch /etc/yum.repos.d/rpm.repo
echo -e "[baseos]\nname=baseos\nbaseurl=/mnt/BaseOS\ngpgcheck=0\n[appstream]\nname=appstrean\nbaseurl=/mnt/AppStream\ngpgcheck=0" > /etc/yum.repos.d/rpm.repo
}

repod() {
    yum=`yum repolist all | grep enabled | wc -l`
    if [ $yum -eq 0 ]
    then
        echo "没有仓库,mount"
        vim_repo
        mount /dev/sr0 /mnt
    else
        echo "有仓库,并挂载"
        mount /dev0 /mnt
    fi
}
httpd_whether_installed() {
    httpd_server=`rpm -qa | grep httpd | wc -l`
    if [ $httpd_server -eq 0 ];then
        echo "Httpd is not exists and insatll"
        yum install httpd
    else
        echo "Httpd is exists"
    fi
}

httpd_whether_enabled () {
    httpd_server=`netstat -lntup | grep -w 80 | wc -l`
    if [ $httpd_server -eq 0 ];then
        echo "httpd is not active and start httpd"
        systemctl start httpd
    else
        echo "httpd is active and make httpd enable"
        systemctl enable httpd
    fi
}

open_firewall_server() {
    firewall_listall=`firewall-cmd --list-all | grep http | wc -l`
    if [ $firewall_listall -eq 0 ];then
        echo "未开放http服务,并开始放行http服务,并重启"
        firewall-cmd --add-service=http --permanent
        firewall-cmd --reload
    else
        echo "已放行http服务"
    fi
}

set_selinux() {
while :
do
    getenforce=`getenforce`
    case $getenforce in
    "Enforcing")
        echo "SElinux为强制模式"
        break
        ;;
    "Permissive")
        echo "SElinux为宽容模式,正在换强制模式"
        setenforce 1
        getenforce
        break
        ;;
    *)
        echo "SElinux关闭"
        break
    esac
done
}

open_firewall_port() {
    firewall_port=`firewall-cmd --list-ports | wc -l`
    if [ $firewall_port -eq 0 ];then
        echo "未开放8080端口,正在开启并重启防火墙"
        firewall-cmd --zone=public --add-port=8080/tcp
        firewall-cmd --reload
    else
        echo "8080端口已经被放行!"
    fi
}

httpd_conf() {
    echo "正在配置httpd服务配置文件"
    echo -e " <directory /www>\nallowoverride\nrequire all granted\n</directory>\nlisten 8080\n<virtualhost 0.0.0.0:8080>\ndocumentroot /www/8080\nservername 192.168.80.131\n</virtualhost>" >/etc/httpd/conf.d/host.conf
    echo "启动httpd并检查状态"
    systemctl restart httpd
}

semanage_port() {
    #取出80端口的类型
    se_port=`semanage port -l | grep -w 80 | tr -s " " | cut -d " " -f1`
    echo "正在为selinux添加新端口"
    #更改 SELinux 类型端口 8080 使其与端口 80 匹配
    semanage port -a -t $se_port -p tcp 8080
    echo "添加端口完成,并查看修改对象安全上下文"
    se_content=`ls /var/www/html -Z | tr -s " " | cut -d ":" -f3`
    chcon -t $se_content /www/8080/index.html
    echo "修改成功,并检查/www/8080的安全上下文"
    ls /www/8080/index.html -lZ
    echo "检查完毕,正在重启httpd服务并测试服务能否访问"
    systemctl restart httpd
    wget 192.168.80.131:8080
}

repod
httpd_whether_installed
httpd_whether_enabled
open_firewall_server
set_selinux
open_firewall_port
httpd_conf
semanage_port

三,结果展示

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路喝狗狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值