运维必看--Shell脚本实现LAMP自动部署!

简介: 运维必看--Shell脚本实现LAMP自动部署!

实验环境:Centos7.7

实验思路: 下载源码包,编译安装,讲代码封装到一个函数里,通过read -p实现函数调用


1.apache函数


#! /bin/bash
# by caq 0530
# auto_install_lamp
#链接:https://pan.baidu.com/s/1ZOaqgznBZsTStWaaM7S2Ag 
#提取码:wjrz
#方便下载,当然也可以在官网下载
apache()
{
H_FILES=httpd-2.4.43.tar.gz
H_PREFIX=/usr/local/httpd/
a=apr-1.7.0.tar.gz
b=apr-util-1.6.1.tar.gz
c=apr-1.7.0
d=apr-util-1.6.1
systemctl stop firewalld
setenforce 0
yum -y install \
gcc \
gcc-c++ \
expat-devel \
perl \
net-tools \
vim
if [[ ! -f $a ]];then
        echo "Please Download $a"
elif [[ ! -f $b ]];then
        echo "Please Download $b"
elif [[ ! -f $H_FILES ]];then
        echo "Please Download $H_FILES"
else
        tar xvf $a
        tar xvf $b
        tar xvf $H_FILES
fi
mv $c $H_FILES_DIR/srclib/apr
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,please check $H_FILE_DIR!"
fi
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
sed '/bash/a # chkconfig: 35 85 21' /etc/init.d/httpd
chkconfig --add httpd
systemctl daemon-reload
sed -i 's/#ServerName www.example.com:80/ServerName www.example.com/g' /etc/httpd.conf
ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
ln -s /usr/local/httpd/bin/* /usr/local/bin/
systemctl start httpd
systemctl enable httpd
echo '--------------------------------'
echo "apache install sueccessful"             
echo '-------------------------------'
}

装完apachel后可以访问测试页面,看是否安装成功

image.png

(注意,网页默认显示内容为:/usr/local/httpd/htdocs/index.html)


2.Mysql函数


mysql()
{
M_FILE=mysql-5.5.62.tar.gz
M_DIR=mysql-5.5.62
C_FILE=cmake-2.8.11.2.tar.gz
C_DIR=cmake-2.8.11.2
# install cmake
# edit install
cd
cd $C_DIR
./configure
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,please $C_DIR!"
fi
cd
# config mysql
mkdir -p /usr/local/mysql/data
groupadd mysql
useradd -g mysql -s /usr/sbin/nologin mysql
if [[ ! -f $M_FILE ]];then
        echo "Please Download $M_FILE"
else
        tar zxvf $M_FILE
fi
cd $M_DIR
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,please CMAKE!"
fi
rm -rf /etc/my.cnf
cp support-files/my-medium.cnf /etc/my.cnf
#set authority
chmod +x /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data
#config auto start
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
cat << EOF >> /root/qwe.txt
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
user = mysql
tmpdir = /tmp
EOF
sed -i '/\[mysqld\]/r /root/qqqq.txt' /etc/my.cnf
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
echo "PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
source  /etc/profile
service mysqld start
service mysqld enable
mysqladmin -u root password "123"
echo "-------------------------"
echo "mysql install successful!"
echo "-------------------------"
}


Mysql安装成功可以查看能不能登录mysql,登录成功即安装成功


mysql的3306端口已经打开

[root@a ~]# netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::* 

mysql登录测试,成功!

[root@a ~]# mysql -uroot -p123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.62-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

3.php函数


php()
{
P_FILES=php-5.5.14.tar.gz
P_DIR=php-5.5.14
cd
yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
if [[ ! -f $P_FILES ]];then
    echo "Please install $P_FILES"
else
        tar zxvf $P_FILES
fi
cd $P_DIR
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,Please $P_DIR!"
fi
cp php.ini-development /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini
echo "AddType application/x-httpd-php .php" >> /etc/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd.conf
sed  -i 's/index.html/index\.php index\.html/' /etc/httpd.conf
rm -f /usr/local/httpd/htdocs/index.html
cat << EOF >> /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
EOF
systemctl restart httpd
systemctl enable httpd
echo "------------------------"
echo "PHP install successful!"
echo "------------------------"
}

4.调用函数


read -p "Please Input Your Install [1.apache 2.mysql 3.php 4.exit]" caq
if [[ $caq -eq 1 ]];then
        apache
elif [[ $caq -eq 2 ]];then
        mysql
elif [[ $caq -eq 3 ]];then
        php
elif [[ $caq -eq 4 ]];then
        exit 0
else
        echo "error,input number[1、2、3、4]"
fi

5.源码


#! /bin/bash
# by caq 0530
# auto_install_lamp
#链接:https://pan.baidu.com/s/1ZOaqgznBZsTStWaaM7S2Ag 
#提取码:wjrz
#方便下载,当然也可以在官网下载
apache()
{
H_FILES=httpd-2.4.43.tar.gz
H_PREFIX=/usr/local/httpd/
a=apr-1.7.0.tar.gz
b=apr-util-1.6.1.tar.gz
c=apr-1.7.0
d=apr-util-1.6.1
systemctl stop firewalld
setenforce 0
yum -y install \
gcc \
gcc-c++ \
expat-devel \
perl \
net-tools \
vim
if [[ ! -f $a ]];then
        echo "Please Download $a"
elif [[ ! -f $b ]];then
        echo "Please Download $b"
elif [[ ! -f $H_FILES ]];then
        echo "Please Download $H_FILES"
else
        tar xvf $a
        tar xvf $b
        tar xvf $H_FILES
fi
mv $c $H_FILES_DIR/srclib/apr
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,please check $H_FILE_DIR!"
fi
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
sed '/bash/a # chkconfig: 35 85 21' /etc/init.d/httpd
chkconfig --add httpd
systemctl daemon-reload
ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
ln -s /usr/local/httpd/bin/* /usr/local/bin/
systemctl start httpd
systemctl enable httpd
echo '--------------------------------'
echo "apache install sueccessful"             
echo '-------------------------------'
}
mysql()
{
M_FILE=mysql-5.5.62.tar.gz
M_DIR=mysql-5.5.62
C_FILE=cmake-2.8.11.2.tar.gz
C_DIR=cmake-2.8.11.2
# install cmake
# edit install
cd
cd $C_DIR
./configure
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,please $C_DIR!"
fi
cd
# config mysql
mkdir -p /usr/local/mysql/data
groupadd mysql
useradd -g mysql -s /usr/sbin/nologin mysql
if [[ ! -f $M_FILE ]];then
        echo "Please Download $M_FILE"
else
        tar zxvf $M_FILE
fi
cd $M_DIR
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,please CMAKE!"
rm -rf /etc/my.cnf
cp support-files/my-medium.cnf /etc/my.cnf
#set authority
chmod +x /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data
#config auto start
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
cat << EOF >> /root/qwe.txt
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
user = mysql
tmpdir = /tmp
EOF
sed -i '/\[mysqld\]/r /root/qqqq.txt' /etc/my.cnf
echo "PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
source  /etc/profile
service mysqld start
service mysqld enable
mysqladmin -u root password "123"
echo "-------------------------"
echo "mysql install successful!"
echo "-------------------------"
}
php()
{
P_FILES=php-5.5.14.tar.gz
P_DIR=php-5.5.14
cd
yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
if [[ ! -f $P_FILES ]];then
        tar zxvf $P_FILES
fi
cd $P_DIR
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
if [[ $? -eq 0 ]];then
        make && make install
else
        echo "error,Please $P_DIR!"
fi
cp php.ini-development /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini
echo "AddType application/x-httpd-php .php" >> /etc/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd.conf
sed  -i 's/index.html/index\.php index\.html/' /etc/httpd.conf
rm -f /usr/local/httpd/htdocs/index.html
cat << EOF >> /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
EOF
systemctl restart httpd
systemctl enable httpd
echo "------------------------"
echo "PHP install successful!"
echo "------------------------"
}
read -p "Please Input Your Install [1.apache 2.mysql 3.php 4.exit]" caq
if [[ $caq -eq 1 ]];then
        apache
elif [[ $caq -eq 2 ]];then
        mysql
elif [[ $caq -eq 3 ]];then
        php
elif [[ $caq -eq 4 ]];then
        exit 0
else
        echo "error,input number[1、2、3、4]"
fi

6.成果图


image.png

撒花完结^^)


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
10月前
|
存储 人工智能 运维
别再靠脚本“救火”了!让智能数据治理接管你的运维世界
别再靠脚本“救火”了!让智能数据治理接管你的运维世界
438 14
|
11月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
972 2
|
11月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
907 9
|
11月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1329 1
|
机器学习/深度学习 消息中间件 人工智能
别只会写脚本了!看看机器学习是怎么帮运维“摸鱼”的
别只会写脚本了!看看机器学习是怎么帮运维“摸鱼”的
313 13
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
432 60
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
783 17
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
709 3
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
440 4