天天看点

lnmp一键安装脚本

#!/bin/bash
# name: Install nginx service.
#
# user
path="/server/packages"
nginx_package="nginx-1.17.10"
mysql_user=mysql
mysql_package=mysql-5.5.62
mysql_path=/usr/local/mysql
php_package="php-5.6.38"

nginx_init(){
    if [ ! -d $path ];then
        mkdir -p $path
    fi
    if [ ! -e ${path}/${nginx_package}.tar.gz ];then
        wget -P ${path}/ http://172.16.0.7/packages/${nginx_package}.tar.gz &>/dev/null
    fi
}
nginx_install(){
    id nginx &>/dev/null
    [ $? -ne 0 ]&&{
        useradd -r -s /sbin/nologin nginx &>/dev/null
    }
    yum install -y gcc* pcre-devel net-tools opensll lsof openssl-devel zlib-devel ncurses-devel cmake bison libxml2-devel libpng-devel &>/dev/null
    [ $? -ne 0 ]&&{
        echo "Installation dependency failed."
        exit 1
    }
    tar zxf /server/packages/${nginx_package}.tar.gz -C /server/packages/ &>/dev/null
    if [ $? -eq 0 ]
    then
        echo "Installing..."
        cd /server/packages/nginx-1.17.10
        ./configure --prefix=/usr/local/nginx \
        --user=nginx --group=nginx \
        --with-http_stub_status_module \
        --with-http_ssl_module &>/dev/null
        if [ $? -eq 0 ];then
            make &> /dev/null
            make install &> /dev/null
            [ $? -eq 0 ]||{
                echo "nginx install failed."
                exit 1
            }
        else
            echo -e "\033[31mconfigure falied.\033[0m"
        fi
        echo -e "\033[32mnginx install success.\033[0m"
    fi
}
#mysql_user=mysql
#mysql_package=mysql-5.5.62
#mysql_path=/usr/local/mysql
mysql_init(){
    if [ ! -e /server/packages/${mysql_package}.tar.gz ];then
        wget -P /server/packages/ http://172.16.0.7/packages/${mysql_package}.tar.gz &>/dev/null
    fi
    id ${mysql_user} &>/dev/null
    [ $? -ne 0 ]&&{
        useradd -r -s /sbin/nologin ${mysql_user} &>/dev/null
        if [ $? -eq 0 ];then
            echo "user: ${mysql_user} create success."
        else
            echo "user: ${mysql_user} create failed,please check it!"
            exit 1
        fi
    }
}
mysql_install(){
    tar zxf /server/packages/${mysql_package}.tar.gz -C /server/packages/ &>/dev/null
    if [ $? -eq 0 ];then
        echo "Installing..."
        cd /server/packages/${mysql_package}/
        cmake -DCMAKE_INSTALL_PREFIX=${mysql_path} \
        -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DEXTRA_CHARSETS=all \
        -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
        -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 \
        -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=${mysql_user} \
        -DMYSQL_TCP_PORT=3306 &>/dev/null
        if [ $? -eq 0 ];then
            make &>/dev/null
            make install &>/dev/null
            [ $? -ne 0 ]&&{
                echo -e "\033[31minstall failed,please check.\033[0m"
                exit 1
            }||{
               echo "install success."
               ln -s ${mysql_path}/bin/* /usr/local/bin/ &>/dev/null
               }
        fi
        cd ${mysql_path}/
        chown -R root .
        chown -R mysql data/
    fi
    echo "configure mysql..."
    \cp /server/packages/${mysql_package}/support-files/my-medium.cnf /etc/my.cnf &>/dev/null
    cd ${mysql_path}
    ./scripts/mysql_install_db --user=mysql &>/dev/null
    if [ $? -eq 0 ];then
        \cp ${mysql_path}/support-files/mysql.server /etc/init.d/mysqld &>/dev/null
        chmod +x /etc/init.d/mysqld
        chkconfig --add mysqld &>/dev/null
        chkconfig mysqld on &>/dev/null
        service mysqld start
        mysqladmin -uroot password 123456
        [ $? -eq 0 ]&&{
        echo "mysql configure success."
        }||{
        echo -e "\033[31mconfigure falied,please check it.\033[0m"
           }
    fi
}
#php_package="php-5.6.38"
php_init(){
    id www &>/dev/null
    if [ $? -ne 0 ];then
    groupadd www
    if [ ! -e ${path}/${php_package}.tar.gz ];then
        wget -P ${path}/ http://172.16.0.7/packages/${php_package}.tar.gz &>/dev/null
        if [ $? -ne 0 ];then
        echo "download php package failed."
            exit 1
        fi
    fi
    fi
}
php_install(){
    tar zxf ${path}/${php_package}.tar.gz -C ${path}/ &>/dev/null
    if [ $? -eq 0 ];then
        echo "Installing..."
        cd ${path}/${php_package}/
        ./configure \
        --prefix=/usr/local/php/ \
        --with-config-file-path=/usr/local/php/etc/ \
        --with-mysqli=/usr/local/mysql/bin/mysql_config \
        --enable-soap --enable-mbstring=all \
        --enable-sockets \
        --with-pdo-mysql=/usr/local/mysql \
        --with-gd --without-pear --enable-fpm &>/dev/null
        if [ $? -eq 0 ];then
            make &>/dev/null
            make install &>/dev/null
            [ $? -ne 0 ]&&{
                echo -e "\033[31minstall failed,please check.\033[0m"
                exit 1
            }||{
                   echo "install success."
           }
        else
            echo "configure failed"
            exit 1
        fi
    else
        exit 2
    fi
}
php_config(){
echo "configure php..."
\cp -a ${path}/${package}/php.ini-production /usr/local/php/etc/php.ini
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
}
starts(){
    id www &>/dev/null
    [ $? -ne 0 ]&&{
        useradd -u 1111 www -g www  &>/dev/null
    }
    wget -O /usr/local/nginx/conf/nginx.conf http://172.16.0.7/profile/nginx.conf &>/dev/null
    wget -O /usr/local/php/etc/php-fpm.conf http://172.16.0.7/profile/php-fpm.conf &>/dev/null
    wget -O ${path}/nginx http://172.16.0.7:/start/nginx &>/dev/null
    \cp ${path}/nginx /etc/init.d/
    chkconfig --add nginx
    chkconfig nginx on
    if [ $? -eq 0 ];then
         \cp ${path}/nginx /usr/bin/
         chmod +x /usr/bin/nginx
         nginx start
         if [ $? -eq 0 ];then
             echo "nginx is started."
         fi
    fi
}
mon(){
    wget -O ${path}/nginx/mon-nginx.sh http://172.16.0.7/scripts/mon-nginx.sh &>/dev/null
    echo "/bin/sh /usr/local/nginx/mon-nginx.sh" >>/etc/rc.local
    echo "*/5 * * * * /bin/sh /usr/local/nginx/mon-nginx.sh" >>/var/spool/cron/root
    chmod +x /etc/rc.d/rc.local
}
main(){
    nginx_init
    nginx_install
    mysql_init
    mysql_install
    php_init
    php_install
    php_config
    starts
    mon
}
main           

继续阅读