Shell脚本---lamp环境安装
2021年4月9日
lamp 环境安装
思路:
- lamp 安装
- apache(apr 及其相关包)
- mysql(mysql 的配置,安装时调用配置)
- php(php 相关包)
代码:
#!/bin/bash
#变量定义===========================================================
PACKAGES="../soft"
CURRENT=`pwd`
APR=apr-1.5.2.tar.bz2
APR_UTIL=apr-util-1.5.4.tar.bz2
APR_ICONV=apr-iconv-1.2.1.tar.bz2
APACHE=httpd-2.4.12.tar.bz2
MYSQL=mysql-5.5.29.tar.gz
LIBICONV=libiconv-1.14.tar.gz
LIBMCRYPT=libmcrypt-2.5.8.tar.gz
MCRYPT=mcrypt-2.6.8.tar.gz
MHASH=mhash-0.9.9.9.tar.gz
PHP=php-5.6.9.tar.bz2
INS_PATH="/usr/local"
SOU_PATH="/usr/src"
#定义函数========================================================
#确认是否为root用户----------------------
check(){
[ $UID -ne 0 ] && echo "need to root is that" && exit 1
#安装依赖---------------------------------------
yum seach elink &>/dev/null
if [ $? -eq 0 ];then
yum -y install gcc gcc-c++ openssl-devel pcre-devel ncurses-devel libtool libxml2-devel bzip2-cevel net-snmp-devel curl-devel libpng-devel freetype-devel libjpeg-devel zlib-devel cmake
[ $? -eq 0 ] && echo "YUM: ERROR" && exit 1
else
echo "YUM: ERROR";exit 1
fi
}
#安装apache-----------------------------------------------------------------------------------------------
apr(){
if [ -f $PACKAGES/$APR ];then
tar -xf $PACKAGES/$APR -C $SOU_PATH
CUR_PACKAGE=`echo $APR | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];then
cd $SOU_PATH/$CUR_PACKAGE
./configure --prefix=$INS_PATH/apr 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "APR: make install ERROR , EXIT......";exit 1
else
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "APR: make ERROR , EXIT......";exit 1
fi
else
echo "APR: configure ERROR , ECXIT......";exit 1
fi
else
echo "APR: tar command ERROR , EXIT......";exit 1
fi
else
echo "APR: not found $APR , EXIT";exit 1
fi
}
apr_util(){
if [ -f $PACKAGES/$APR_UTIL ];then
tar -xf $PACKAGES/$APR_UTIL -C $SOU_PATH
CUR_PACKAGE=`echo $APR_UTIL | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --prefix=$INS_PATH/apr-util --with-apr=$INS_PATH/apr/ 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "APR-UTIL: make install ERROR , EXIT......";exit 1
else
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "APR-UTIL: make ERROR , EXIT......";exit 1
fi
else
echo "APR-UTIL: configure ERROR , ECXIT......";exit 1
fi
else
echo "APR-UTIL: tar command ERROR , EXIT......";exit 1
fi
else
echo "APR-UTIL: not found $APR_UTIL , EXIT";exit 1
fi
}
apr_iconv(){
if [ -f $PACKAGES/$APR_ICONV ];then
tar -xf $PACKAGES/$APR_ICONV -C $SOU_PATH
CUR_PACKAGE=`echo $APR_ICONV | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --prefix=$INS_PATH/apr-iconv --with-apr=$INS_PATH/apr/ 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "APR-ICONV: make install ERROR , EXIT......";exit 1
else
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "APR-ICONV: make ERROR , EXIT......";exit 1
fi
else
echo "APR-ICONV: configure ERROR , ECXIT......";exit 1
fi
else
echo "APR-ICONV: tar command ERROR , EXIT......";exit 1
fi
else
echo "APR-ICONV: not found $APR_ICONV , EXIT";exit 1
fi
}
apache(){
apr;apr_util;apr_iconv
[ $? -ne 0 ] && echo "ERROR: please check apr install" && exit 1
if [ -f $PACKAGES/$APACHE ];then
tar -xf $PACKAGES/$APACHE -C $SOU_PATH
CUR_PACKAGE=`echo $APACHE | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --prefix=$INS_PATH/apache --with-apr=$INS_PATH/apr --with-apr-util=INS_PATH/apr-util --enable-module=so --enable-deflate=shared --enable-expires=shared --enable-rewite=shared --enable-cache=shared --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-static-support --enable-static-ab --disable-userdir --with-mpm=prefork --enable-nonportable-atomics --disable-ipv6 --with-sendfile 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "APACHE: make install ERROR , EXIT......";exit 1
else
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "APACHE: make ERROR , EXIT......";exit 1
fi
else
echo "APACHE: configure ERROR , ECXIT......";exit 1
fi
else
echo "APACHE: tar command ERROR , EXIT......";exit 1
fi
else
echo "APACHE: not found $APR_ICONV , EXIT";exit 1
fi
}
#安装mysql--------------------------------------------------------------------------------------------------
#mysql配置
mysql_install_post_set(){
useradd -s /sbin/nologin -r mysql
mkdir -pv /usr/local/mysql/data
cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 etc/init.d/mysql
chown mysql:mysql /use/local/mysql/ -R
ln -s /usr/local/mysql/bin/* /usr/bin/
ln -s /usr/local/mysql/lib/* /usr/lib/
ln -s /usr/local/mysql/libexec/* /usr/local/libexec
ln -s /usr/local/mysql/share/man/man1/* /usr/share/man/man1
ln -s /usr/local/mysql/share/man/man8/* /usr/share/man/man8
sh scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 1>/dev/null
}
mysql(){
if [ -f $PACKSGES/$MYSQL ];then
tar -xf $PACKAGES/$MYSQL -C $SOU_PATH
CUR_PACKAGE=`echo $APACHE | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];then
cd $SOU_PATH/SUR_PACKAGE
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data/ -DMYSQL_UNIX_ADDR=/usr/local/mysql.sock -DWITH_INNODBBASE_STOTRAGE_ENGINE=1 -DENABLE_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql -DWITH_DEBUG=0 -DWITH_EMBEDED_SERVER=0 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "MYSQL: make install ERROR , EXIT......";exit 1
else
mysql_install_port_set
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "MYSQL: make ERROR , EXIT......";exit 1
fi
else
echo "MYSQL: configure ERROR , ECXIT......";exit 1
fi
else
echo "MYSQL: tar command ERROR , EXIT......";exit 1
fi
else
echo "MYSQL: not found $MYSQL , EXIT";exit 1
fi
}
#安装php---------------------------------------------------------------------------------------------------------
libiconv(){
if [ -f $PACKAGES/$LIBICONV ];then
tar -xf $PACKAGES/$LIBICONV -C $SOU_PATH
CUR_PACKAGE=`echo $LIBICONV | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --prefix=/usr/local --with-apr=$INS_PATH/apr 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "LIBICONV: make install ERROR , EXIT......";exit 1
else
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "LIBICONV: make ERROR , EXIT......";exit 1
fi
else
echo "LIBICONV: configure ERROR , ECXIT......";exit 1
fi
else
echo "LIBICONV: tar command ERROR , EXIT......";exit 1
fi
else
echo "LIBICONV: not found $LIBICONV , EXIT";exit 1
fi
}
libmcrypt(){
if [ -f $PACKAGES/$LIBMCRYPYT ];then
tar -xf $PACKAGES/$LIBMCRYPYT -C $SOU_PATH
CUR_PACKAGE=`echo $LIBMCRYPYT | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --prefix=$INS_PATH/libmcrypt 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "LIBMCRYPYT: make install ERROR , EXIT......";exit 1
else
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install 1>/dev/null
make 1>/dev/null
make install 1>/dev/null
if [ $? -eq 0 ];then
echo "export LD_LIBRARY_PATH=$INS_PATH/libmcrypt/lib:LD_LIBRARY_PATH" >> /etc/profile
source /etc/profile
cd $CURRENT;rm -rf $SOU_PATH/CUR_PACKAGE
else
echo "LIBMCRYPT(libltdl): ERROR , EXIT......";exit 1
fi
fi
else
echo "LIBMCRYPT: make ERROR , EXIT......";exit 1
fi
else
echo "LIBMCRYPT: configure ERROR , ECXIT......";exit 1
fi
else
echo "LIBMCRYPT: tar command ERROR , EXIT......";exit 1
fi
else
echo "LIBMCRYPT: not found $LIBMCRYPT , EXIT";exit 1
fi
}
mhash(){
if [ -f $PACKAGES/$MHASH ];then
tar -xf $PACKAGES/$MHASH -C $SOU_PATH
CUR_PACKAGE=`echo $MHASH | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "MHASH: make install ERROR , EXIT......";exit 1
else
ln -s /usr/local/lib* /usr/lib/
ln -s /usr/local/bin/libmcrypt-config /usr/bin/
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "MHASH: make ERROR , EXIT......";exit 1
fi
else
echo "MHASH: configure ERROR , ECXIT......";exit 1
fi
else
echo "MHASH: tar command ERROR , EXIT......";exit 1
fi
else
echo "MHASH: not found $MHASH , EXIT";exit 1
fi
}
mcrypt(){
if [ -f $PACKAGES/$MCRYPT ];then
tar -xf $PACKAGES/$MCRYPT -C $SOU_PATH
CUR_PACKAGE=`echo $MCRYPT | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --with-libmcrypt-prefix=$INS_PATH.libmcrypt 1>/dev/null
if [ $? -eq 0 ];then
make 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "MCRYPT: make install ERROR , EXIT......";exit 1
else
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "MCRYPT: make ERROR , EXIT......";exit 1
fi
else
echo "MCRYPT: configure ERROR , ECXIT......";exit 1
fi
else
echo "MCRYPT: tar command ERROR , EXIT......";exit 1
fi
else
echo "MCRYPT: not found $MCRYPT , EXIT";exit 1
fi
}
php(){
libiconv;libmcrypt;mhash;mcrypt
[ $? -ne 0 ] && echo "check libiconv and encrypt install" && exit 1
if [ -f $PACKAGES/$PHP ];then
tar -xf $PACKAGES/$PHP -C $SOU_PATH
CUR_PACKAGE=`echo $PHP | awk -F ".tar" '{print $1}'`
if [ -d $SOU_PATH/$CUR_PACKAGE ];thne
cd $SOU_PATH/SUR_PACKAGE
./configure --prefix=$INS_PATH/php --with-config-file-path=$INS_PATH/php/etc --with-mysql=$INS_PATH/mysql/ --with-mysql=$INS_PATH/mysql/bin/mysql_config --with-pdo-mysql=$INS_PATH/mysql --with-iconv-dir=/usr/local/ --enable-fpm --with-fpm-user=apache --with-fpm-group=apache --with-pcre-regex --with-zlib --with-bz2 --enable-calendar --disable-phar --with-curl --enable-dba --with-libxml-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-mhash --enable-mbstring --with-mcrypt=$INS_PATH/libmcrypt --enable-opcache=no --enable-pcntl --enable-xml --disable-rpath --enable-shmop --enable-sockets --enable-zip --enable-bcmath --with-snmp --disable-ipv6 --disable-rpath --disable-debug --with-apxs2=$INS_PATH/apache/bin/apxs 1>/dev/null
if [ $? -eq 0 ];then
make ZEND_EXTRA_LIBS='-liconv' 1>/dev/null
if [ $? -eq 0 ];then
make install 1>/dev/null
if [ $? -ne 0 ];then
echo "PHP: make install ERROR , EXIT......";exit 1
else
sed -i.bak '/libphp5.so/a\AddType application/x-httpd-php .php' $INS_PATH/apache/conf/httpd.con
cp php.ini-production $INS_PATH/php/etc/php.ini
cd -;rm -rf $SOU_PATH/CUR_PACKAGE
fi
else
echo "PHP: make ERROR , EXIT......";exit 1
fi
else
echo "PHP: configure ERROR , ECXIT......";exit 1
fi
else
echo "PHP: tar command ERROR , EXIT......";exit 1
fi
else
echo "PHP: not found $PHP , EXIT";exit 1
fi
}
#调用函数===========================================================
case $1 in
apache) apache;;
mysql) mysql;;
lamp) check;apache;mysql;php;;
*) echo "$0: apache|mysql|lamp";;
esac