ls0398 发表于 2018-12-3 07:26:29

LAMP、LNMP、Tomcat自动安装脚本

  脚本说明:

[*]  实现自动化搭建LAMP服务,或者是LNMP服务,还有Tomcat
[*]  注意安装Tomcat需要下载jdk-8u161-linux-x64.tar.gz到/usr/local/src
[*]  不能同时安装LAMP 和 LNMP
[*]  安装完成后需要执行source /etc/profile

  安装菜单:
http://s1.运维网.com/images/20180329/1522311235739967.png
  脚本源码:
#!/bin/bash
# create by kindy
# date: 2018.03.26
# centos7 + apache2.4 + mysql5.6 + php5.6 + nginx1.8 + jdk1.8 + tomcat8
dependence(){
    echo -e "\e[1;32m ----- install wget pcre gcc libxml2 expat perl autocon ----- \e[0m"
    yum install -y wget pcre-devel gcc libxml2-devel expat-devel perl-Module-Install autoconf
    echo -e "\e[1;32m ----- install openssl bzip2 libjpeg libpng freetype epel mcrypt curl ----- \e[0m"
    yum install -y openssl-devel bzip2-devel libjpeg-devel libpng-devel freetype-devel epel-release libmcrypt-devel curl-devel
}
download(){
    cd /usr/local/src
    echo -e "\e[1;32m ----- downloading apache-2.4.29 ----- \e[0m"
    wget http://mirrors.sohu.com/apache/httpd-2.4.29.tar.gz
    wget http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.3.tar.gz
    wget http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
    echo -e "\e[1;32m ----- downloading mysql-5.6.36 ----- \e[0m"
    wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
    echo -e "\e[1;32m ----- downloading php-5.6.9 ----- \e[0m"
    wget http://mirrors.sohu.com/php/php-5.6.9.tar.gz
    echo -e "\e[1;32m ----- downloading nginx-1.8.0 ----- \e[0m"
    wget http://mirrors.sohu.com/nginx/nginx-1.8.0.tar.gz
    echo -e "\e[1;32m ----- download xcache-3.2 ----- \e[0m"
    wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
    echo -e "\e[1;32m ----- download tomcat-8.5 ----- \e[0m"
    wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz
    echo -e "\e[1;32m ********** download finished ********* \e[0m"
    ls /usr/local/src/
}
install_apache(){
    cd /usr/local/src/
    [ -d apr-1.6.3 ] || tar -zxf apr-1.6.3.tar.gz
    [ -d apr-util-1.6.1 ] || tar -zxf apr-util-1.6.1.tar.gz
    [ -d httpd-2.4.29 ] || tar -zxf httpd-2.4.29.tar.gz
    cd /usr/local/src/apr-1.6.3/
    ./configure --prefix=/usr/local/apr && make && make install
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- apr install failed! ----- \e[0m"
    else
      echo -e "\e[1;32m ----- apr is installed. ----- \e[0m"
    fi
    cd /usr/local/src/apr-util-1.6.1/
    ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- apr-util install failed! ----- \e[0m"
    else
      echo -e "\e[1;32m ----- apr-util is installed. ----- \e[0m"
    fi
    cd /usr/local/src/httpd-2.4.29/
    ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most && make && make install
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- apache2.4 install failed! ----- \e[0m"
    else
      echo -e "\e[1;32m ----- apache2.4 is installed. ----- \e[0m"
    fi
}
config_apache(){
    echo "PATH=$PATH:/usr/local/apache2.4/bin/" >> /etc/profile
    /usr/local/apache2.4/bin/apachectl start
    port=`netstat -lnp|grep httpd|wc -l`
    if [ $port -gt 0 ];then
      echo -e "\e[1;32m ----- httpd service on ----- \e[0m"
    else
      echo -e "\e[1;31m ----- httpd start failed. ----- \e[0m"
    fi
    echo -e "\e[1;32m ----- httpd config php ----- \e[0m "
    sed -i '203s/denied/granted/' /usr/local/apache2.4/conf/httpd.conf
    sed -i '/AddType application\/x-gzip .gz .tgz/a\    AddType application\/x-httpd-php .php' /usr/local/apache2.4/conf/httpd.conf
    sed -i 's/.*index.html/& index.php/' /usr/local/apache2.4/conf/httpd.conf
    sed -i 's/^#ServerName/ServerName/' /usr/local/apache2.4/conf/httpd.conf
    /usr/local/apache2.4/bin/apachectl restart
    echo -e "\e[1;32m -------- httpd config php finished.------ \e[0m"
}
install_nginx(){
    cd /usr/local/src/
    [ -d nginx-1.8.0 ] || tar -zxf nginx-1.8.0.tar.gz
    cd /usr/local/src/nginx-1.8.0/
    ./configure --prefix=/usr/local/nginx && make && make install
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- nginx is installed. ----- \e[0m"
    else
      echo -e "\e[1;32m ----- nginx is installed. ----- \e[0m"
    fi
    echo "PATH=$PATH:/usr/local/nginx/sbin/" >> /etc/profile
}
install_mysql(){
    cd /usr/local/src/
    tar -zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
    [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_bak
    mv /usr/local/src/mysql-5.6.36-linux-glibc2.5-x86_64/ /usr/local/mysql/
    useradd -s /sbin/nologin mysql
    mkdir -p /data/mysql
    chown -R mysql:mysql /data/mysql
    /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql--datadir=/data/mysql
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- mysql install failed ----- \e[0m"
    else
      echo -e "\e[1;32m ----- mysql is installed. ----- \e[0m"
    fi
}
config_mysql(){
    cd /usr/local/mysql/
    mv /etc/my.cnf/etc/my.cnf_bak
    cp support-files/my-default.cnf /etc/my.cnf
    sed -ir 's@# basedir = .....@basedir = /usr/local/mysql@' /etc/my.cnf
    sed -ir 's@# datadir = .....@datadir = /data/mysql@' /etc/my.cnf
    cp support-files/mysql.server /etc/init.d/mysqld
    chmod 755 /etc/init.d/mysqld
    sed -i 's@^datadir=@datadir=/data/mysql@' /etc/init.d/mysqld
    chkconfig --add mysqld && chkconfig mysqld on
    service mysqld start
    port=`netstat -lnp|grep 3306|wc -l`
    if [ $port -eq 1 ];then
      echo -e "\e[1;32m --- mysql service on --- \e[0m"
    fi
}
install_php(){
    cd /usr/local/src/
    [ -d php-5.6.9 ] || tar -zxf php-5.6.9.tar.gz
    cd /usr/local/src/php-5.6.9/
    ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc--with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --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 && make && make install
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- php install failed ----- \e[0m"
    else
      echo -e "\e[1;32m ----- php is installed. ----- \e[0m"
    fi
}
config_php(){
    cp /usr/local/src/php-5.6.9/php.ini-production /usr/local/php/etc/php.ini
}
install_php_fpm(){
    cd /usr/local/src/
    [ -d php-5.6.9 ] || tar -zxf php-5.6.9.tar.gz
    cd /usr/local/src/php-5.6.9/
    ./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-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl--with-openssl && make && make install
    if [ $? -ne 0 ];then
      echo -e "\e[1;31m ----- php-fpm install failed ----- \e[0m"
    else
      echo -e "\e[1;32m ----- php-fpm is installed. ----- \e[0m"
    fi
}
config_php_fpm(){
    useradd -s /sbin/nologin php-fpm
    cp /usr/local/src/php-5.6.9/php.ini-production /usr/local/php-fpm/etc/php.ini
    touch /usr/local/php-fpm/etc/php-fpm.conf
    (
    cat >/usr/local/php-fpm/etc/php-fpm.conf > /etc/profile
    echo "JAVA_BIN=/usr/local/jdk1.8/bin" >> /etc/profile
    echo "JRE_HOME=/usr/local/jdk1.8/jre" >> /etc/profile
    echo "CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar" >> /etc/profile
    echo "PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin" >> /etc/profile
    echo -e "\e[1;32m ----- jdk is installed. ----- \e[0m"
}
install_tomcat(){
    cd /usr/local/src
    [ -d apache-tomcat-8.5.29 ] || tar -zxf apache-tomcat-8.5.29.tar.gz
    [ -d /usr/local/tomcat ] && mv /usr/local/tomcat /usr/local/tomcat_bak
    mv apache-tomcat-8.5.29/ /usr/local/tomcat/
    /usr/local/tomcat/bin/startup.sh
    port=`netstat -lnp|grep java|wc -l`
    if [ $port -gt 0 ];then
      echo -e "\e[1;32m ----- tomcat is installed. ----- \e[0m"
    else
      echo -e "\e[1;31m ----- tomcat install failed. ----- \e[0m"
    fi
}
install_lamp(){
    install_apache
    install_mysql
    install_php
    install_xcache_php
}
config_lamp(){
    config_apache
    config_mysql
    config_php
}
install_lnmp(){
    install_nginx
    install_mysql
    install_php_fpm
    install_xcache_fpm
}
config_lnmp(){
    config_mysql
    config_php_fpm
}
menu(){
    echo -e "\e[1;32m ----- please input your choice: ----- \e[0m"
    cat
页: [1]
查看完整版本: LAMP、LNMP、Tomcat自动安装脚本