[SHELL]LNMP一鍵安裝指令碼設計(v1.0)

技術小胖子發表於2017-11-07

週末奮戰了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 libtool*

   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 >> $IN_LOG 2>&1

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

   # 建立nginx啟動指令碼

   cd $DEFAULT_DIR

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

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

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

   # 啟動nginx並設定開機啟動    

   echo “start nginx service…”

   sleep 1

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

   else

       echo “unknown system,quit…”

       exit 0

   fi


   # 設定環境變數

   echo `PATH=$PATH:/usr/local/nginx/sbin;export PATH` >> /etc/profile

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


   # 判斷服務是否啟動

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

   if [ $PORT_80 == 0 ]; then

      echo “Nginx service is not active,please check your configure!”

      exit 0

   else

      echo “Congratulation,Nginx 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


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

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

   # 複製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

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

   # 複製mysql啟動指令碼到系統服務目錄

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

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

   # 系統啟動項相關配置

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

   update-rc.d -f mysqld defaults >> $IN_LOG 2>&1

   # 啟動mysql

   $INIT_DIR/mysqld start >> $IN_LOG 2>&1

   else

       echo “unknown system,quit…”

       exit 0

   fi


   # 配置許可權

   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 nginx…”

   sleep 1


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

   echo “The initial configuration before installation…”

   sleep 1

   # 新增php使用者組

   groupadd nobody >> $IN_LOG 2>&1


   # php安裝

   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 –enable-fastcgi –enable-fpm –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-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

   cd $DEFAULT_DIR/src/php-5.4.13

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

   # 隱藏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 = $NGINX_DIR/logs/php_error.log”  >> $PHP_DIR/lib/php.ini

   # 配置啟動FastCGI程式:

   cd $PHP_DIR/etc/

   [ ! -e php-fpm.conf ] && cp php-fpm.conf.default php-fpm.conf


# 設定開機啟動

   # 注:ubuntu新增啟動項需插入到 exit 0 前

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

       echo “$PHP_DIR/sbin/php-fpm” >> /etc/rc.local

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

       sed -i “/^exit 0/ i\$PHP_DIR/sbin/php-fpm” /etc/rc.local

   else

       echo “unknown system,quit…”

       exit 0

   fi


   # 配置nginx載入php-fpm

   sed -i `$ i    include “/usr/local/nginx/conf/vhosts/*.conf”; ` $NGINX_DIR/conf/nginx.conf >> $IN_LOG 2>&1

   mkdir -p $NGINX_DIR/conf/vhosts >> $IN_LOG 2>&1

   mkdir -p $NGINX_DIR/html/127.0.0.1 >> $IN_LOG 2>&1

   cd $DEFAULT_DIR

   cp conf/default.conf $NGINX_DIR/conf/vhosts/ >> $IN_LOG 2>&1    

   cp conf/info.php $NGINX_DIR/html/127.0.0.1/ >> $IN_LOG 2>&1        


   # 重啟nginx和php-fpm:

   echo “restart nginx and php-fpm to load the php module…”

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

   sleep 1

   killall php-fpm >> $IN_LOG 2>&1

   $PHP_DIR/sbin/php-fpm >> $IN_LOG 2>&1

   sleep 1

   # 判斷php-fpm服務是否啟動

   PORT_9000=$(lsof -i:9000|wc -l)

   if [ $PORT_9000 == 0 ]; then

      echo “php-fpm service is not active,please check your configure!”

      exit 0

   else

      echo “php-fpm service start…”

   fi


   # 判斷PHP是否載入:

   PHP_LOAD=$(curl –head http://127.0.0.1/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 51CTO部落格,原文連結:http://blog.51cto.com/showerlee/1335053,如需轉載請自行聯絡原作者


相關文章