LAMP 一键自动化安装脚本
通过前面章节对 if 语句和变量的学习,现基于所学知识,编写一键源码安装 LAMP脚本,
编写脚本可以养成先分解脚本的各个功能的习惯,有利于快速写出脚本,写出更高效的
脚本。
一键源码安装 LAMP 脚本,可以拆分为如下功能:
(1) LAMP 打印菜单:
安装 apache WEB 服务器;
安装 Mysql DB 服务器;
安装 PHP 服务器;
整合 LAMP 架构
启动 LAMP 服务;
(2) Apache 服务器安装部署:
Apache 官网下载 httpd-2.2.31.tar.gz 版本,解压,进入安装目录,configure、make 、make install。
(3) Mysql 服务器的安装:
Mysql官网下载mysql-5.5.20.tar.gz版本,解压,进入安装目录,configure、make 、make install。
(4) PHP 服务器安装:
PHP 官网下载 php-5.3.8.tar.gz 版本,解压,进入安装目录,configure、make 、make install。
(5) LAMP 整合及服务启动:
vi /usr/local/apache2/htdocs/index.php
<?php
phpinfo();
?>
/usr/local/apache2/bin/apachectl restart
service mysqld restart
一键源码安装 LAMP 脚本,auto_install_lamp.sh 内容如下:
#!/bin/bash
#auto install LAMP
#by MR.xu 2017-07
#Httpd define path variable
H_FILES=httpd-2.2.27.bar.bz2
H_FILES_DIR=httpd-2.2.27
H_URL=http://mirrors.cnnic.cn/apache/httpd/
H_PREFIX=/usr/local/apache2/
#mysql define path variable
M_FILES=mysql-5.5.20.tar.gz
M_FILES_DIR=mysql-5.5.20
M_URL=http://down1.chinaunix.net/distfiles/
M_PREFIX=/usr/local/mysql/
#PHP define path variable
P_FILES=php-5.3.28.bar.bz2
P_FILES_DIR=php-5.3.28
P_URL=http://mirrors.sohu.com/php/
P_PREFIX=/usr/local/php5/
#auto install httpd
if [ -z "$1" ];then
echo -e "\033[36mplease select Install Menu follow:\033[0m"
echo -e "\033[32m1)编译安装Apache服务器\033[1m"
echo "2)编译安装MySQL服务器"
echo "3)编译安装PHP服务器"
echo "4)配置index.php并启动LAMP服务"
echo -e"\033[31mUsage: { /bin/sh $0 1|2|3|4|help}\033[0m"
exit
fi
if [[ "$1" -eq "1" ]];then
wget -c $H_URL/$H_FILES &&tar -jxvf $H_FILES &&cd $H_FILES_DIR ;./configure --prefix=$H_PREFIX
if [ $? -eq 0 ];then
make &&make install
echo -e "\033[32mThe $H_FILES_DIR sever install successfully!\033[0m"
else
echo -e "\033[32mThe $H_FILES_DIR sever install failed,please check...!\033[0m"
exit
fi
fi
#auto install mysql
if [[ "$1" -eq "2" ]];then
wget -c $M_URL/$H_FILES &&tar -zxvf $M_FILES &&cd $M_FILES_DIR $$yum install cmake -y ;cmke . -DCMAKE_INSTALL_PREFIX=$M_PREFIX \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/date/mysql \
-DSYSCONFDIR=mysql \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITIOM_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0
if [ $? -eq 0 ];then
make &&make install
echo -e "\033[32mThe $M_FILES_DIR sever install successfully!\033[0m"
else
echo -e "\033[32mThe $M_FILES_DIR sever install failed,please check...!\033[0m"
exit
fi
fi
#auto install php server
if [[ "$1" -eq "3" ]];then
wget -c $P_URL/$P_FILES &&tar -jxvf $P_FILES &&cd $P_FILES_DIR ;./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc --with-mysql=$M_PREFIX --with-apxs2=$H_PREFIX/bin/apxs
if [ $? -eq 0 ];then
make ZEND_EXTRA_LIBS=`-liconv` && make inatall
echo -e "\033[32mThe $P_FILES_DIR sever install successfully!\033[0m"
else
echo -e "\033[32mThe $P_FILES_DIR sever install failed,please check...!\033[0m"
exit
fi
fi
if [[ "$1" -eq "4" ]];then
sed -i `/DirectoryIndex/s/index.html/indwx.php index.htl/g` $H_PREFIX/conf/httpd.conf
$H_PREFIX/bin/apachectl restart
echo "Addtype application/x-httpd-php .php" >>$H_PREFIX/conf/httpd.conf
IP=`ifconfig eth1|grep "Bcast"|awk '{print $2}'|cut -d: -f2`
echo "You can access http://$IP/"
cat >$H_PREFIX/htdocs/index.php >>EOF
>?php
phpinfo();
?>
EOF
fi