Linux下AMP/NMP 源码编译后之---水平迁移

 

      近些日子,由于手里的项目涉及到了WEB基础环境,于是乎想着尽量简化基础环境的编译过程,因为编译实在是一件漫长的事情,而且其中就说不定会有编译报错,而解决这一问题的同时将花费一定时间,会给工作进度带来无形的压力。其实WEB基础环境的配置不复杂,之前笔者也部署过许许多多次,也看过很多名家的有关图书与文章。见过许多经典的SHELL脚本,一键安装、优化WEB配置,本次所写内容也是基于自己的项目、柔和了别人的脚本,而得来,还望看官不要见笑。

      首先说,网上的很多LA/NMP的一键安装脚本,多数都是直接下载并源码编译。我觉得编译速度、编译出错是我更改脚本直接原因。而我现在做的是只编译一台服务器的WEB环境,将此环境归档打包至其余服务器上。这样我只要保证1台服务器的可用,其余服务器也会一样可用。现给出脚本与其中细节遇到的问题处理方法。

 

#!/bin/bash
#---Check if user is root---#
if [ $(id -u) != "0" ]; then
printf "Error: You must be root to run this script!\n"
exit 1
fi
#---config system env ---#
IP=`/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
WEBPATH=/usr/local/src/webserver
WEBDATA=/data/web
echo "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/bashrc
echo "Beginning config System"
sleep 5
echo "[dvd]
name=server
baseurl=http://xxx.xxx.xxx.xxx/linux/5/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" > /etc/yum.repos.d/rhel-debuginfo.repo
yum -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" ];then
echo "Warning: User mysql has exsit,System will rebulid it Now"
sleep 5
userdel -r mysql
groupadd mysql
useradd -g mysql mysql
if [ $? == 0 ];then
echo "User rebulid successful"
fi
else
groupadd mysql
useradd -g mysql mysql
fi
#---install lamp---#
echo "Beginning install LAMP"
sleep 5
tar zxvf ./webserver.tar.gz -C /usr/local/src
cp $WEBPATH/mysql/support-files/my-medium.cnf  /etc/my.cnf
cp $WEBPATH/mysql/support-files/mysql.server   /etc/init.d/mysqld
cp $WEBPATH/apache/bin/apachectl  /etc/init.d/httpd
#---config vsftpd---#
cat > /etc/vsfptd.conf <<EOF
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
dual_log_enable=YES
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/user_list
local_max_rate=10000
anon_max_rate=10000
listen=YES
listen_port=21
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
EOF
useradd -d /var/ftp -s /sbin/nologin ftpuser
passwd ftpuser <<EOF
ftpuser
ftpuser
EOF
chmod -R 777 /var/ftp/
#---chkconfig serive httpd---#
chmod a+x /etc/init.d/httpd
mkdir -p /data/web
echo "# chkconfig: 345 85 15" >> /etc/rc.d/init.d/httpd
echo "# description: Activates/Deactivates Apache Web Server" >> /etc/rc.d/init.d/httpd
#---config httpd source---#
echo "<?phpinfo();?>" >$WEBDATA/test.php
echo "" > $WEBPATH/apache/logs/access_log
echo "" > $WEBPATH/apache/logs/error_log
#---Tuning system---#
ulimit -HSn 65536
echo -ne "
* soft nofile 409600
* hard nofile 409600
" >>/etc/security/limits.conf
cat >>/etc/sysctl.conf<<EOF
net.ipv4.netfilter.ip_conntrack_max = 131072
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096        87380   4194304
net.ipv4.tcp_wmem = 4096        16384   4194304
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 300
net.ipv4.ip_local_port_range = 1024    65000
#add ip_conntrack option
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120
net.ipv4.ip_conntrack_max = 128454
net.ipv4.netfilter.ip_conntrack_max = 128454
#add tcp option
net.ipv4.netfilter.ip_conntrack_tcp_timeout_last_ack = 30
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 10
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 30
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 600
net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_recv = 30
net.ipv4.netfilter.ip_conntrack_tcp_timeout_syn_sent = 30
#add syn option
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 3
EOF
#---CHECK LAMP SERVICE---#
/etc/init.d/iptables stop
if [ `getenforce` == "Disabled" ];then
echo "" > /dev/null
else
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
echo "Warning: SElinux has changed,You must reboot your system after Complete this installation "
sleep 5
fi
check_ftp=`lsof -i:21`
check_httpd_proc=`lsof -i:80`
check_mysqld_proc=`lsof -i:3306`
echo " NOW CHECKING SERVICE ON LAMP "
sleep 5
if [ "$check_httpd_proc" == "" ];then
/etc/init.d/httpd -k start
if [ $? -eq 0 ];then
echo "starting Httpd for apache              [  OK  ]"
else
echo "starting Httpd for apache              [ failed ]"
exit 1
fi
else
killall httpd
sleep 3
/etc/init.d/httpd -k start
if [ $? -eq 0 ];then
echo "starting Httpd for apache              [  OK  ]"
else
echo "starting Httpd for apache              [ failed ]"
exit 1
fi
fi
if [ "$check_mysqld_proc" == "" ];then
/etc/init.d/mysqld start
else
killall mysqld
sleep 3
/etc/init.d/mysqld start
fi
if [ "$check_ftp" == "" ];then
/etc/init.d/vsftpd start
else
killall vsftpd
sleep 3
/etc/init.d/vsftpd start
fi
echo "LAMP installed successful"
echo "You can visit http://$IP/test.php"
chkconfig --add httpd
chkconfig httpd on
chkconfig --add mysqld
chkconfig mysqld --level 2345 on
chkconfig --add vsftpd
chkconfig 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的启动项与判断条件,如有需要也可联系笔者,笔者愿与您一起分享。

 

本文出自 “运维-上下求索” 博客,转载请与作者联系!

你可能感兴趣的:(平移,lamp,LNMP,一键脚本,WEB基础环境)