shell编程LANMP一键安装脚本v1.0(初稿)

shell编程LANMP一键安装脚本v1.0(初稿)


centos6,不区分32位、64位,要求机器可以上外网,支持lamp和lnmp,mysql支持5.1和5.6两个版本,php支持5.3和5.6两个版本,apache2.2,nginx1.8.nginx启动脚本 ,php配置文件等还没有补充完整。

#!/bin/bash
echo "LANMP一键安装脚本v1.0"
sleep 1
##排错函数,检查上个命令执行是否成功
check_ok() {
if [ $? != 0 ]
then
echo "安装发生错误,详情查看错误日志!"
exit 1
fi
}
##获取系统架构
ar=`arch`
##关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then
    setenforce 0
fi
##清空iptables
iptables-save > /etc/sysconfig/iptables_`date +%s`
iptables -F
service iptables save


##yum安装函数,对于已经安装的包不重复安装
myum() {
    if ! rpm -qa|grep -q "^$1"
    then
        yum install -y $1
        check_ok
     else
        echo "$1 已经安装!"
     fi
}
##安装一些依赖的包
for p in epel-release gcc wget perl perl-devel libaio libaio-devel pcre-devel epel-release zlib-devel
do
    myum $p
done
##安装mysql
install_mysqld() {
   echo "请选择MySQL版本!"
   select mysql_v in 5.1 5.6 
do
     case $mysql_v in
      5.1)
         cd /usr/local/src
         [ -f mysql-5.1.40-linux-$ar-glibc23.tar.gz ] || wget https://cdn.mysql.com/archives/mysql-5.1/mysql-5.1.40-linux-$ar-glibc23.tar.gz
         check_ok
         tar zxf mysql-5.1.40-linux-$ar-glibc23.tar.gz
         check_ok
         [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
         mv mysql-5.1.40-linux-$ar-glibc23 /usr/local/mysql
         cd /usr/local/mysql
         if ! grep '^mysql:' /etc/passwd
         then
                useradd -M mysql -s /sbin/nologin 
         fi
         myum compat-libstdc++-33
         [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_bak
         mkdir -p /data/mysql
         chown -R mysql:mysql /data/mysql
         ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
         check_ok
         /bin/cp support-files/my-huge.cnf /etc/my.cnf
         check_ok
         sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
          /bin/cp support-files/mysql.server /etc/init.d/mysqld
         sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
         chmod 755 /etc/init.d/mysqld
         chkconfig --add mysqld
         chkconfig mysqld on
         service mysqld start
         check_ok
         break
;;
        5.6)
         cd /usr/local/src
         [ -f mysql-5.6.39-linux-glibc2.12-$ar.tar.gz ] || wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-$ar.tar.gz  
         check_ok
         tar zxf mysql-5.6.39-linux-glibc2.12-$ar.tar.gz
         check_ok
         [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
         mv mysql-5.6.39-linux-glibc2.12-$ar /usr/local/mysql
         cd /usr/local/mysql
         if ! grep '^mysql:' /etc/passwd
         then
                useradd -M mysql -s /sbin/nologin 
         fi
         myum compat-libstdc++-33
         [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_bak
         mkdir -p /data/mysql
         chown -R mysql:mysql /data/mysql
         ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
         check_ok
         /bin/cp support-files/my-huge.cnf /etc/my.cnf
         check_ok
         sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
          /bin/cp support-files/mysql.server /etc/init.d/mysqld
         sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
         chmod 755 /etc/init.d/mysqld
         chkconfig --add mysqld
         chkconfig mysqld on
         service mysqld start
         check_ok
         break
         ;;
         *)
           echo "只能选择5.1或者5.6中的任一版本!"
           exit 1
           ;;
         esac
done
}      
##安装httpd
install_httpd() {
  echo "安装 APACHE-2.2.34 "
cd /usr/local/src
[ -f httpd-2.2.34.tar.gz ] || wget weget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.34.tar.gz  
 check_ok
 tar zxf httpd-2.2.34.tar.gz
 check_ok
cd httpd-2.2.34
./configure \
--prefix=/usr/local/apache2 \
--with-include-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre
check_ok
make && make install
check_ok
}
##安装lamp的php
install_php() {
echo -e "请选择需要安装php的版本"}
select php_v in 5.3 5.6
do 
   case $php_v in
         5.3)
            cd /usr/local/src
            [ -f php-5.3.28.tar.gz ] || wget http://cn.php.net/distributions/php-5.3.28.tar.gz
            check_ok
tar zxf php-5.3.28.tar.gz
cd php-5.3.28
for p in openssl-devel bzip2-devel \
libxml2-devel curl-devel libpng-devel \
libjpeg-devel freetype-devel libmcrypt-devel \
libtool-ltdl-devel perl-devel
do
     myum $p
done
check_ok
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6
check_ok
make && make install
check_ok
[ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production /usr/local/php/etc/php.ini
break
;;
5.6)
cd /usr/local/src
[ -f php-5.6.33.tar.gz ] || wget http://cn2.php.net/distributions/php-5.6.33.tar.gz
          check_ok
tar zxf php-5.6.33.tar.gz
cd php-5.6.33
for p in openssl-devel bzip2-devel \
libxml2-devel curl-devel libpng-devel \
libjpeg-devel freetype-devel libmcrypt-devel \
libtool-ltdl-devel perl-devel
do
     myum $p
done
check_ok
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6
check_ok
make && make install
check_ok
[ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production /usr/local/php/etc/php.ini
break
;;
*)
echo "只能选择php版本5.3或者5.6"
;;
           esac
done
}
##php与apache结合
join_apa_php() {
sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' /usr/local/apache2/conf/httpd.conf
check_ok
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' /usr/local/apache2/conf/httpd.conf
check_ok
cat > /usr/local/apache2/htdocs/index.php <
EOF
if /usr/local/php/bin/php -i |grep -iq 'date.timezone => no value'
then
    sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"' /usr/local/php/etc/php.ini
fi
/usr/local/apache2/bin/apachectl restart
check_ok
}


##检查nginx、httpd、php-fpm服务运行情况,防止多次执行脚本重复安装
check_service() {
if [ "$1" == "phpfpm" ]
then
    s="php-fpm"
else
    s=$1
fi
n=`ps aux|grep "$s"|wc -l`
if [ $n -gt 1 ]
then 
  echo "$1 服务已经启动了!"
else
   if [ -f /etc/init.d/$1 ]
   then
      /etc/init.d/$1 start
      check_ok
   else
      install_$1
   fi
fi
}


##安装lamp
lamp() {
check_service mysqld
check_service httpd
install_php
join_apa_php
echo "LAMP已经安装完成,访问http://ip/index.php进行测试,ip为本机ip地址。"
}


##安装nginx
install_nginx() {
cd /usr/local/src
[ -f nginx-1.6.3.tar.gz ] || wget http://nginx.org/download/nginx-1.6.3.tar.gz
check_ok
tar zxf nginx-1.6.3.tar.gz
cd nginx-1.6.3
myum pcre-devel
./configure --prefix=/usr/local/nginx
make && make install
check_ok
if [ -f /etc/init.d/nginx ]
then
    /bin/mv /etc/init.d/nginx /etc/init.d/nginx_`date +%s`
fi
/bin/cp /usr/local/src/nginx /etc/init.d/nginx
check_ok
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
/bin/cp /usr/local/src/nginx.conf /usr/local/nginx/conf/nginx.conf
check_ok
service nginx start
check_ok
echo -e " /usr/local/nginx/html/index.php
check_ok
}


##php-fpm安装
install_phpfpm() {
echo -e "安装php,请选择php版本!"
select php_v in 5.3 5.6
do 
   case $php_v in
         5.3)
            cd /usr/local/src
            [ -f php-5.3.28.tar.gz ] || wget http://cn.php.net/distributions/php-5.3.28.tar.gz
            check_ok
tar zxf php-5.3.28.tar.gz
cd php-5.3.28
for p in openssl-devel bzip2-devel \
libxml2-devel curl-devel libpng-devel \
libjpeg-devel freetype-devel libmcrypt-devel \
libtool-ltdl-devel perl-devel
do
     myum $p
done
check_ok
if ! grep -q '^php-fpm' /etc/passwd
then
   useradd -M -s /sbin/nologin php-fpm
   check_ok
fi
./configure \
--prefix=/usr/local/php-fpm \
--with-config-file-path=/usr/local/php-fpm/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysql-sock=/tmp/mysql.sock \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--enable-zend-multibyte \
--disable-ipv6 \
--with-curl \
--with-pear
check_ok
make && make install
check_ok
[ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production /usr/local/php-fpm/etc/php.ini
if /usr/local/php-fpm/bin/php -i|grep -iq 'date.timezone => no value'
then
    sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"' /usr/local/php-fpm/etc/php.ini
    check_ok
fi
/bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
chkconfig -add phpfpm
chkconfig phpfpm on
chmod 755 /etc/init.d/phpfmp
/bin/cp /usr/local/php-fpm/etc/{php-fpm.conf.default,php-fpm.conf}
service phpfpm start
break
;;
5.6)
cd /usr/local/src
[ -f php-5.6.33.tar.gz ] || wget http://cn2.php.net/distributions/php-5.6.33.tar.gz
check_ok
tar zxf php-5.6.33.tar.gz
cd php-5.6.33
for p in openssl-devel bzip2-devel \
libxml2-devel curl-devel libpng-devel \
libjpeg-devel freetype-devel libmcrypt-devel \
libtool-ltdl-devel perl-devel
do
     myum $p
done
check_ok
if ! grep -q '^php-fpm' /etc/passwd
then
   useradd -M -s /sbin/nologin php-fpm
   check_ok
fi
./configure \
--prefix=/usr/local/php-fpm \
--with-config-file-path=/usr/local/php-fpm/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysql-sock=/tmp/mysql.sock \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--enable-zend-multibyte \
--disable-ipv6 \
--with-curl \
--with-pear
check_ok
make && make install
check_ok
[ -f /usr/local/php-fpm/etc/php.ini ] || /bin/cp php.ini-production /usr/local/php-fpm/etc/php.ini
if /usr/local/php-fpm/bin/php -i|grep -iq 'date.timezone => no value'
then
    sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"' /usr/local/php-fpm/etc/php.ini
    check_ok
fi
/bin/cp sapi/fpm/init.d.php-fpm /etc/init.d/phpfpm
chkconfig -add phpfpm
chkconfig phpfpm on
chmod 755 /etc/init.d/phpfpm
/bin/cp /usr/local/php-fpm/etc/{php-fpm.conf.default,php-fpm.conf}
service phpfpm start
break
;;
*)
echo "只能选择php版本5.3或者5.6"
;;
esac
done
}


##安装lnmp
lnmp() {
check_service mysqld
check_service nginx
check_service phpfpm
echo "LNMP已经安装完成,访问http://ip/index.php进行测试,ip为本机IP地址!"
}
read -p "请选择需要安装的架构,输入LAMP或者LNMP!"
case $t in
   LAMP)
      lamp
      ;;
   LNMP)
      lnmp
      ;;
    *)
      echo "只能输入LAMP或者LNMP!"
      ;;
esac

你可能感兴趣的:(shell笔记备存)