欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

[SHELL] LAMP一键安装脚本设计(v2.1)

showerlee 2013-11-30 22:14 Programming, SHELL 阅读 (4,080) 2条评论

这个周末刚好闲下来,终于把支持ubuntu的版本搞定,期间还算顺利.

本版本的测试环境为Ubuntu server 12.04.3 x86 & x64

在2.0版本基础上修复了一些已知BUG,有需要的朋友可以尝试在其他版本的centos和ubuntu系统版本上测试下,如有问题,请在此留言.

程序下载地址:

Onekey_LAMP_v2.1: http://www.showerlee.com/down/Onekey_lamp_v2.1.zip

以下为部分脚本内容:

# 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

INIT_DIR=/etc/init.d

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,created by showerlee.

   Version:2.1

   QQ:381362772

   BLOG:http://www.showerlee.com

   Since 2013.11.30

==================================================================

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

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

   echo "Install the Dependency package..."

   sleep 1

   OS_NAME=$(sed -n '1p' /etc/issue |awk '{print $1}')

   if [ $OS_NAME == 'CentOS' ]; then

       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*

   elif [ $OS_NAME == 'Ubuntu' ]; then

       apt-get update

       apt-get install -y  cmake gcc g++ make autoconf libltdl-dev \

libgd2-xpm-dev libfreetype6 libfreetype6-dev libxml2-dev \

libjpeg-dev libpng12-dev libcurl4-openssl-dev libssl-dev \

patch libmcrypt-dev libmhash-dev libncurses5-dev  \

libreadline-dev bzip2 libcap-dev ntpdate diffutils \

exim4 iptables unzip sudo

# 安装chkconfig

dpkg -i $DEFAULT_DIR/src/chkconfig_11.0-79.1-2_all.deb

ln -s /usr/lib/insserv/insserv /sbin/insserv >> $IN_LOG 2>&1

   else

       echo "unknown system,quit..."

       exit 0

   fi

   # 关闭相关服务和SELINUX

   echo "Stop useless service..."

   sleep 1

   if [ $OS_NAME == 'CentOS' ]; then

       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

   elif [ $OS_NAME == 'Ubuntu' ]; then

       iptables -F >> $IN_LOG 2>&1

       iptables-save >> $IN_LOG 2>&1

   else

       echo "unknown system,quit..."

       exit 0

   fi

   chkconfig httpd off 2>/dev/null

   chkconfig mysql off 2>/dev/null

   service httpd stop 2>/dev/null

   service mysql stop 2>/dev/null      

   # 判断能否访问公网

   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

   # 同步时间

   echo "synchronize time..."

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

   hwclock -w

   echo "finish check..."

   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

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   fi

   make >> $IN_LOG 2>&1

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   fi

   make install >> $IN_LOG 2>&1

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   else

       echo "compilation finish..."

   fi

   # 判断目录是否创建:

   if [ ! -d $APACHE_DIR ];then

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

       exit 0

   fi

   # mysql配置项:

   echo "configuration after the installation...."

   sleep 1

   # 防止apache启动报错.

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

   # 设置开机启动

   # 注:ubuntu添加启动项需插入到 exit 0 前

   if [ $OS_NAME == 'CentOS' ]; then

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

   elif [ $OS_NAME == 'Ubuntu' ]; then

       sed -i "/^exit 0/ i\\$APACHE_DIR\/bin\/apachectl start" /etc/rc.local

   else

       echo "unknown system,quit..."

       exit 0

   fi    

   # 启动apache

   echo "start apache httpd service..."

   sleep 1

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

   # 设置环境变量

   echo 'PATH=$PATH:/usr/local/apache2/bin;export PATH' >> /etc/profile

   source /etc/profile >> $IN_LOG 2>&1

   # 判断服务是否启动

   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 been 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

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   fi

   make >> $IN_LOG 2>&1

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   fi

   make install >> $IN_LOG 2>&1

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   else

      echo "compilation finish..."

   fi

   # 判断目录是否创建:

   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启动脚本到系统服务目录

   [ -e $INIT_DIR/mysqld ] && mv $INIT_DIR/mysqld $INIT_DIR/mysqld.old >> $IN_LOG 2>&1

   cp $MYSQL_DIR/support-files/mysql.server $INIT_DIR/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:/usr/local/mysql/bin;export PATH' >> /etc/profile

   source /etc/profile >> $IN_LOG 2>&1

   # 判断服务是否启动

   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 been 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

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   fi

   make >> $IN_LOG 2>&1

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   fi

   make install >> $IN_LOG 2>&1

   if [[ $? != 0 ]]; then

        echo "error in the compilation,stop.."

        exit 0

   else

       echo "compilation finish..."

   fi

   # 判断目录是否创建:

   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 been installed correctly!"

   fi

}

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

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/1005

继续浏览:LAMP APACHE PHP MYSQL

2条大神的评论

loading
  1. 沙发
    uuus0072014年9月18日下午5:26 回复

    :grin: :grin: 好羞射,文章真的好赞啊,顶博主!

    :x

  2. 板凳
    Rocker2014年11月23日上午9:09 回复

    thanks for your sharing…

    :grin: :grin: 好羞射,文章真的好赞啊,顶博主!

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif