今天對昨天的腳本優化了下,該腳本功能是能夠按選項進行安裝,雖然不如一鍵安裝腳本方便,但加入了選項變量,在別的場景仍是會時經常使用到的。同時隱去了輸出過程,更方便查看安裝進程。(因環境不一樣,僅供參考)
一樣,先上效果圖:
腳本內容:
#!/bin/bashphp
#Auto LAMP Installhtml
#by liupengfang ----2015-07-24-----mysql
if [ -z "$1" ];thenlinux
echo -e "\033[35m--歡迎使用LAMP自動安裝腳本--\033[0m"sql
echo -e '\033[32m----------------------------\033[0m'apache
echo -e "\033[35m-----請按照選項進行安裝-----\033[0m"bash
echo -e "\033[32m1)編譯安裝Apache服務器\033[0m"服務器
echo -e "\033[32m2)編譯安裝MySQL服務器\033[0m"app
echo -e "\033[32m3)編譯安裝PHP服務器\033[0m"ide
echo -e "\033[32m4)Apache+PHP整合\033[0m"
echo -e "\033[32m5)Mysql服務配置\033[0m"
echo -e "\033[31m請輸入: { /bin/sh $0 1|2|3|4|5}\033[0m"
exit
fi
if [[ "$1" -eq "1" ]];then
echo -e "\033[35m--Apache安裝--\033[0m"
echo -e "\033[35m開始解壓……\033[0m"
tar vxf httpd-2.2.29.tar.bz2 &>/dev/null
echo -e "\033[35m文件解壓完成!開始預編譯……\033[0m"
cd /lamp/httpd-2.2.29
else
echo -e "\033[35m預編譯失敗!\033[0m"
exit 0
fi
echo -e "\033[35mApache正在安裝……\033[0m"
make &>/dev/null && make install &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[35mApache安裝成功!\033[0m"
else
echo -e "\033[35mApache安裝失敗!\033[0m"
exit 0
fi
fi
if [[ "$1" -eq "2" ]];then
echo -e "\033[35m--Mysql安裝--\033[0m"
echo -e "\033[35m開始解壓……\033[0m"
cd /lamp/ ; tar vxf mysql-5.1.63.tar.gz &>/dev/null
echo -e "\033[35m解壓完成!開始預編譯……\033[0m"
else
echo -e "\033[35m預編譯失敗!\033[0m"
exit 0
fi
echo -e "\033[35m開始安裝Mysql……\033[0m"
make &>/dev/null && make install &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[35mMysql安裝成功!\033[0m"
else
echo -e "\033[35mMysql安裝失敗!\033[0m"
exit 0
fi
fi
if [[ "$1" -eq "3" ]];then
echo -e "\033[35m--PHP安裝--\033[0m"
yum install libxml2 libxml2-devel -y &>/dev/null
echo -e "\033[35m開始解壓……\033[0m"
cd /lamp/ ; tar vxf php-5.3.29.tar.bz2 &>/dev/null
echo -e "\033[35m解壓完成!開始預編譯……\033[0m"
cd /lamp/php-5.3.29
else
echo -e "\033[35m預編譯失敗!\033[0m"
exit 0
fi
echo -e "\033[35m開始安裝PHP……\033[0m"
make &>/dev/null && make install &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[35mPHP安裝成功!\033[0m"
else
echo -e "\033[35mPHP安裝失敗!\033[0m"
exit 0
fi
fi
if [[ "$1" -eq "4" ]];then
echo -e "\033[35m----Apache+PHP整合中……-----\033[0m"
ADDTYPE=/usr/local/apache2/conf/httpd.conf
sed -i 's/AddType application\/x-gzip .gz .tgz/& \n AddType applicati
on\/x-httpd-php .php/' $ADDTYPE
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/
' $ADDTYPE
echo -e "\033[35m----編寫PHP測試頁面……-----\033[0m"
sleep 2
cd /usr/local/apache2/htdocs/
echo '<?php' >> index.php
echo 'phpinfo();' >> index.php
echo '?>' >> index.php
echo -e "\033[35m----請訪問PHP測試頁面index.php-----\033[0m"
fi
if [[ "$1" -eq "5" ]];then
echo -e "\033[35m----Mysql配置-----\033[0m"
sleep 2
useradd mysql
rm -rf /etc/my.cnf
cp /lamp/mysql-5.1.63/support-files/my-medium.cnf /etc/my.cnf
cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on
mkdir /usr/local/mysql/data
chown -R mysql.mysql /usr/local/mysql
MY_CNF=/etc/my.cnf
sed -i 's/myisam_sort_buffer_size = 8M/& \ndatadir = \/usr\/local\/mysql
\/data\//' $MY_CNF
echo -e "\033[35mMysql初始化中……\033[0m"
/usr/local/mysql/bin/mysql_install_db --user=mysql --datadir=/usr/local/
mysql/data/ &>/dev/null
echo -e "\033[35m關閉防火牆……\033[0m"
service iptables stop &>/dev/null
echo -e "\033[35m關閉selinux……\033[0m"
setenforce 0 &>/dev/null
/usr/local/apache2/bin/apachectl start &>/dev/null
service mysqld start &>/dev/null
echo -e "\033[35m----Mysql配置完成-----\033[0m"
fi
if [[ "$1" -gt "5" ]];then
echo -e "\033[35m請輸入正確選項!\033[0m"
fi