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

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

showerlee 2013-12-02 18:22 Dev, SHELL 阅读 (2,427) 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 >> $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)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/1007

继续浏览:lnmp

目前有1条大神的评论

loading
  1. 沙发
    zhangsir2015年6月15日下午2:51 回复

    不错啊

发表评论

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