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

[SHELL] LNMP一键安装脚本设计(v1.0)

showerlee 2013-12-02 18:22 Programming, SHELL 阅读 (3,867) 1条评论

周末奋战了2天,终于把LNMP的centos和ubuntu双系统的一键安装脚本搞定,晚上可以好好休息休息了.

本版本的测试环境为Ubuntu server 12.10和Centos6.3的x86和x64

欢迎前来测试...

程序下载地址:

Onekey_LNMP_v1.0: http://www.showerlee.com/down/Onekey_lnmp_v1.0.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

NGINX_DIR=/usr/local/nginx

PHP_DIR=/usr/local/php5

. lib/check_env.sh

. lib/mysql.sh

. lib/nginx.sh

. lib/php.sh

echo "=================================================================

   Welcome to Onekey LNMP installation,created by showerlee.

   Version:1.0

   QQ:381362772

   BLOG:http://www.showerlee.com

   Since 2013.12.1

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

Select option for your choice.

   1 install all service(nginx + php + mysql)

   2 install nginx+php

   3 install nginx

   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

    nginx_ins

    php_ins

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

    check_env_ins

    nginx_ins

    php_ins

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

    check_env_ins

    nginx_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 gcc-c++ ncurses ncurses-devel cmake \

   make perl bison openssl openssl-devel pcre-devel gcc* libxml2 \

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

   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 openssl \

libreadline-dev bzip2 libcap-dev ntpdate diffutils \

exim4 iptables unzip sudo libpcre3 libpcre3-dev

   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

       chkconfig httpd off >> $IN_LOG 2>&1

       chkconfig mysql off >> $IN_LOG 2>&1

       service httpd stop >> $IN_LOG 2>&1

       service mysql stop >> $IN_LOG 2>&1

       sleep 1

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

       iptables -F >> $IN_LOG 2>&1

       iptables-save >> $IN_LOG 2>&1

       update-rc.d -f nginx remove >> $IN_LOG 2>&1

       update-rc.d -f mysql remove >> $IN_LOG 2>&1

       $INIT_DIR/nginx stop >> $IN_LOG 2>&1

       $INIT_DIR/mysql stop >> $IN_LOG 2>&1

   else

       echo "unknown system,quit..."

       exit 0

   fi

   # 判断能否访问公网

   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/nginx.sh

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

# nginx install function

function nginx_ins {  

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

   echo "Install the Nginx service..."

   sleep 1

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

   echo "The initial configuration before installation..."

   sleep 1

   # 添加nginx用户和用户组

   groupadd nginx >> $IN_LOG 2>&1

   useradd -g nginx -s /bin/false -M nginx >> $IN_LOG 2>&1

   # nginx安装

   echo "make install nginx package..."

   sleep 1

   cd $DEFAULT_DIR/src

   tar -zxvf nginx-1.4.1.tar.gz >> $IN_LOG 2>&1

   cd nginx-1.4.1

    ./configure --prefix=$NGINX_DIR --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-debug --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi >> $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 $NGINX_DIR ];then

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

       exit 0

   fi

   # nginx配置项:

   echo "configuration after the installation...."

   sleep 1

   # 创建缓存目录:

   mkdir -p /var/tmp/nginx/client >> $IN_LOG 2>&1

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

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

       # 创建nginx启动脚本

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

   cd $DEFAULT_DIR

   cp conf/nginx $INIT_DIR/  >> $IN_LOG 2>&1

   chmod 755 $INIT_DIR/nginx >> $IN_LOG 2>&1

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

   # 启动nginx并设置开机启动    

   echo "start nginx service..."

   sleep 1

   service nginx start >> $IN_LOG 2>&1

   chkconfig nginx on