最近一直在学习PYTHON,抽空在闲暇时间利用SHELL编写了一个LAMP一键安装脚本,目前分v1无人值守版本和v2用户自定义版本,下面把脚本的一些设计思路和部分SHELL脚本写了下来,供大家参考.

   目前版本的测试环境为CentOS6.3 x86和x64版本,后期会针对ubuntu系统环境以及LNMP架构进行更新.


   开始正题:

   v1版本很简单,项目下分conf和src目录,分别放置相关配置文件和LAMP编译包,以及4个shell安装脚本,无人值守情况下,运行相应脚本文件即可完成安装.

如图:

[SHELL]  LAMP一键安装脚本设计(v1,v2)_lamp

v2版本相对比较系统:

   conf目录放置配置文件

   lib目录放置安装具体服务的SHELL脚本(apache,mysql,php等),脚本内为相应的方法,供主脚本调用

   log目录放置安装日志

   src目录放置LAMP具体编译包

   install_lamp.sh脚本为主配置脚本,运行后会提供给用户可选择安装界面,供用户自定义安装

如图:

[SHELL]  LAMP一键安装脚本设计(v1,v2)_lamp_02

[SHELL]  LAMP一键安装脚本设计(v1,v2)_一键安装脚本设计_03

以下介绍v2版本的具体脚本内容,具体安装包下载地址如下:

Onekey_LAMP_v1: http://www.showerlee.com/down/lamp_v1.zip

Onekey_LAMP_v2: http://www.showerlee.com/down/lamp_v2.zip


注:系统编码使用UTF-8,加载mysql与apache的环境变量

  apache编译包版本为2.4.4,DocumentRoot路径/usr/local/apache2/htdocs,加载https加密通道.

  mysql编译包版本为5.6.13,库文件路径/usr/local/mysql/data.

  mysql安装初始账户密码为root/123456

  php编译包版本为5.4.13,默认加载了笔者线上常用的模块

# cat install_lamp.sh

----------------------------------------------------

#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin;

export PATH


# LAMP目录

DEFAULT_DIR=$(pwd)

LOG_DIR=$(pwd)/log

MYSQL_DIR=/usr/local/mysql

APACHE_DIR=/usr/local/apache2

PHP_DIR=/usr/local/php5

PCRE_DIR=/usr/local/pcre


. lib/check_env.sh

. lib/mysql.sh

. lib/apache.sh

. lib/php.sh


echo "Welcome to Onekey LAMP installation,select option for your choice.

   1 install all service(apache + php + mysql)

   2 install apache+php

   3 install apache

   4 install mysql

   5 quit"

sleep 0.1

read -p "Please Input 1,2,3,4,5: " SERVER_ID

if [[ $SERVER_ID == 1 ]]; then

    check_env_ins

    mysql_ins

    apache_ins

    php_ins

elif [[ $SERVER_ID == 2 ]]; then

    check_env_ins

    apache_ins

    php_ins

elif [[ $SERVER_ID == 3 ]]; then

    check_env_ins

    apache_ins

elif [[ $SERVER_ID == 4 ]]; then

    check_env_ins

    mysql_ins

else

   exit

fi


-------------------------------------------------------


# cat lib/check_env.sh

----------------------------------------------------

# check the system environment

function check_env_ins {

   local IN_LOG=$LOG_DIR/check_env_install-$(date +%F).log

   echo "check the system environment..."

   sleep 1  


   # 判断是否为root用户

   if [ $UID != 0 ]; then

       echo "You must be root to run the install script."

       exit 0

   fi


   # 关闭相关服务和SELINUX

   echo "Stop useless service..."

   sleep 1

   chkconfig httpd off 2>/dev/null

   chkconfig mysql off 2>/dev/null

   service httpd stop 2>/dev/null

   service mysql stop 2>/dev/null

   iptables -F >> $IN_LOG 2>&1

   service iptables save 2>/dev/null

   setenforce 0 >> $IN_LOG 2>&1

   sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config >> $IN_LOG 2>&1

   sleep 1


   # 判断能否访问公网

   echo 8.8.8.8 >> /etc/resolv.conf >> $IN_LOG 2>&1

   echo "Check your Networking..."

   NET_ALIVE=$(ping 8.8.8.8 -c 5 |grep 'received'|awk 'BEGIN {FS=","} {print $2}'|awk '{print $1}')

   if [ $NET_ALIVE == 0 ]; then

      echo "Network is not active,please check your network configuration!"

      exit 0

   else

      echo "Network is active,continue.."

      sleep 1

   fi


   # 安装开发包(使用默认CENTOS更新源):

   echo "Install the Dependency package..."

   sleep 1

   yum -y install lsof wget gcc-c++ ncurses ncurses-devel cmake \

make perl bison openssl openssl-devel gcc* libxml2 \

libxml2-devel curl-devel libjpeg* libpng* freetype*


   # 同步时间

   echo "synchronization time..."

   ntpdate tiger.sina.com.cn >> $IN_LOG 2>&1

   hwclock -w

   sleep 1

}

----------------------------------------------------


# cat lib/apache.sh

----------------------------------------------------

# apache install function

function apache_ins {  

   local IN_LOG=$LOG_DIR/apache_install-$(date +%F).log

   echo "Install the Apache service..."

   sleep 1


   # pcre依赖包安装

   echo "install the dependency pcre package..."

   sleep 1

   cd $DEFAULT_DIR/src

   tar -xzvf pcre-8.32.tar.gz >> $IN_LOG 2>&1

   cd pcre-8.32

   ./configure --prefix=$PCRE_DIR >> $IN_LOG 2>&1

   make >> $IN_LOG 2>&1

   make install >> $IN_LOG 2>&1


   # apache安装

   echo "install apache package..."

   sleep 1

   # 注:httpd-2.4.3-deps.tar.bz2已集成APR,安装apache前检查pcre是否安装成功.

   cd $DEFAULT_DIR/src

   tar jxvf httpd-2.4.4.tar.bz2 >> $IN_LOG 2>&1

   tar jxvf httpd-2.4.3-deps.tar.bz2 >> $IN_LOG 2>&1

   cp -rf httpd-2.4.3/* httpd-2.4.4

   cd httpd-2.4.4

    ./configure --prefix=$APACHE_DIR --enable-so --enable-rewrite \

        -enable-ssl=static -with-ssl=/usr/local/ssl -enable-mods-shared=all \

        --with-pcre=$PCRE_DIR/bin/pcre-config >> $IN_LOG 2>&1

   make >> $IN_LOG 2>&1

   make install >> $IN_LOG 2>&1


   # 判断目录是否创建:

   if [ ! -d $APACHE_DIR ];then

       echo "$APACHE_DIR is not exist,stop..."

       exit 0

   fi


   # apache配置项:

   echo "configuration after the installation...."

   sleep 1

   # 防止apache启动报错.

   echo "ServerName localhost:80" >> $APACHE_DIR/conf/httpd.conf

   # 设置开机启动

   echo "$APACHE_DIR/bin/apachectl start" >> /etc/rc.local


   # 启动apache

   echo "start apache httpd service..."

   sleep 1

   $APACHE_DIR/bin/apachectl start  >> $IN_LOG 2>&1


    # 设置环境变量

   echo "PATH=$PATH:$APACHE_DIR/bin;export PATH" >> /etc/profile

   source /etc/profile


   # 判断服务是否启动

   PORT_80=$(lsof -i:80|wc -l)

   if [ $PORT_80 == 0 ]; then

      echo "Apache httpd service is not active,please check your configure!"

      exit 0

   else

      echo "Congratulation,Apache httpd service has installed correctly!"

   fi

}


----------------------------------------------------


# cat lib/mysql.sh

----------------------------------------------------

# mysql install function

function mysql_ins {

   local IN_LOG=$LOG_DIR/mysql_install-$(date +%F).log

   echo "Install the MySQL service..."

   sleep 1


   # 安装前的初始配置工作:

   echo "The initial configuration before installation..."

   sleep 1

   mkdir -p $MYSQL_DIR >> $IN_LOG 2>&1

   useradd -d $MYSQL_DIR mysql >> $IN_LOG 2>&1

   mkdir -p $MYSQL_DIR/data >> $IN_LOG 2>&1      

   mkdir -p $MYSQL_DIR/log >> $IN_LOG 2>&1          

   chown -R mysql:mysql $MYSQL_DIR/data/ >> $IN_LOG 2>&1

   chown -R mysql:mysql $MYSQL_DIR/log/ >> $IN_LOG 2>&1

   chmod 750 $MYSQL_DIR/data >> $IN_LOG 2>&1    

   chmod 750 $MYSQL_DIR/log >> $IN_LOG 2>&1      


   # 解包编译安装:

   echo "make install the MySQL package..."

   sleep 1

   cd $DEFAULT_DIR

   cd src/

   tar -zxvf mysql-5.6.13.tar.gz >> $IN_LOG 2>&1  

   cd mysql-5.6.13  

   cmake -DCMAKE_INSTALL_PREFIX=$MYSQL_DIR \

   -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

   -DDEFAULT_CHARSET=gbk \

   -DDEFAULT_COLLATION=gbk_chinese_ci \

   -DEXTRA_CHARSETS=all \

   -DWITH_MYISAM_STORAGE_ENGINE=1 \

   -DWITH_INNOBASE_STORAGE_ENGINE=1 \

   -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

   -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

   -DWITH_MEMORY_STORAGE_ENGINE=1 \

   -DWITH_FEDERATED_STORAGE_ENGINE=1 \

   -DWITH_READLINE=1 \

   -DENABLED_LOCAL_INFILE=1 \

   -DMYSQL_DATADIR=$MYSQL_DIR/data \

   -DMYSQL_USER=mysql \

   -DMYSQL_TCP_PORT=3306 \

   -DSYSCONFDIR=/etc \

   -DWITH_SSL=yes >> $IN_LOG 2>&1


   make >> $IN_LOG 2>&1

   make install >> $IN_LOG 2>&1


   # 判断目录是否创建:

   if [ ! -d $MYSQL_DIR ];then

       echo "$MYSQL_DIR is not exist,stop..."

       exit 0

   fi


   # mysql配置项:

   echo "configuration after the installation...."

   sleep 1

   [ -e /etc/my.cnf ] && rm -rf /etc/my.cnf >> $IN_LOG 2>&1

   cd $DEFAULT_DIR

   cp conf/my.cnf /etc/my.cnf >> $IN_LOG 2>&1


   # 将mysql的库文件路径加入系统的库文件搜索路径中

   ln -s $MYSQL_DIR/lib/mysql /usr/lib/mysql >> $IN_LOG 2>&1


   # 输出mysql的头文件到系统头文件

   ln -s $MYSQL_DIR/include/mysql /usr/include/mysql >> $IN_LOG 2>&1


   # 进入安装路径,初始化配置脚本

   echo "Initialize the configuration of the MySQL..."

   sleep 1

   cd $MYSQL_DIR

   scripts/mysql_install_db --user=mysql --datadir=$MYSQL_DIR/data >> $IN_LOG 2>&1


   # 复制mysql启动脚本到系统服务目录

   cp $MYSQL_DIR/support-files/mysql.server /etc/init.d/mysqld >> $IN_LOG 2>&1


   # 系统启动项相关配置

   chkconfig --add mysqld  >> $IN_LOG 2>&1

   chkconfig --level 35 mysqld on >> $IN_LOG 2>&1


   # 启动mysql

   service mysqld start >> $IN_LOG 2>&1


   # 配置权限

   echo "Configure MySQL authority..."

   sleep 1

   $MYSQL_DIR/bin/mysqladmin -u root password 123456 >> $IN_LOG 2>&1

   #给root用户非本地链接所有权限,并改密码和赋予其给其他人下发权限.

   $MYSQL_DIR/bin/mysql -u root -p123456 -e "grant all privileges on *.* to root@'%' identified by '123456' with grant option;" >> $IN_LOG 2>&1

   $MYSQL_DIR/bin/mysql -u root -p123456 -e "grant all privileges on *.* to root@'localhost' identified by '123456' with grant option;" >> $IN_LOG 2>&1


   # 设置环境变量

   echo "PATH=$PATH:$MYSQL_DIR/bin;export PATH" >> /etc/profile

   source /etc/profile


   # 判断服务是否启动

   PORT_3306=$(lsof -i:3306|wc -l)

   if [ $PORT_3306 == 0 ]; then

      echo "MySQL service is not active,please check your configure!"

      exit 0

   else

      echo "Congratulation,MySQL service has installed correctly!"

   fi

}

----------------------------------------------------


# cat lib/php.sh

----------------------------------------------------

# php install function

function php_ins {

   local IN_LOG=$LOG_DIR/php_install-$(date +%F).log

   echo "Install the PHP module for apache..."

   sleep 1


   cd $DEFAULT_DIR/src

   tar -jxvf php-5.4.13.tar.bz2 >> $IN_LOG 2>&1

   cd php-5.4.13

   # 此处编译安装了我们项目经常用到的PHP模块,如有其它需要可以自定义添加.

   ./configure --prefix=$PHP_DIR --with-apxs2=$APACHE_DIR/bin/apxs \

    --with-libxml-dir=/usr/local/lib --with-zlib-dir=/usr/local/lib \

    --with-mysql=$MYSQL_DIR --with-mysqli=$MYSQL_DIR/bin/mysql_config \

    --with-gd --enable-soap --enable-sockets --enable-xml --enable-mbstring \

    --with-png-dir=/usr/local --with-jpeg-dir=/usr/local --with-curl=/usr/lib \

    --with-freetype-dir=/usr/include/freetype2/freetype/ --enable-bcmath \

    --enable-calendar --enable-zip --enable-maintainer-zts >> $IN_LOG 2>&1

   make >> $IN_LOG 2>&1

   make install >> $IN_LOG 2>&1


   # 判断目录是否创建:

   if [ ! -d $PHP_DIR ];then

       echo "$PHP_DIR is not exist,stop..."

       exit 0

   fi


   # PHP配置项:

   echo "configuration after the installation...."

   sleep 1

   cp php.ini-development $PHP_DIR/lib/php.ini

   echo "AddType application/x-httpd-php .php" >> $APACHE_DIR/conf/httpd.conf

   sed -i '/DirectoryIndex index.html/s/$/ &index.php/g' $APACHE_DIR/conf/httpd.conf

   # 隐藏PHP版本信息:

   # echo "expose_php = Off" >> $PHP_DIR/lib/php.ini

   # 关闭警告及错误信息,爆路径:

   echo "display_errors = Off" >> $PHP_DIR/lib/php.ini

   # 调整时区,防止phpinfo()函数报错.

   echo "date.timezone =PRC" >> $PHP_DIR/lib/php.ini

   # 开启php错误日志并设置路径.

   echo "log_errors = On" >> $PHP_DIR/lib/php.ini

   echo "error_log = $APACHE_DIR/logs/php_error.log"  >> $PHP_DIR/lib/php.ini


   cd $DEFAULT_DIR

   cp conf/info.php $APACHE_DIR/htdocs >> $IN_LOG 2>&1


   # 重启apache:

   echo "restart apache to load the php module..."

   $APACHE_DIR/bin/apachectl stop >> $IN_LOG 2>&1

   $APACHE_DIR/bin/apachectl start >> $IN_LOG 2>&1


   # 判断PHP是否加载:

   PHP_LOAD=$(curl --head http://localhost/info.php |grep PHP |wc -l)

   if [ $PHP_LOAD == 0 ]; then

      echo "PHP does not load,please check your configure!"

      exit 0

   else

      echo "Congratulation,PHP module has installed correctly!"

   fi

}

----------------------------------------------------