脚本环境
1.linux centos 7 系统
2.光盘镜像文件已挂载
3.系统可连接外网
4.软件包下载链接
lamp 链接:https://pan.baidu.com/s/1x6PTj_aUN2P1DfGGOKe90g
提取码:3w8g
lnmp 链接:https://pan.baidu.com/s/1lr4x1yUFVtDsUWDYJePIyw
提取码:8427
实验步骤
1.上传软件包到Linux系统/opt目录下
方法一:挂载 方法二:通过Xftp软件上传
2.编写脚本package.sh
vim package.sh
#!/bin/bash
# this is down package and tar shell!
#
cd /opt
echo -e "\e\t\t\t[34m mysql,php package downing 。。。\e[0m"
sleep 1
#下载mysql,php软件包(此处注释了,去#开启)
#wget http://dev.mysql.com/get/Downloads/MySQL-5.5.24/mysql-5.5.24.tar.gz &&wget http://php.net/distributions/php-5.6.11.tar.gz && echo "mysql,php package down finish!"
#下载编译工具包
yum -y install gcc gcc-c++ make cmake pcre perl bison gd libpng libpng-devel libaio-devel ncurses-devel libxml2-devel zlib-devel pcre-devel expat-devel openssl-devel libjpeg-devel fontconfig-devel bzip2-devel
bash=`rpm -q gcc gcc-c++ make cmake pcre perl bison gd libpng libaio-devel libpng-devel ncurses-devel libxml2-devel zlib-devel pcre-devel expat-devel openssl-devel | grep "未安装" | wc -l`
#判断编译工具是否下载完成
if [ $bash -eq 0 ];then
echo -e "\033\t\t\t[32m make tool down finish!\033[0m"
else
exit 0 && echo echo -e "\033\t\t\t[31m make tool down fail,check ! \033[0m"
fi
#
#人机交互输入脚本名称
read -p "please input script name (la/nmp): " install
echo -e "\e\t\t\t[32m installing $install ... \e[0m"
#延迟1秒,让用户看到自己输入的脚本
sleep 1
#
case $install in
#lamp软件包下载和解压
lamp)
#下载httpd相关软件包(此处注释了,去#开启)
#wget http://archive.apache.org/dist/httpd/httpd-2.4.29.tar.gz &&wget http://archive.apache.org/dist/apr/apr-1.6.2.tar.gz &&wget http://archive.apache.org/dist/apr/apr-util-1.6.0.tar.gz
#解压所有.gz结尾的软件包
find -maxdepth 1 -name "*.gz"|xargs -i tar xvzf {}
#判断软件包解压状态,输出相关信息
[[ $? -eq 0 ]] && echo -e "\033[32m lamp package tar finish!\033[0m" || exit 0 && echo -e "\033[31m lamp down fail,check!\033[0m"
mv apr-1.6.2 httpd-2.4.29/srclib/apr && mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
;;
#lnmp软件包下载和解压
lnmp)
#下载nginx相关软件包(此处注释了,去#开启)
#wget http://nginx.org/download/nginx-1.12.0.tar.gz &&
#wget http://downloads.zend.com/guard/7.0.0/zend-loader-php5.5-linux-x86_64_update1.tar.gz
find -maxdepth 1 -name "*.gz" | xargs -i tar xvzf {};
#解压所有.tar.gz 软件包\
[[ $? -eq 0 ]] && echo -e "\033[32m lnmp package tar success!\033[0m" || exit 0 && echo -e "\033[31m lnmp package tar fail,check! \033[0m"
;;
*)
echo "请输入:lamp | lnmp "
exit 0
esac
#将用户输入的脚本名导入e.sh文件
echo "$install" >>/root/e.sh
3.编写脚本lnmp.sh
vim lnmp.sh
#!/bin/bash
#this is auto install lamp or lnmp shell!#apache安装
apache()
{
#apache编译安装
cd /opt/httpd-2.4.29 &&./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &&make && make install &&cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
#
#修改配置文件
#在/etc/init.d/httpd文件中第二行插入2段
sed -i "2i#chkconfig: 35 85 21 \n\
#description: Apache is a World Wide Web server." /etc/init.d/httpd
#将Listen 80 替换成 Listen $ip:80 ,$ip为变量,是本机ip地址(特别注意变量的替换方法)
sed -i "s/^Listen 80/Listen $ip:80/g" /usr/local/httpd/conf/httpd.conf
#
#优化服务
ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd ;ln -s /usr/local/httpd/bin/* /usr/local/bin/
chkconfig --add httpd
systemctl daemon-reload
systemctl start httpd
#
#检查端口和网页测试
port=`netstat -anpt | grep httpd | wc -l`
if [ $? -eq 0 ] && [ $port -ne 0 ]; then
echo -e "\033\t\t\t[44;32m test http://$ip \033[0m"
read -p "input result:(y/n) " u
if [ $u == "y" ]; then
echo -e "\033\t\t\t[32m apache install success!\033[0m"
else
echo -e "\033\t\t\t[5;31m apache install fail!\033[0m"
fi
else
#"-e" 为转义,\033为固定格式(同 \e),"/t"为制表位,同tab键,"5"为闪烁,31m为字体颜色,此处红色,"\033[0m "为固定结束格式
echo -e "\033\t\t\t[5;31m apache install fail,check!!\033[0m"
exit 0
fi
}
#
#安装nginx
nginxt()
{
#编译安装
useradd -M -s /sbin/nologin nginx
cd /opt/nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ;nginx &&nginx -t;
#检查服务启动
stat=`netstat -ntap | grep "80" | wc -l`
if [ $? -eq 0 ]&&[ $stat -ne 0 ] ;then
echo -e "\033\t\t\t[32m nginx install success!!\033[0m"
else
echo -e "\033\t\t\t[5;31m nginx install fail!!\033[0m"
exit 0
fi
}
#
#mysql安装
mysql_con()
{
#编译安装
#判断有无/usr/local/mysql目录,没有就创建
[ ! -d /usr/local/mysql ] && mkdir -p /usr/local/mysql
/usr/sbin/useradd -s /sbin/nologin mysql
cd /opt/mysql-5.5.24
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/mysql \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306
if [ $? -eq 0 ];then
make && make install
#
#修改配置文件
echo "export PATH=$PATH:/usr/local/mysql/bin/ " >> /etc/profile
source /etc/profile && chown -R mysql:mysql /usr/local/mysql &&cd /usr/local/mysql/support-files &&cp my-medium.cnf /etc/my.cnf &&cp mysql.server /etc/init.d/mysqld &&chmod 755 /etc/init.d/mysqld &&chkconfig --add /etc/init.d/mysqld ;
chkconfig mysqld --level 35 on
else
echo -e "\e\t\t\t[5;31m set error,check ![0m"
exit 1
fi
#初始化数据库
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
ln -s /usr/local/mysql/bin/ /usr/local/bin/
#
#搜索basedir=,datadir=,并替换
sed -i 's#^basedir=.$#basedir=/usr/local/mysql#' /etc/init.d/mysqld
sed -i 's#^datadir=.*$#datadir=/home/mysql#' /etc/init.d/mysqld
#mysql启动和检查
systemctl daemon-reload && systemctl start mysqld
port=`netstat -ntap | grep mysql |wc -l`
if [ $? -eq 0 ]&&[ $port -ne 0 ];then
echo -e "\033\t\t\t[32m mysql install success!! \033[0m"
else
echo -e "\033\t\t\t[5;31m mysql install fail,check!! \033[0m"
exit 0
fi
}
#
#apache-php安装
apa_php()
{
#php编译安装
cd /opt/php-5.6.11
./configure \
--prefix=/usr/local/php5 \
--with-gd \
--with-zlib \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring
make && make install &&cp php.ini-development /usr/local/php5/php.ini && ln -s /usr/local/php5/bin/ /usr/local/bin/ ; ln -s /usr/local/php5/sbin/ /usr/local/sbin/ ;
useradd -M -s /sbin/nologin php#检查httpd.conf文件中是否有php5_module,并统计
num=`cat /usr/local/httpd/conf/httpd.conf | grep "php5_module" | wc -l`
#判断是否有php5_module
if [ $num -eq 0 ] ;then
echo -e "\e\t\t\t[5;31m no php5_module, check! \e[0m"
exit 0
else
echo -e "\e\t\t\t[32m php5_module is exist! \e[0m"
fi
#
#修改httpd配置,测试php
#在第258行行尾加index.php
sed -i '258s/$/ index.php/' /usr/local/httpd/conf/httpd.conf
#在383行插入相关文件
sed -i "383iAddType application/x-httpd-php .php\n\
AddType application/x-httpd-php-source .phps" /usr/local/httpd/conf/httpd.conf
systemctl restart httpd
#
#echo文件到index.php里面
echo -e "" >>/usr/local/httpd/htdocs/index.php
#输出用户测试网页链接
echo -e "\e\t\t\t[5;34m test http://$ip/index.php \e[0m"
#php连接mysql测试
read -p "if connect mysql? (y/n)? " an
if [ $an == "y" ];then
#echo文件到index.php中,注意单引号中不识别$ip
echo -e '' >>/usr/local/httpd/htdocs/index1.php
#将localhost替换成 $ip','root' ,注意双引号中不识别$link(要特别注意单引号和双引号之间的区别!)
sed -i "s/localhost/'$ip','root'/g" /usr/local/httpd/htdocs/index1.php
echo -e "\e\t\t\t[32m http://$ip/index.php \e[0m"
else
echo -e "\e\t\t\t[32m lamp install finish!\e[0m"
exit 0
fi
}
#
#nginx-php安装
nginx_php()
{
useradd -M -s /sbin/nologin php
cd /opt/php-5.6.11
./configure \
--prefix=/usr/local/php5 \
--with-gd \
--with-zlib \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring \
--with-jpeg-dir \
--with-openssl \
--disable-ipv6 \
--enable-fpm
make && make install
#安装gd和修改php配置文件
cd /opt/zend-loader-php5.5-linux-x86_64/ && cp ZendGuardLoader.so /usr/local/php5/lib/php
#
echo -e "[Zend Guard Loader]\nzend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so\nzend_loader.enable=1" >>/usr/local/php5/php.ini
cd /usr/local/php5/etc/ && cp php-fpm.conf.default php-fpm.conf
#替换文件
sed -i 's#^user = .$#user = php#' php-fpm.conf
sed -i 's#^group = .$#group = php#' php-fpm.conf
sed -i 's#^pm.max_children = .$#pm.max_children = 50#' php-fpm.conf
sed -i 's#^pm.max_spare_server = .$#pm.max_children = 35#' php-fpm.conf
sed -i 's#^pm.min_spare_server = .$#pm.min_children = 5#' php-fpm.conf
sed -i 's#^pm.start_server = .$#pm.min_children = 20#' php-fpm.conf
#php服务启动和检查
/usr/local/php5/sbin/php-fpm
port=`netstat -ntap | grep ":9000" | wc -l`
if [ $? -eq 0 ]&&[ $port -ne 0 ];then
echo -e "\033\t\t\t[32m php install success! \033[0m"
else
echo -e "\033\t\t\t[5;31m php install fail! \033[0m"
exit 0
fi
}
#
#nginx和php结合
ngin_vim()
{
#修改nginx配置
mkdir -p /var/www/html/btcom
#将45行 ; 替换成index.php; (或者在 ; 前面加 index.php,这个命令不好写)
sed -i '45s/;/ index.php;/g' /usr/local/nginx/conf/nginx.conf
#在64行插入
sed -i "64ilocation ~ .php$ {\n\
root /var/www/html/btcom;\n\
fastcgi_pass 127.0.0.1:9000;\n\
fastcgi_index index.php;\n\
fastcgi_param SCRIPT_FILENAME;\n\
include fastcgi_params;\n\
}" /usr/local/nginx/conf/nginx.conf
#在68行;后插入语句,注意双引号不识别$document_root$fastcgi_script_name,才用单引号重新插入
sed -i '68s/;/ $document_root$fastcgi_script_name;/g' /usr/local/nginx/conf/nginx.conf
echo -e "" >>/var/www/html/btcom/index.php
nginx -s reload
echo -e "\e[32m test http://$ip/index.php \e[0m"
#
#php连接mysql
read -p "php connect mysql (y/n)? " answer
if [ $answer == "y" ];then
#echo文件,注意单,双引号,原因同上
echo -e '' >>/var/www/html/btcom/index1.php
sed -i "s/localhost/'$ip','root'/g" /var/www/html/btcom/index1.php
echo -e "\e\t\t\t[32m test http://$ip/index.php \e[0m"
else
echo -e "\e\t\t\t[32m lnmp install finish!\e[0m"
exit 0
fi
}
#
#执行package.sh脚本
./package.sh
#关闭防火墙,增强性
systemctl stop firewalld.service
setenforce 0
#过滤出本机ip
ip=`ifconfig ens33 | grep "netmask" | awk '{print $2}'`
#查看e.sh,将2个脚本连接起来
num=`cat /root/e.sh`
#2脚本已经建立连接,删除e.sh,防止占用空间
rm -rf e.sh
#lamp脚本安装
if [ $num == "lamp" ]
then
#apache 安装
apache
#mysql 安装
mysql_con
#apache和php连接
apa_php
#lnmp脚本安装
elif [ $num == "lnmp" ]
then
#nginx.mysql,php安装
nginxt &&mysql_con &&nginx_php
#修改nginx配置文件
ngin_vim
else
echo -e "\e\t\t\t[5;31m input error,check! \e[0m"
exit 1
fi
4.赋予脚本权限
chmod +x *.sh
5.执行脚本
./lnmp.sh
6.验证脚本
温馨提示:
1.在上面提供了所有软件安装包,可以直接使用。也可以使用脚本中链接下载,脚本中为节约时间已经注释点了,去#号即可(不推荐联网下载,下载慢还容易出错)!
2.此脚本只是为了给大家学习参考,不建议直接拷贝使用,容易出错。想要的,可以去我上传资料中下载,解压直接使用。(http://down.51cto.com/data/2458725)
3.此脚本不推荐工作时使用,太繁琐了,可作为新手练习写shell 脚本的一个项目。我相信,通过此脚本练习,对大家的shell脚本编写能力会大大提高!
4.为避免一个脚本内容过长,编写起来不方便,采用了2个脚本。package.sh脚本是软件包下载和解压,lnmp.sh脚本是在执行package.sh脚本后安装配置服务。(难点是学习2个脚本的结合点)
5.此脚本重难点:(1) 2个脚本结合部分
(2) 修改配置文件要对sed,awk熟练运用
(3) 所有服务安装与配置之间的检查,顺序整理
(4) 对 echo " , ' , -e, 知识掌握与区别