近些日子,由于手里的项目涉及到了WEB基础环境,于是乎想着尽量简化基础环境的编译过程,因为编译实在是一件漫长的事情,而且其中就说不定会有编译报错,而解决这一问题的同时将花费一定时间,会给工作进度带来无形的压力。其实WEB基础环境的配置不复杂,之前笔者也部署过许许多多次,也看过很多名家的有关图书与文章。见过许多经典的SHELL脚本,一键安装、优化WEB配置,本次所写内容也是基于自己的项目、柔和了别人的脚本,而得来,还望看官不要见笑。
首先说,网上的很多LA/NMP的一键安装脚本,多数都是直接下载并源码编译。我觉得编译速度、编译出错是我更改脚本直接原因。而我现在做的是只编译一台服务器的WEB环境,将此环境归档打包至其余服务器上。这样我只要保证1台服务器的可用,其余服务器也会一样可用。现给出脚本与其中细节遇到的问题处理方法。
#!/bin/bash#---Check if user is root---#if [ $(id -u) != "0" ]; thenprintf "Error: You must be root to run this script!\n"exit 1fi#---config system env ---#IP=`/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`WEBPATH=/usr/local/src/webserverWEBDATA=/data/webecho "PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:$WEBPATH/mysql/bin:$WEBPATH/apache/bin" >> /etc/bashrc. /etc/bashrcecho "Beginning config System"sleep 5echo "[dvd]name=serverbaseurl=http://xxx.xxx.xxx.xxx/linux/5/enabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" > /etc/yum.repos.d/rhel-debuginfo.repoyum -y install dialog ntp vim-enhanced vixie-cron gcc gcc-c++ flex bison autoconf automake glibc \glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel libtool* zlib-devel \libxml2-devel libjpeg-devel libpng-devel libtiff-devel fontconfig-devel freetype-devel \libXpm-devel gettext-devel curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn \libidn-devel openssl openssl-devel openldap openldap-devel \nss_ldap openldap-clients openldap-servers patch unzip lsof vsftpd#---check user mysql---#check_user=`id mysql`if [ -n "$check_user" ];thenecho "Warning: User mysql has exsit,System will rebulid it Now"sleep 5userdel -r mysqlgroupadd mysqluseradd -g mysql mysqlif [ $? == 0 ];thenecho "User rebulid successful"fielsegroupadd mysqluseradd -g mysql mysqlfi#---install lamp---#echo "Beginning install LAMP"sleep 5tar zxvf ./webserver.tar.gz -C /usr/local/srccp $WEBPATH/mysql/support-files/my-medium.cnf /etc/my.cnfcp $WEBPATH/mysql/support-files/mysql.server /etc/init.d/mysqldcp $WEBPATH/apache/bin/apachectl /etc/init.d/httpd#---config vsftpd---#cat > /etc/vsfptd.conf <> /etc/rc.d/init.d/httpdecho "# description: Activates/Deactivates Apache Web Server" >> /etc/rc.d/init.d/httpd#---config httpd source---#echo " " >$WEBDATA/test.phpecho "" > $WEBPATH/apache/logs/access_logecho "" > $WEBPATH/apache/logs/error_log#---Tuning system---#ulimit -HSn 65536echo -ne "* soft nofile 409600* hard nofile 409600" >>/etc/security/limits.confcat >>/etc/sysctl.conf< /dev/nullelsesed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/configecho "Warning: SElinux has changed,You must reboot your system after Complete this installation "sleep 5ficheck_ftp=`lsof -i:21`check_httpd_proc=`lsof -i:80`check_mysqld_proc=`lsof -i:3306`echo " NOW CHECKING SERVICE ON LAMP "sleep 5if [ "$check_httpd_proc" == "" ];then/etc/init.d/httpd -k startif [ $? -eq 0 ];thenecho "starting Httpd for apache [ OK ]"elseecho "starting Httpd for apache [ failed ]"exit 1fielsekillall httpdsleep 3/etc/init.d/httpd -k startif [ $? -eq 0 ];thenecho "starting Httpd for apache [ OK ]"elseecho "starting Httpd for apache [ failed ]"exit 1fifiif [ "$check_mysqld_proc" == "" ];then/etc/init.d/mysqld startelsekillall mysqldsleep 3/etc/init.d/mysqld startfiif [ "$check_ftp" == "" ];then/etc/init.d/vsftpd startelsekillall vsftpdsleep 3/etc/init.d/vsftpd startfiecho "LAMP installed successful"echo "You can visit http://$IP/test.php"chkconfig --add httpdchkconfig httpd onchkconfig --add mysqldchkconfig mysqld --level 2345 onchkconfig --add vsftpdchkconfig vsftpd --level 2345 on
以上脚本是AMP的快速平移过程。笔者已测试并在生产环境中运行。其中apache2.4.3,mysql5.5.28,php5.4.9。源码编译过程这里就不详细书写了,编译很简单3板斧嘛,configure && make && make install ,过程中笔者在编译apache时出现,dirxxx.so找不到,解决办法为将apache 的apr,apr-util扩展编译好后,复制到apache的slib目录中即可。另:编译时的安装目录,即--prefix 选项,在平移后要保持绝对路径一致。因为编译已经写死,大家可以在编译前按照给自公司运维规范,把以上脚本改好,只需改动路径即可。另:低版本amp编译过程中,会遇到php的动态库问题,导致apache无法启动,这与高版本的apache、php不太一样,需要把缺少的so文件复制到另一server的/usr/lib /usr/lib64中。本脚不做详细解释,如果您也是一名运维人员或是shell爱好着,相信以上的脚本您可以看懂,还能帮我提出不少宝贵意见。如果有朋友用了以上的脚本出现问题,或是平移途中出现问题,可以与笔者联系,笔者将尽力帮您分析解决您的问题。 NMP的脚本与AMP类似,无非是多加个php-cgi或是php-fpm的启动项与判断条件,如有需要也可联系笔者,笔者愿与您一起分享。