👹 关于作者
大家好,我是秋意零。
😈 CSDN作者主页
- 😎 博客主页
👿 简介
- 👻 普通本科在读;
- 在校期间参与众多计算机相关比赛,如:🌟 “省赛”、“国赛” ,斩获多项奖项荣誉证书;
- 🔥 各个平台,秋意临 账号创作者;
- 🔥 云社区 创建者;
记得收藏+关注免得迷了路。
欢迎加入云社区
一、前言🙊
今天给大家介绍一个 Web 应用程序平台,LAMP 架构,这个架构放在今天为止也是非常热门的架构。
这里我们将使用 shell 脚本实现具有以下三大功能: 👖
- 一键搭建 LAMP 环境
- 一键删除 LAMP 环境
- exit 退出脚本
shell 脚本细节: 👖
- 1、基础环境配置(前提条件)
- 2、安装 \ 卸载 apache (web 服务器)
- 3、安装 \ 卸载 mariadb (数据库)
- 4、安装 \ 卸载 php (计算机编程语言)
- 5、阶段成功信息写入 log 日志文件中( lamp_success.log、lamp_error.log )
ps:php 能直接嵌入 html 中,与 html 混编,目的主要是 web 开发人员快速编写动态生成的 web 页面。
二、脚本中方法说明 👑
这个脚本我们将创建7个方法
1、prerequisite(基础环境配置)
- 关闭和永久关闭防火墙、
- 关闭 SELINUX、
- 配置阿里 yum 源
2、install_apache(安装 apache 服务器)
- 判断 apache 是否安装
- 安装 apache
- 启动 apache
- 检测 apache 服务端口和服务状态
- curl 访问 apache
3、install_mariadb(安装 maraidb 数据库)
- 判断 maraidb 是否安装
- 安装 mariadb
- 启动 mariadb
- 检测 mariadb 服务端口和服务状态
- 初始化数据库
- 测试 mariadb 是否能使用
4、install_php(安装 php 环境)
- 判断 php 是否安装
- 安装 php
- 重新启动 httpd
- 判断 info.php 文件是否存在
- curl 访问 php
5、remove_lamp(删除 LAMP 环境)
- remove apache:判断是否安装 apache server、删除 http
- remove mariadb:判断是否安装 maraidb、删除 mariadb
- remove php:判断 php 是否安装、删除 php
6、lamp_log(创建日志文件)
- 判断 lamp 目录是否存在、创建 lamp 目录
7、main(脚本入口)
- 脚本使用说明
- 判断是否输入的是数字
- 判断用户输入的值是否匹配,安装,移除
- 依次调用上面的方法
三、LAMP shell源码 ✈
将阶段成功信息写入 log 日志文件中
- 1:代表标准输出
- 2:代表错误输出
下列方法是将 1 标准输出写入一个文件,2 错误输出写入一个文件 😾
1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log
LAMP shell源码
#!/bin/bash #################### ##### 前提条件 ##### #################### prerequisite(){ echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Start init config ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log # 防火墙状态、SELINUX 状态 FIREWALLD_STATUS=`systemctl status firewalld | grep loaded | wc -l` SELINUX_STATUS=$(getenforce) # 关闭和永久关闭防火墙 if [ ${FIREWALLD_STATUS} -ne 1 ] then systemctl stop firewalld && systemctl disable firewalld if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Firewalld successfully ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Firewalld close failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 1 fi fi # 关闭 SELINUX if [ ${SELINUX_STATUS} -ne Disabled ] then setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Selinux successfully ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Selinux close failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 2 fi fi # 配置阿里 yum 源 if [ -f /etc/yum.repos.d/CentOS-Base.repo ] && [ -f /etc/yum.repos.d/epel.repo ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Centos_7 yum config successfully ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Config centos_7 yum string..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &&\ curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Yum config successfully ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Yum config failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 3 fi # 清除缓存 & 建立缓存 yum clean all ; yum makecache fi echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Init config successfully ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log } ########################## ##### Install Apache ##### ########################## install_apache(){ # Apache 版本控制 APACHE_VERSION=2.4.6 # 判断 apache 是否安装 APACHE_INSTALLED=$(rpm -aq | grep httpd-${APACHE_VERSION} | wc -l) if [ ${APACHE_INSTALLED} -ne 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Start install apache server ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log # install apache yum install -y httpd-${APACHE_VERSION} # 判断是否安装成功,安装成功就启动,失败就退出 if [ $? -eq 0 ] then sleep 3 echo "`date '+%Y-%m-%d %H:%M:%S'` Apache server install successfully ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log # 启动 apache 并设置开机自启 systemctl enable httpd ; systemctl start httpd # 判断是否启动成功 if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Start apache server successfully ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Start apache server failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 4 fi else echo "`date '+%Y-%m-%d %H:%M:%S'` Install apache server failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 5 fi # 检测 apache 服务端口和服务状态 sleep 3 APACHE_STATUS=$(systemctl status httpd | grep running | wc -l) APACHE_PORT=$(netstat -ntplu | grep 80 | wc -l) if [ ${APACHE_PORT} -eq 1 ] && [ ${APACHE_STATUS} -eq 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Apache port exists or status ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Apache port no eists or status no ok..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 6 fi # 判断 index.html 文件是否存在 if [ ! -f /var/www/html/index.html ] then echo hello,world > /var/www/html/index.html sleep 2 fi # curl 访问 apache curl -I 127.0.0.1 | grep "200 OK" if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Apache server 访问 successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Apache server 访问 failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 7 fi echo "`date '+%Y-%m-%d %H:%M:%S'` ============== apache server successfully ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` ============== apache server installed ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log fi } ########################### ##### Install Mariadb ##### ########################## install_mariadb(){ # mariadb 版本控制 MARIADB_VERSION=5.5.68 # mariadb password MARIADB_PASSWORD=000000 # 判断 maraidb 是否安装 MARIADB_INSTALLED=$(rpm -qa | grep mariadb-${MARIADB_VERSION} | wc -l) MARIADB_SERVER_INSTALLED=$(rpm -qa | grep mariadb-server-${MARIADB_VERSION} | wc -l) #MARIADB_LIBS_INSTALLD=$(rpm -qa | grep mariadb-libs-${MARIADB_VERSION} | wc -l) MARIADB_DEVEL_INSTALLED=$(rpm -qa | grep mariadb-devel-${MARIADB_VERSION} | wc -l) if [ ${MARIADB_INSTALLED} -ne 1 ] && [ ${MARIADB_SERVER_INSTALLED} -ne 1 ] && [ ${MARIADB_DEVEL_INSTALLED} -ne 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Start install mariadb server ==============" # 安装 mariadb yum install mariadb-${MARIADB_VERSION} mariadb-server-${MARIADB_VERSION} mariadb-libs-${MARIADB_VERSION} mariadb-devel-${MARIADB_VERSION} expect -y if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Install mariadb server successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log sleep 3 # 启动 mariadb systemctl enable mariadb ; systemctl start mariadb if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Start mariadb server successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Start mariadb server failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 8 fi # 检测 mariadb 服务端口和服务状态 MARIADB_PORT=$(netstat -ntplu | grep 3306 | wc -l) MARIADB_STATUS=$(systemctl status mariadb | grep running | wc -l) if [ ${MARIADB_PORT} -eq 1 ] && [ ${MARIADB_STATUS} -eq 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Mariadb server port exists or status ok" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Mpache server port no exists or status no ok... " 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 9 fi # 初始化数据库 expect -c " spawn mysql_secure_installation expect \"Enter current password for root (enter for none):\" send \"\r\" expect \"Set root password?\" send \"y\r\" expect \"New password:\" send \"${MARIADB_PASSWORD}\r\" expect \"Re-enter new password:\" send \"${MARIADB_PASSWORD}\r\" expect \"Remove anonymous users?\" send \"y\r\" expect \"Disallow root login remotely?\" send \"n\r\" expect \"Remove test database and access to it?\" send \"y\r\" expect \"Reload privilege tables now?\" send \"y\r\" " sleep 2 # 测试 mariadb 是否能使用 mysql -uroot -p${MARIADB_PASSWORD} -e "show databases;" if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Mariadb server init successfully ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Mariadb server init failed... ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 10 fi echo "`date '+%Y-%m-%d %H:%M:%S'` ============== over install mariadb server ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Install mariadb failed" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 10 fi else echo "`date '+%Y-%m-%d %H:%M:%S'` ============== mariadb server installed ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log fi } ######################### ##### Install php ####### ######################### install_php(){ # PHP 版本控制 PHP_VERSION=5.4.16 # 判断 php 是否安装 PHP_INSTALLED=$(rpm -qa |grep "php-5.4.16" | wc -l) if [ ${PHP_INSTALLED} -ne 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Start install php ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log # Install php yum install php-${PHP_VERSION} php-devel-${PHP_VERSION} php-mysql-${PHP_VERSION} php-gd-${PHP_VERSION} php-imap-${PHP_VERSION} php-ldap-${PHP_VERSION} php-odbc-${PHP_VERSION} php-pear php-xml-${PHP_VERSION} php-xmlrpc-${PHP_VERSION} -y if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Install php server successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log sleep 3 # 重新启动 httpd systemctl restart httpd if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Restart apache server successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Restart apache server failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 11 fi # 判断 info.php 文件是否存在 if [ ! -f /var/www/html/info.php ] then cat > /var/www/html/info.php << EOF <?php phpinfo(); ?> EOF sleep 2 fi # curl 访问 php curl -I 127.0.0.1/info.php | grep "200 OK" if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Php server 访问 successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Php server 访问 failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 12 fi echo "`date '+%Y-%m-%d %H:%M:%S'` ============== over install php ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Install php server failed" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 13 fi else echo "`date '+%Y-%m-%d %H:%M:%S'` ============== php server installed ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log fi } remove_lamp(){ ####################### #### remove apache #### ####################### # 首先判断是否安装 apache server APACHE_VERSION=2.4.6 APACHE_INSTALLED=$(rpm -aq | grep httpd-${APACHE_VERSION} | wc -l) if [ ${APACHE_INSTALLED} -eq 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Start remove apache server" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log # 删除http REMOVE_HTTPD=`rpm -qa |grep http` yum remove -y $REMOVE_HTTPD && rm -rf /var/www && rm -rf /var/log/httpd if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Apache server remove successfully ok!" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Apache server remove failed" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 1 fi else echo "`date '+%Y-%m-%d %H:%M:%S'` no install apache" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log fi ######################## #### remove mariadb #### ######################## # maraidb 是否安装 MARIADB_VERSION=5.5.68 MARIADB_INSTALLED=$(rpm -qa | grep mariadb-${MARIADB_VERSION} | wc -l) MARIADB_SERVER_INSTALLED=$(rpm -qa | grep mariadb-server-${MARIADB_VERSION} | wc -l) #MARIADB_LIBS_INSTALLD=$(rpm -qa | grep mariadb-libs-${MARIADB_VERSION} | wc -l) MARIADB_DEVEL_INSTALLED=$(rpm -qa | grep mariadb-devel-${MARIADB_VERSION} | wc -l) #判断是否安装 maraidb if [ ${MARIADB_INSTALLED} -eq 1 ] && [ ${MARIADB_SERVER_INSTALLED} -eq 1 ] && [ ${MARIADB_DEVEL_INSTALLED} -eq 1 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Start remove mariadb server" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log yum remove mariadb-${MARIADB_VERSION} mariadb-server-${MARIADB_VERSION} mariadb-libs-${MARIADB_VERSION} mariadb-devel-${MARIADB_VERSION} expect -y && rm -rf /etc/my.cnf* && rm -rf /var/lib/mysql if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` Remove mariadb server successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` Remove mariadb server failed" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 1 fi else echo "`date '+%Y-%m-%d %H:%M:%S'` no install mariadb" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log fi #################### #### remove php #### #################### PHP_VERSION=5.4.16 # 判断 php 是否安装 #PHP_INSTALLED=$(rpm -qa |grep "php-5.4.16" | wc -l) php -v if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` ============== Start remove php ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log yum remove php-${PHP_VERSION} php-devel-${PHP_VERSION} php-mysql-${PHP_VERSION} php-gd-${PHP_VERSION} php-imap-${PHP_VERSION} php-ldap-${PHP_VERSION} php-odbc-${PHP_VERSION} php-pear php-xml-${PHP_VERSION} php-xmlrpc-${PHP_VERSION} php-common-${PHP_VERSION} -y if [ $? -eq 0 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` remove php server successfully" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log else echo "`date '+%Y-%m-%d %H:%M:%S'` remove php server failed..." 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 11 fi else echo "`date '+%Y-%m-%d %H:%M:%S'` no install php" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log fi } # lamp log create lamp_log(){ # 判断 lamp 目录是否存在 if [ ! -e /var/log/lamp ] then echo "`date '+%Y-%m-%d %H:%M:%S'` create dir_lamp and file_lamp " 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log mkdir /var/log/lamp && touch /var/log/lamp/lamp_success.log && touch /var/log/lamp/lamp_error.log fi if [ $? -ne 0 ] then echo "log file create failed" exit 2 fi } # 主方法,脚本入口 main(){ #脚本使用说明 echo "=======请选择=======" echo "1.install LAMP 【1】" echo "2.remove LAMP 【2】" echo "3.exit 【3】" read -p "输入对应数字即可:" LAMP # 判断是否输入的是数字 expr ${LAMP} + 1 if [ $? -eq 0 ] then echo "你输入的数字是:${LAMP}" else echo "你输入的数字是:${LAMP},输入不合法,请输入[1|2|3]" exit 2 fi # 判断用户输入的值是否匹配,安装,移除 if [ ${LAMP} -eq 1 ] then # 调用方法 lamp_log sleep 2 prerequisite sleep 3 install_apache sleep 3 install_mariadb sleep 3 install_php sleep 1 echo "`date '+%Y-%m-%d %H:%M:%S'` ============== LAMP install successfully! ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log elif [ ${LAMP} -eq 2 ] then lamp_log remove_lamp echo "`date '+%Y-%m-%d %H:%M:%S'` ============== LAMP remove successfully! ==============" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log elif [ ${LAMP} -eq 3 ] then echo "`date '+%Y-%m-%d %H:%M:%S'` exit install lamp" 1>>/var/log/lamp/lamp_success.log 2>>/var/log/lamp/lamp_error.log exit 1 else echo "输入不合法,请输入[1|2|3]" fi } # 调用主方法 main
✊ 最后
👏 我是秋意临,欢迎大家一键三连、加入云社区
👋 我们下期再见(⊙o⊙)!!!