微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell脚本:一键安装LAMP、LNMP脚本

 系统大全为您提供

历时三天边写边测试,终于完成目前最长代码的脚本,总共253行,在2台机器安装LAMP,LNMP都顺利通过,脚本可做为生产环境使用。

 

脚本需要注意的地方:

 

1:脚本适用于centos系统6.5以上,干净系统;如安装过其他环境需要修改脚本内容

 

2:脚本适用于:apache 2.4.x 版本;MysqL5.5.x以上版本;PHP5.5.x以上版本;

 

3:MysqL 、PHP、 apache、 Nginx 安装包事先要下载到/usr/local

c目录下,如名字不对需要更改脚本里的软件包名称

 

脚本安装的版本为以下,如需其他名称,请自行修改软件安装包名称和对应的目录名即可;

httpd-2.4.7.tar.gz 、mysql-5.6.25.tar.gz 、Nginx-1.6.2.tar.gz、 PHP-5.6.8.tar.gz

 

4:脚本没有增加判断,设定的环境为干净系统;

 

脚本内容如下:

 

#!/bin/bash

#written by mofansheng @2015-11-06

#one key install LAMP or LNMP scripts; apply to apache 2.4.x,MysqL 5.6.x,Nginx 1.6.x;

. /etc/init.d/functions

#check the results of the command execution

function check_ok(){

  if [ $? -eq 0 ]

   then

     continue

  else

     echo "please check error"

     exit

  fi

}

function yum_update(){

 #set yum repos

 echo "===update yum repos,it will take serval mintinues==="

 yum install wget -y

 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null

 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo &>/dev/null

 yum clean all &>/dev/null

 yum makecache &>/dev/null

 check_ok

 action  "yum repos update is ok" /bin/true

}

function yum_depend(){

   #install dependencies packages

   yum install wget gcc gcc-c++ make re2c curl curl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel libmcrypt libmcrypt-devel zlib zlib-devel openssl openssl-devel freetype freetype-devel gd gd-devel perl perl-devel ncurses ncurses-devel bison bison-devel libtool gettext gettext-devel cmake bzip2 bzip2-devel pcre pcre-devel -y

}

function install_MysqL(){

 echo "MysqL5.6.25 will be installed,please be patient"

 cd /usr/local

c

 tar -zxf mysql-5.6.25.tar.gz

 cd mysql-5.6.25

 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL -DMysqL_DATADIR=/data/MysqL -DMysqL_UNIX_ADDR=/usr/local/MysqL/MysqL.sock -DSYSconfdIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMysqL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

 check_ok

 make && make install

 check_ok

  

 useradd -M -s /sbin

login MysqL

 mkdir -p /data/MysqL

 chown -R MysqL:MysqL /data/MysqL/

 chown -R MysqL:MysqL /usr/local/MysqL/

 check_ok

 cd /usr/local/MysqL/scripts/

 ./MysqL_install_db --basedir=/usr/local/MysqL/ --datadir=/data/MysqL/ --user=MysqL

 check_ok

 /bin

 /usr/local/MysqL/my.cnf /etc/my.cnf

 sed -i '/^[MysqLd]$/auser = MysqL

datadir = /data/MysqL

default_storage_engine = InnoDB

' /etc/my.cnf

 check_ok

  

 cp /usr/local/MysqL/support-files/MysqL.server /etc/init.d/MysqLd

 sed -i 's#^datadir=#datadir=/data/MysqL#' /etc/init.d/MysqLd

 sed -i 's#^basedir=#basedir=/usr/local/MysqL#' /etc/init.d/MysqLd

 service MysqLd start

 chkconfig --add MysqLd

 chkconfig MysqLd on

 check_ok

  

 iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

 /etc/init.d/iptables save

 check_ok

 echo "export PATH=$PATH:/usr/local/MysqL/bin" >>/etc/profile

 source /etc/profile

 check_ok

}

function install_apache(){

 echo "apache2.4.7 will be installed,please be patient"

 cd /usr/local

c

 wget http://mirrors.cnnic.cn/apache/apr/apr-1.5.2.tar.gz

 wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.5.4.tar.gz

 check_ok

 tar zxf apr-1.5.2.tar.gz

 cd apr-1.5.2

 ./configure --prefix=/usr/local/apr

 check_ok

 make && make install

 check_ok

  

 cd /usr/local

c

 tar zxf apr-util-1.5.4.tar.gz

 cd apr-util-1.5.4

 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

 check_ok

 make && make install

 check_ok

  

 cd /usr/local

c

 tar zxf httpd-2.4.7.tar.gz

 /bin

 -r apr-1.5.2 /usr/local

c

tpd-2.4.7

clib/apr

 /bin

 -r apr-util-1.5.4 /usr/local

c

tpd-2.4.7

clib/apr-util

 cd httpd-2.4.7

 ./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre --enable-mods-shared=most --enable-so --with-included-apr

 check_ok

 make && make install

 check_ok

  

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

 source /etc/profile

 check_ok

  

 iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

 /etc/init.d/iptables save

 check_ok

 /usr/local/apache2/bin/apachectl

 check_ok

}

function install_PHP(){

 echo "PHP5.6.8 will be installed,please be patient"

 cd /usr/local

c

 tar zxf PHP-5.6.8.tar.gz

 cd PHP-5.6.8

 ./configure   --prefix=/usr/local/PHP   --with-apxs2=/usr/local/apache2/bin/apxs   --with-config-file-path=/usr/local/PHP/etc   --with-MysqL=/usr/local/MysqL   --with-libxml-dir   --with-gd   --with-jpeg-dir   --with-png-dir   --with-freetype-dir   --with-iconv-dir   --with-zlib-dir   --with-bz2   --with-openssl   --with-mcrypt   --enable-soap   --enable-gd-native-ttf   --enable-mbstring   --enable-sockets   --enable-exif   --disable-ipv6

 check_ok

 make && make install

 check_ok

  

 cp /usr/local

c/PHP-5.6.8/PHP.ini-production /usr/local/PHP/etc/PHP.ini

 sed -i 's#^;date.timezone =#date.timezone=Asia/Shanghai#' /usr/local/PHP/etc/PHP.ini

 check_ok

  

}

function set_lamp(){

 sed -i '/AddType application/x-gzip .gz .tgz/a    AddType application/x-httpd-PHP .PHP

' /usr/local/apache2/conf

tpd.conf

 sed -i 's#index.html#index.html index.PHP#' /usr/local/apache2/conf

tpd.conf

 sed -i '/#ServerName www.example.com:80/aServerName localhost:80

' /usr/local/apache2/conf

tpd.conf

 check_ok

cat >>/usr/local/apache2

docs/test.PHP<<EOF

<?PHP

echo "PHP is OK

";

?>

EOF

 /usr/local/apache2/bin/apachectl graceful

 check_ok

 curl localhost/test.PHP

 check_ok

 action "LAMP is install success" /bin/true

}

function install_PHPfpm(){

 echo "PHP5.6.8 will be installed,please be patient"

 useradd -s /sbin

login PHP-fpm

 cd /usr/local

c

 tar zxf PHP-5.6.8.tar.gz

 cd PHP-5.6.8

 ./configure --prefix=/usr/local/PHP-fpm --with-config-file-path=/usr/local/PHP-fpm/etc --enable-fpm --with-fpm-user=PHP-fpm --with-fpm-group=PHP-fpm --with-MysqL=MysqLnd  --with-pdo-MysqL=MysqLnd --with-MysqLi=MysqLnd --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-exif --disable-ipv6 --with-pear --with-curl --enable-bcmath --enable-mbstring --enable-sockets --with-gettext

 check_ok

 make && make install

 check_ok

  

 cp /usr/local

c/PHP-5.6.8/PHP.ini-production /usr/local/PHP-fpm/etc/PHP.ini

 sed -i 's#^;date.timezone =#date.timezone=Asia/Shanghai#' /usr/local/PHP-fpm/etc/PHP.ini

 cd /usr/local/PHP-fpm/etc/

 mv PHP-fpm.conf.default PHP-fpm.conf

 check_ok

  

 cp /usr/local

c/PHP-5.6.8

pi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm

 chmod 755 /etc/init.d/PHP-fpm

 chkconfig --add PHP-fpm

 chkconfig PHP-fpm on

 service PHP-fpm start

 check_ok

}

function install_Nginx(){

 echo "Nginx1.6.2 will be installed,please be patient"

 cd /usr/local

c

 tar zxf Nginx-1.6.2.tar.gz

 cd Nginx-1.6.2

 ./configure --prefix=/usr/local

inx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

 check_ok

 make && make install

 check_ok

  

 /usr/local

inx/sbin

inx

 check_ok

}

function set_lnmp(){

 sed -i '56alocation ~ .PHP$ {

    root           html;

    fastcgi_pass   127.0.0.1:9000;

    fastcgi_index  index.PHP;

    fastcgi_param  SCRIPT_FILENAME  /usr/local

inx

ml$fastcgi_script_name;

    include        fastcgi_params;

}

' /usr/local

inx/conf

inx.conf

 /usr/local

inx/sbin

inx -s reload

 check_ok

 echo -e '<?PHP

 echo "Nginx and PHP is OK";

?>

' >/usr/local

inx

ml/index.PHP

 curl localhost/index.PHP

 check_ok

 action "LNMP is install success" /bin/true

}

function install_lamp(){

 echo "apache 2.4.7 MysqL 5.6.24 PHP5.6.8 will be installed"

 echo "===update yum repos and install dependecies packages,it will take serval mintinues==="

 yum_update

 check_ok

 yum_depend

 check_ok

 install_MysqL

 check_ok

 install_apache

 check_ok

 install_PHP

 check_ok

 set_lamp

}

function install_lnmp(){

 echo "Nginx1.6.2 MysqL 5.6.24 PHP5.6.8 will be installed"

 echo "===update yum repos and install dependecies packages,it will take serval mintinues==="

 yum_update

 check_ok

 yum_depend

 check_ok

 install_MysqL

 check_ok

 install_PHPfpm

 check_ok

 install_Nginx

 check_ok

 set_lnmp

}

cat <<EOF

    1:[install LAMP]

    2:[install LNMP]

    3:[exit]

EOF

read -t 10 -p "please input the num you want:" input

case ${input} in

 1)

 install_lamp

 ;;

 2)

 install_lnmp

 ;;

 3)

 exit

 ;;

 *)

 printf "You must input only in {1|2|3}

"

esac 

经验总结:

  以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。 

 

本文来自系统大全http://www.win7cn.com/如需转载请注明!推荐:win7纯净版

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐