说明:把安装包和此脚本放在/usr/local/src目录下,运行脚本就行了。
#!/bin/bash
#this script used to install apache mysql and php
#check user
if [ `whoami` != "root" ];then
echo "Installtion needs root user!"
exit 1
fi
#define add path to $PATH function
add_path(){
eval ${soft_name}_path=$path
eval exec_path=\$${soft_name}_path
echo "export PATH=\$PATH:${exec_path}/bin" >> /root/.bash_profile
unset path
}
#if compile had something error then exit
compile_error(){
echo "${soft_name} compile has somethine wrong,please manual check it" |tee /var/lamp_error.log
exit 1
}
#install php library
echo "Before you install php,you must install some library for php,be sure you yum can work well!"
echo "yum install libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel gd gd-devel"
echo "yum remove httpd php mysql"
echo " "
yum -y install libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel gd gd-devel libtermcap-devel
yum -y remove httpd php mysql
[ $? -eq 0 ]||(echo "yum fail,check your yum repositories" && exit 1)
#read tar file path
echo "You must give me a path where is the tar.gz file,If you download tar.gz file to /usr/local/src,press enter continue!"
echo " "
read -t 10 -p "where is the tar file:" ori_path
ori_path=${ori_path:=/usr/local/src}
cd $ori_path
#which software do you want to install
pack=`ls *.tar.gz|egrep 'http|mysql|php'`
for i in `echo $pack`
do
soft_name=`echo $i|cut -d- -f1`
version=`echo $i|sed "s#.*-\(.*\)\.tar\.gz#\1#g"`
#get package infomation
read -t 10 -p "please enter your $i install path:" path
echo "if you don't assign a path,we'll use default path(/usr/local/you_soft_name)"
if [ "$soft_name" == "httpd" ];then
path=${path:=/usr/local/apache}
else
path=${path:=/usr/local/${soft_name}}
fi
tar xzf $i
#confiure and make
cd ${soft_name}-${version}
case "$soft_name" in
httpd|http)
./configure --prefix=$path --with-mpm=worker --enable-so --enable-rewrite --enable-deflate --enable-proxy
make && make install ||compile_error
add_path
cd .. /
;;
mysql)
useradd mysql
./configure --prefix=$path --with-extra-charsets=all --enable-thread-safe-client
make && make install ||compile_error
./scripts/mysql_install_db --user=mysql
chown -R root.mysql $path
chown -R mysql.mysql $path/var
cp support-files/mysql.server /etc/init.d/mysqld
cp support-files/my-huge.cnf /etc/my.cnf
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
echo "mysql installtion finished,if you want to start mysql server, you can use command like this 'server mysqld start'"
echo " "
add_path
cd ../
;;
php)
echo "before installtion php,you must install some libary for php,you can use yum like below command,yum install libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel gd gd-devel"
./configure --prefix=$path --enable-mbstring --with-apxs2=$httpd_path/bin/apxs --with-mysql=$mysql_path --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --enable-sockets
make && make install ||compile_error
cp php.ini-dist /usr/local/php/lib/php.ini
add_path
cd ../
;;
*)
echo "I'm so sorry,doesn't support $soft_name now!"
exit 1;;
esac
done
#edit configure file
sed -i '/#ServerName www.example.com:80/c\ServerName 127.0.0.1:80' $httpd_path/conf/httpd.conf
grep -q 'php5_module' $httpd_path/conf/httpd.conf || sed -i '/# LoadModule foo_module modules\/mod_foo.so/a\LoadModule php5_module modules/libphp5.so' $httpd_path/conf/httpd.conf
grep -q 'AddType application/x-httpd-php .php' $httpd_path/conf/httpd.conf || sed -i '/#AddType application\/x-gzip/a\AddType application/x-httpd-php .php' $httpd_path/conf/httpd.conf
sed -i '/DirectoryIndex index.html/s/$/ index.php/g' $httpd_path/conf/httpd.conf
echo -ne "<?php\nphpinfo();\n?>\n" > $httpd_path/htdocs/test.php
#start web mysql server
service mysqld start
/usr/local/apache/bin/apachectl start
#this is for user
echo ' '
echo ' '
echo '#####################################################################'
echo 'Install finished..................................'
echo 'you can use command "apache start/stop" to start/stop apache'
echo 'start/stop mysql like "service mysqld start/stop"'
echo 'http and mysql server was started!'
echo 'For test you LAMP, input url http://your ip/test.php in your browser!'
echo '#####################################################################'
echo 'Do not forget execute command "source /root/.bash_profile" to affect '
echo 'your environment!'