shell学习脚本之一键安装LAMP

原创 浪子丶刘少  2019-06-11 09:20  阅读 1,460 次 评论 0 条 百度未收录
#!/bin/bash
#auto install lamp
#by anthors liushao 2019

#LAMP安装日志
LOG_FILE=/mnt/lamp.log

#定义 http 相关变量路径
 H_FILES=httpd-2.4.39.tar.gz
H_FILES_DIR=httpd-2.4.39
H_URL=https://mirrors.cnnic.cn/apache/httpd/
H_PREFIX=/usr/local/apache2/

#定义 MySQL 相关变量路径
M_FILES=mysql-5.7.26.tar.gz
M_FILES_DIR=mysql-5.7.26
M_URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.7/
M_PREFIX=/usr/local/mysql/

#定义 PHP 相关变量路径
P_FILES=php-7.3.6.tar.gz
P_FILES_DIR=php-7.3.6
P_URL=https://www.php.net/distributions/
P_PREFIX=/usr/local/php7/

if [ -z $1 ]; then
    echo -e '\033[32m--------------------------------------------------\033[0m'
    echo -e '\033[35m请选择你要安装的服务\033[0m'
    echo -e '\033[32m--------------------------------------------------\033[0m'
    echo '1)安装httpd服务'
    echo '2)安装mysql服务'
    echo '3)安装php服务'
    echo '4)配置 index.php 启动LAMP'
    echo -e '\033[32m--------------------------------------------------\033[0m'
    echo "使用方法:  /bin/bash $0 1|2|3|4|help "
    echo -e '\033[32m--------------------------------------------------\033[0m'
    exit
else
    #安装编译安装所需常用软件
    yum install gcc apr* pcre* gcc-c++ -y    
fi

#安装httpd从这里开始
if [[ $1 -eq 1 ]]; then

#下载httpd安装包文件
wget -c  $H_URL/$H_FILES
if [ $? -eq 0 ]; then
    echo "$DATA The $H_FILES download Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $H_FILES download Failed,Please check!\033[0m" 
    echo "$DATA The $H_FILES download Failed ..."  >> $LOG_FILE
    exit
fi

#解压http安装包并进入
    tar -xzvf $H_FILES  && cd $H_FILES_DIR
if [ $? -eq 0 ]; then
    echo "$DATA The $H_FILES decompression Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $H_FILES decompression Failed,Please check!\033[0m" 
    echo "$DATA The $H_FILES decompression Failed ..."  >> $LOG_FILE
    exit
fi

#编译http安装包
./configure --prefix=$H_PREFIX
if [ $? -eq 0 ]; then
    echo "$DATA The $H_FILES compile Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $H_FILES compile Failed,Please check!\033[0m" 
    echo "$DATA The $H_FILES compile Failed ..."  >> $LOG_FILE
    exit
fi

#编译安装http
make && make install
if [ $? -eq 0 ]; then
    echo "$DATA The $H_FILES make install Successfully!"  >> $LOG_FILE
    echo -e "\033[32mThe $H_FILES_DIR Install Successfully !\033[0m"
else
    echo -e "\033[32mThe $H_FILES make install Failed,Please check!\033[0m" 
    echo "$DATA The $H_FILES make install Failed ..."  >> $LOG_FILE
    exit
fi
fi
#安装httpd从这里结束

#安装mysql从这里开始
if [[ $1 -eq 2 ]]; then

#下载 MYSQL 安装包文件
wget -c  $M_URL/$M_FILES
if [ $? -eq 0 ]; then
    echo "$DATA The $M_FILES download Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $M_FILES download Failed,Please check!\033[0m" 
    echo "$DATA The $M_FILES download Failed ..."  >> $LOG_FILE
    exit
fi

#解压MYSQL安装包并进入
tar -xzvf $M_FILES  && cd $M_FILES_DIR
if [ $? -eq 0 ]; then
    echo "$DATA The $M_FILES decompression Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $M_FILES decompression Failed,Please check!\033[0m" 
    echo "$DATA The $M_FILES decompression Failed ..."  >> $LOG_FILE
    exit
fi

#安装编译安装所需常用软件
yum install cmake bison ncurses ncurses-devel -y

#编译 MYSQL 安装包
cmake . -DCMAKE_INSTALL_PREFIX=$M_PREFIX \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost \
-DWITH_DEBUG=0

if [ $? -eq 0 ]; then
    echo "$DATA The $M_FILES compile Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $M_FILES compile Failed,Please check!\033[0m" 
    echo "$DATA The $M_FILES compile Failed ..."  >> $LOG_FILE
    exit
fi

#编译安装MYSQL
make && make install
if [ $? -eq 0 ]; then
    echo "$DATA The $M_FILES make install Successfully!"  >> $LOG_FILE
    echo -e "\033[32mThe $M_FILES_DIR Install Successfully !\033[0m"
else
    echo -e "\033[32mThe $M_FILES make install Failed,Please check!\033[0m" 
    echo "$DATA The $M_FILES make install Failed ..."  >> $LOG_FILE
    exit
fi

#设置启动相关
#/bin/cp support-files/my-small.cnf /etc/my.cnf
/bin/cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

fi
#安装 MYSQL 从这里结束

#安装 PHP 从这里开始
if [[ $1 -eq 3 ]]; then

#下载 PHP 安装包文件
wget -c  $P_URL/$P_FILES
if [ $? -eq 0 ]; then
    echo "$DATA The $P_FILES download Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $P_FILES download Failed,Please check!\033[0m" 
    echo "$DATA The $P_FILES download Failed ..."  >> $LOG_FILE
    exit
fi

#解压 PHP 安装包并进入
    tar -xzvf $P_FILES  && cd $P_FILES_DIR
if [ $? -eq 0 ]; then
    echo "$DATA The $P_FILES decompression Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $P_FILES decompression Failed,Please check!\033[0m" 
    echo "$DATA The $P_FILES decompression Failed ..."  >> $LOG_FILE
    exit
fi

#编译 PHP 安装包
./configure --prefix=$P_PREFIX \
--with-config-file-path=$P_PREFIX/etc \
--with-mysql=$M_PREFIX \
--with-apxs2=$H_PREFIX/bin/apxs

if [ $? -eq 0 ]; then
    echo "$DATA The $P_FILES compile Successfully!"  >> $LOG_FILE
else
    echo -e "\033[32mThe $P_FILES compile Failed,Please check!\033[0m" 
    echo "$DATA The $P_FILES compile Failed ..."  >> $LOG_FILE
    exit
fi

#编译安装php
make ZEND_EXTRA_LIBS='-liconv' && make install
if [ $? -eq 0 ]; then
    echo "$DATA The $P_FILES make install Successfully!"  >> $LOG_FILE
    echo -e "\033[32mThe $P_FILES_DIR Install Successfully !\033[0m"
else
        echo -e "\033[32mThe $P_FILES make install Failed,Please check!\033[0m" 
    echo "$DATA The $P_FILES make install Failed ..."  >> $LOG_FILE
    exit
fi
fi
#安装 PHP 从这里结束

#配置 index.php 启动LAMP
if [[ $1 -eq 4 ]]; then
    sed -i 'DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf
    $H_PREFIX/bin/apachectl restart
    IP=`ifconfig | grep "inet" | awk '{print $2}' | head -n1 | cut -d ":" -f1 `
    echo -e "\033[32mYou can access http://$IP/\033[0m"
cat > $H_PREFIX/htdocs/index.php << EOF

EOF
fi
#配置 index.php 结束
本文地址:https://www.ezliushao.com/208.html
加入我们:请加入刘少技术博客交流群:扫描二维码刘少技术博客的QQ交流群 | 浪子丶刘少 QQ:1150110267(注:微信暂停添加好友)
版权声明:本文为原创文章,版权归 浪子丶刘少 所有,欢迎分享本文,转载请保留出处!

发表评论


表情