十二章、LAMP環境搭建的自動化搭建
所謂自動化搭建當然是實現一定的自動化部署了,並且可以承受一定的大型的網站點選量,LAMP環境的部署完全可以使用自動化指令碼實現,這樣便達到了無人值守的安裝方式。
1、準備下列軟體
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.
2、編寫一個安裝指南install放在/root/apmg_install
下載後,tar zxvf apmg_install.tar.gz ;
cd apmg_install;
./setup.sh 自動完成安裝,時間大概10多分鐘
3、編寫httpd.sed檔案放在/root/apmg_install
目的是為了在apache的httpd.conf中加入相關的行。
檔案 httpd.sed內容:
/application/x-compress .Z/a
AddType application/x-httpd-php .php
4、編寫test.php放在/root/apmg_install
<?php
phpinfo();
?>
5、編寫測試指令碼放在/root/apmg_install/setup.sh
這個指令碼基於linux和freebsd,可能要經過多次測試才成功。
#!/bin/sh
#this is a script about *nix installing apache,php with gd2.write by sery(sery@163.com),in 2009-12-03
#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”>>/etc/ld.so.conf
ldconfig
rm -rf mysql-5.0.41
echo “mysql client install ok!”
sleep 2
fi
#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
fi
#install php.apache must be installed in /usr/local/apache2
if [ ! -d /usr/local/php ]
then
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
make
make install
cp php.ini-dist $php_prefix/lib/php.ini
cd ..
rm -rf php-5.3.6
echo “php with gd2 install is ok!”
sleep 2
fi
# setting apache configuration file
sed -f httpd.sed $http_prefix/conf/httpd.conf>$http_prefix/conf/httpd.conf.temp
cd $http_prefix/conf
mv httpd.conf httpd.conf.old
cat httpd.conf.temp>httpd.conf
$http_prefix/bin/apachectl -t
cd
echo “It is very ok!”
elif [ “$Os_is” = “Linux” ]
then
echo “this linux”
## 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”>>/etc/ld.so.conf
ldconfig
rm -rf mysql-5.0.41
echo “mysql client install ok!”
sleep 2
fi
#install freetype
if [ ! -d /usr/local/freetype ]
then
tar zxvf freetype-2.3.5.tar.gz
cd freetype-2.3.5
./configure –prefix=/usr/local/freetype
make
make install
cd ..
rm -rf freetype-2.3.5
echo “freetype install is ok!”
sleep 2
fi
#install jpeg
if [ ! -d /usr/local/jpeg ]
then
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!”
fi
#install libpng
if [ ! -d /usr/local/libpng ]
then
tar zxvf libpng-1.2.31.tar.gz
cd libpng-1.2.31
./configure –prefix=/usr/local/libpng
make
make install
cd ..
rm -rf libpng-1.2.31
echo “libpng install is ok!”
sleep 5
fi
#link header file
if [ ! -f /usr/include/pngconf.h ]
then
ln -s /usr/local/libpng/include/pngconf.h /usr/include
fi
if [ ! -f /usr/include/png.h ]
then
ln -s /usr/local/libpng/include/png.h /usr/include
fi
#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 –with-freetype=/usr/local/freetype –with-png=/usr/local/libpng –with-jpeg=/usr/local/
jpeg
make
make install
cd ..
rm -rf gd-2.0.35
echo “gd2 install is ok!”
sleep 2
fi
#install php.apache must be installed in /usr/local/apache2
if [ ! -d /usr/local/php ]
then
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-pdo-mysql –with-freetype=/usr/local/freetype
make
make install
cp php.ini-dist $php_prefix/lib/php.ini
cd ..
rm -rf php-5.3.6
echo “php with gd2 install is ok!”
sleep 2
fi
# setting apache configuration file
sed -f httpd.sed $http_prefix/conf/httpd.conf>$http_prefix/conf/httpd.conf.temp
cd $http_prefix/conf
mv httpd.conf httpd.conf.old
cat httpd.conf.temp>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
fi
6、以上準備工作做好以後,執行setup.sh
執行完畢後,啟動apache,然後執行test.php測試
然後執行,setup許可權不夠賦予許可權。
遇到下面的錯誤提示,如果sh目錄存在且指令碼無誤,最可能是原因是在windows下寫的指令碼是dos格式的,放在Linux下編碼不識別,解決方法是使用dos2unix命令轉一下,即輸入: dos2unix 檔名
我們可以看到,飛快的執行程式呢?結果我們拭目以待。
還有一點特別提醒,我們編寫指令碼檔案的時候是在window條件下,雖然執行了轉換,但難免會有亂碼,所以一定要vi setup.sh檔案檢視,還要留心標點符號也要符合linux環境才行。如windows下的‘’“”要轉換為linux下的` ` 和” “。
編譯遇到了php5模組沒載入正確。查文件需要關閉selinux。
Vi /etc/sysconfig/selinux
把 SELINUX=enforcing 註釋掉:#SELINUX=enforcing ,然後新加一行為:
SELINUX=disabled 然後重啟從新編譯。
我們然後在編譯這次出現
幾乎都是ok啦,我們執行/usr/local/apache2/bin/apachectl start
編譯完畢我們測試一下:http://192.168.22.132/test.php
7、打包歸檔
cd /root切換到root
tar zcvf apmg_install.tar.gz apmg_install
這樣將打包好儲存起來,下次可以解包直接使用