摘要: 脚本部署lnmp环境
#!/bin/sh
#date: 2014.8
#author: leeypp@gmail.com
#system: red hat enterprise 6.0
#software message:
# mysql-5.5.12.tar.gz
# nginx-1.2.1.tar.gz
# php-5.3.6.tar.bz2
#function: compile and install mysql,php,nginx on linux
#/tmp/lnmp.log trace the running of script
#after run script,can check error via /tmp/lnmp.log
#if all success,can use directly,default directory is /usr/local/lnmp/
##[root@server60 lnmp]# ls
# a key to lnmp mcrypt-2.6.8.tar.gz nginx-1.2.1.tar.gz
# libiconv-1.13.1.tar.gz mhash-0.9.9.9.tar.bz2 php-5.3.6.tar.bz2
# libmcrypt-2.5.8.tar.bz2 mysql-5.5.12.tar.gz
#function: remove all old httpd,mysql,php packets
RemoveOldSoftware ()
{
#--------< check httpd packet,stop service,remove packet >---------
rpm -q httpd > /dev/null
if [ $? == "0" ]
then
pgrep httpd > /dev/null
if [ $? == "1" ]
then
rpm -e httpd > /dev/null
echo "remove httpd packets ..." >> /tmp/lnmp.log
else
/etc/init.d/httpd stop > /dev/null
rpm -e httpd > /dev/null
echo "stop httpd service ..." >> /tmp/lnmp.log
echo "remove httpd packets ..." >> /tmp/lnmp.log
fi
fi
#--------< check mysqld packet,stop service,remove packet >---------
rpm -q mysql > /dev/null
if [ $? == "0" ]
then
pgrep mysqld > /dev/null
if [ $? == "1" ]
then
rpm -e mysql --nodeps > /dev/null
echo "remove mysql packets ..." >> /tmp/lnmp.log
else
/etc/init.d/mysqld stop > /dev/null
rpm -e mysql-server --nodeps > /dev/null
echo "stop mysqld service ..." >> /tmp/lnmp.log
echo "remove mysql packets ..." >> /tmp/lnmp.log
fi
fi
#--------< check php packet, remove packet >---------
rpm -q php > /dev/null
if [ $? == "0" ]
then
rpm -e php --nodeps > /dev/null
echo "remove php packets ..." >> /tmp/lnmp.log
fi
}
#function: install all tools during compile and install,and install libs
InstallToolsAndLib ()
{
yum install cmake gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel -y > /dev/null
echo "all tools and libs needed during compile and install" >> /tmp/lnmp.log
}
#function: compile and install mysql, and start mysqld service
#notice: mysql-5.5.12.tar.gz on /lnmp
MysqlPart()
{
#-----------< compile and install mysql >-------------------
cd /lnmp/
tar zxf mysql-5.5.12.tar.gz
cd mysql-5.5.12/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock
-DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
-DEXTRA_CHARSETS=all
make && make install
echo "compile and install mysql finished ..." >> /tmp/lnmp.log
#-----------< config mysql and start mysqld >------------------
cd /usr/local/lnmp/mysql/
#add user mysql with nologin shell
useradd -M -s /sbin/nologin mysql
#copy mysql module file
cp support-files/my-medium.cnf /etc/my.cnf
chown mysql.mysql . -R
#init mysql
scripts/mysql_install_db --user=mysql --basedir=/usr/local/lnmp/mysql/ --datadir=/usr/local/lnmp/mysql/data/ > /dev/null
#change dir pemission
chown -R root .
chown -R mysql data
#copy mysql script to /etc/init.d/
cp support-files/mysql.server /etc/init.d/mysqld
#start mysqld service
/etc/init.d/mysqld start > /dev/null
if [ $? == "0"]
then
echo "mysqld started"
fi
#不然php编译的时候找不到mysql的库文件
ln -s /usr/local/lnmp/mysql/lib /usr/local/lnmp/mysql/lib64
echo "mysql finished ..." >> /tmp/lnmp.log
#change ~/.bash_profile file
cat > .bash_profile << end
# .bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
export PATH
end
}
#function: compile and install nginx
NginxPart()
{
# install libs needed to compile nginx
yum install openssl-devel pcre-devel -y > /dev/null
cd /lnmp/
#compile and install
tar zxf nginx-1.2.1.tar.gz
cd nginx-1.2.1/
useradd -M -s /sbin/nologin nginx
./configure --user=nginx --group=nginx --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module > /dev/null
make > /dev/null && make install > /dev/null
echo "nginx compile and install seccess!" >> /tmp/lnmp.log
#start nginx
cd /usr/local/lnmp/nginx/conf/
cp nginx.conf nginx.conf.bak
sed '2a user nginx nginx;' nginx.conf > /dev/null
cd /usr/local/lnmp/nginx/sbin/
./nginx
echo "start nginx service ...." >> /tmp/lnmp.log
#test nginx
echo "wait to test ..." >> /tmp/lnmp.log
curl http://127.0.0.1
if [ $? == "0" ]
then
echo "nginx test ok!" >> /tmp/lnmp.log
else
echo "nginx test not ok! please check ..." >> /tmp/lnmp.log
fi
}
PhpPart()
{
# install libs
cd /lnmp/
yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openldap-devel -y > /dev/null
#compile and install modules
tar zxf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --libdir=/usr/local/lib64/ > /dev/null
make > /dev/null && make install > /dev/null
cd ..
tar jxf libmcrypt-2.5.8.tar.bz2
cd libmcrypt-2.5.8
./configure --libdir=/usr/local/lib64/ > /dev/null
make > /dev/null && make install > /dev/null
cd libltdl/
./configure --libdir=/usr/local/lib64 --enable-ltdl-install > /dev/null
make > /dev/null && make install > /dev/null
cd ../../
tar jxf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure --libdir=/usr/local/lib64/ > /dev/null
make > /dev/null && make install > /dev/null
cd ..
ldconfig /usr/local/lib64/
tar zxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure --libdir=/usr/local/lib64/ > /dev/null
#注意:./configure 时可能会报这个错:/bin/rm: cannot remove `libtoolT’: No such file or directory 直接忽略
make > /dev/null && make install > /dev/null
echo "all modules have been installed" >> /tmp/lnmp.log
echo "installing php" >> /tmp/lnmp.log
cd ..
tar jxf php-5.3.6.tar.bz2
cd php-5.3.6
./configure --prefix=/usr/local/lnmp/php \
--with-config-file-path=/usr/local/lnmp/php/etc \
--with-mysql=/usr/local/lnmp/mysql/ \
--with-openssl --with-snmp --with-gd \
--with-zlib --with-curl --with-libxml-dir \
--with-png-dir --with-jpeg-dir \
--with-freetype-dir --without-pear \
--with-gettext --with-gmp --enable-inline-optimization \
--enable-soap --enable-ftp --enable-sockets \
--enable-mbstring --with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config \
--enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx \
--with-libdir=lib64 --with-ldap --with-ldap-sasl \
--with-mcrypt --with-mhash > /dev/null
make ZEND_EXTRA_LIBS='-liconv' > /dev/null
make install > /dev/null
echo "php install finished ..." >> /tmp/lnmp.log
echo "config php and start php service" >> /tmp/lnmp.log
cd /usr/local/lnmp/php/bin/
wget http://pear.php.net/go-pear.phar > /dev/null
echo "input you chioce:"
yes "" | ./php go-pear.phar
cd /lnmp/php-5.3.6/
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp php.ini-production /usr/local/lnmp/php/etc/php.ini
cd /usr/local/lnmp/php/etc/
mv php-fpm.conf.default php-fpm.conf
cat >> php-fpm.conf << end
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 5
end
echo "starting php...." >> /tmp/lnmp.log
echo "test php service ..."
/etc/init.d/php-fpm start > /dev/null
lsof -i:9000
if [ $? == "0"]
then
echo "php service ok!"
else
echo "php service not ok,please check...."
fi
}
# RemoveOldSoftware test successful
# InstallToolsAndLib test successful
# MysqlPart test successful
# NginxPart test successful
# PhpPart test successful
echo "compile and install mysql,nginx,and php on RHEL6.0 finished" >>/tmp/lnmp.log
echo "please check logfile /tmp/lnmp.log" >> /tmp/lnmp.log
echo "use it after config services's configfile"