天天看点

服务器架构之性能扩展-第十二章(13)

十二章、<b>LAMP</b><b>环境搭建的自动化搭建</b>

所谓自动化搭建当然是实现一定的自动化部署了,并且可以承受一定的大型的网站点击量,LAMP环境的部署完全可以使用自动化脚本实现,这样便达到了无人值守的安装方式。

<b>1</b><b>、准备下列软件</b><b></b>

<b></b>

httpd-2.2.14.tar.gz

mysql-5.1.41.tar.gz

gd-2.0.35.tar.gz

php-5.2.11.tar.gz

freetype-2.3.10.tar.gz

jpegsrc.v7.tar.gz

libpng-1.2.41.tar.gz

下载好,上传到/root/apmg_install.

<b>2、编写一个安装指南install放在/root/apmg_install</b>

下载后,tar zxvf apmg_install.tar.gz ;

cd apmg_install;

./setup.sh 自动完成安装,时间大概10多分钟

<b>3、编写httpd.sed文件放在/root/apmg_install</b>

目的是为了在apache的httpd.conf中加入相关的行。

文件 httpd.sed内容:

/application\/x-compress .Z/a\

\    AddType application/x-httpd-php .php

<b>4、编写test.php放在/root/apmg_install</b>

&lt;?php

phpinfo();

?&gt;

<b>5、编写测试脚本放在/root/apmg_install/setup.sh</b>

这个脚本基于linux和freebsd,可能要经过多次测试才成功。

#!/bin/sh

#define some variables

http_prefix=/usr/local/apache2

php_prefix=/usr/local/php

#need root install

is_root=`id |awk ‘{print $1}’|awk -F’[=(]‘ ‘{print $2}’`

if [ $is_root != 0 ]

   then

   echo “please use root install this program!!!”

   exit 1

fi

#Os is ?

Os_is=`uname`

if [ "$Os_is" = "FreeBSD" ]

then

  ## install apache

  if [ ! -d "$http_prefix" ]

    then

       tar zxvf httpd-2.2.14.tar.gz

       cd httpd-2.2.14

       ./configure --prefix=$http_prefix --enable-so --enable-write --with-mpm=worker

       make

       make install

       cd ..

       if [ ! -f "$http_prefix/htdocs/test.php" ]

           then

              cp test.php $http_prefix/htdocs

       fi

       rm -rf httpd-2.2.14

       echo “apache install is ok!”

       sleep 2

  fi

  #install mysql client

  is_inst_mysql=`find /usr/local/bin -name mysql | wc -l`

  if [ "$is_inst_mysql" = 0 ]

     then

         tar zxvf mysql-5.0.41.tar.gz

         cd mysql-5.0.41

         ./configure --without-server --with-extra-charsets=gbk,gb2312,utf8 --enable-thread-safe-client

         make

         make install

         cd ..

         echo “/usr/local/lib”&gt;&gt;/etc/ld.so.conf

         ldconfig

         rm -rf mysql-5.0.41

         echo “mysql client install ok!”

         sleep 2

  #install gd2

  if [ ! -d /usr/local/gd2 ]

  then

      tar zxvf gd-2.0.35.tar.gz

      cd gd-2.0.35

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

      make

      make install

      cd ..

      rm -rf gd-2.0.35

      echo “gd2 install is ok!”

      sleep 2

  #install php.apache must be installed in /usr/local/apache2

  if [ ! -d /usr/local/php ]

      tar zxvf php-5.3.6.tar.gz

      cd php-5.3.6

      ./configure --prefix=$php_prefix --with-gd=/usr/local/gd2 --with-apxs2=$http_prefix/bin/apxs --enable-mbregex --enable-bcmath

--with-mysql --with-zlib-dir --enable-mbstring=all --with-freetype-dir --with-pdo-mysql

      cp php.ini-dist $php_prefix/lib/php.ini

      rm -rf php-5.3.6

      echo “php with gd2 install is ok!”

      fi

  # setting apache configuration file

  sed -f httpd.sed $http_prefix/conf/httpd.conf&gt;$http_prefix/conf/httpd.conf.temp

  cd $http_prefix/conf

  mv httpd.conf httpd.conf.old

  cat httpd.conf.temp&gt;httpd.conf

  $http_prefix/bin/apachectl -t

  cd

  echo “It is very ok!”

elif [ "$Os_is" = "Linux" ]

  echo “this linux”

       ./configure --prefix=$http_prefix --enable-so –enable-write --with-mpm=worker

  #install freetype

  if [ ! -d /usr/local/freetype ]

         tar zxvf freetype-2.3.5.tar.gz

         cd freetype-2.3.5

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

         rm -rf freetype-2.3.5

         echo “freetype install is ok!”

    fi

  #install jpeg

  if [ ! -d /usr/local/jpeg ]

        tar zxvf jpegsrc.v6.tar.gz

        cd jpeg-6

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

        make

        make install

        cd ..

        rm -rf jpeg-6

        echo “gpeg install is ok!”

  #install libpng

  if [ ! -d /usr/local/libpng ]

        tar zxvf libpng-1.2.31.tar.gz

        cd libpng-1.2.31

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

        rm -rf libpng-1.2.31

        echo “libpng install is ok!”

        sleep 5

  #link header file

  if [ ! -f /usr/include/pngconf.h ]

        ln -s /usr/local/libpng/include/pngconf.h /usr/include

  if [ ! -f /usr/include/png.h ]

        ln -s /usr/local/libpng/include/png.h /usr/include

        tar zxvf gd-2.0.35.tar.gz

        cd gd-2.0.35

        ./configure --prefix=/usr/local/gd2 --with-freetype=/usr/local/freetype --with-png=/usr/local/libpng --with-jpeg=/usr/local/

jpeg

        rm -rf gd-2.0.35

        echo “gd2 install is ok!”

        sleep 2

--with-mysql --with-zlib-dir --enable-mbstring=all --with-pdo-mysql --with-freetype=/usr/local/freetype

    sed -f httpd.sed $http_prefix/conf/httpd.conf&gt;$http_prefix/conf/httpd.conf.temp

    cd $http_prefix/conf

    mv httpd.conf httpd.conf.old

    cat httpd.conf.temp&gt;httpd.conf

    $http_prefix/bin/apachectl -t

    cd

    echo “It is very ok!”

else

echo “this is other os,please modify the script”

exit 1

<b>6</b><b>、以上准备工作做好以后,执行</b><b>setup.sh</b>

执行完毕后,启动apache,然后运行test.php测试

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113520698.jpg"></a>

然后执行,setup权限不够赋予权限。

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113533900.jpg"></a>

遇到下面的错误提示,如果sh目录存在且脚本无误,最可能是原因是在windows下写的脚本是dos格式的,放在Linux下编码不识别,解决方法是使用dos2unix命令转一下,即输入: dos2unix 文件名

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113541776.jpg"></a>

我们可以看到,飞快的执行程序呢?结果我们拭目以待。

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113558225.jpg"></a>

还有一点特别提醒,我们编写脚本文件的时候是在window条件下,虽然执行了转换,但难免会有乱码,所以一定要vi setup.sh文件查看,还要留心标点符号也要符合linux环境才行。如windows下的‘’“”要转换为linux下的' ' 和" "。

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113607287.jpg"></a>

编译遇到了php5模块没加载正确。查文档需要关闭selinux。

Vi /etc/sysconfig/selinux

把 SELINUX=enforcing 注释掉:#SELINUX=enforcing ,然后新加一行为:

SELINUX=disabled    然后重启从新编译。

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113618422.jpg"></a>

我们然后在编译这次出现

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113628308.jpg"></a>

几乎都是ok啦,我们执行/usr/local/apache2/bin/apachectl start

<a target="_blank" href="http://blog.51cto.com/attachment/201210/113636783.jpg"></a>

<b>7</b><b>、打包归档</b><b></b>

cd /root切换到root

tar zcvf apmg_install.tar.gz apmg_install 

<a href="http://blog.51cto.com/attachment/201210/125725212.jpg" target="_blank"></a>

这样将打包好保存起来,下次可以解包直接使用

本文转自zsaisai 51CTO博客,原文链接:http://blog.51cto.com/3402313/1036233