1. 程式人生 > 實用技巧 >LAMP環境安裝自動化

LAMP環境安裝自動化

搭建LAMP環境時間很長,最少30-40分鐘,我們可以用指令碼來安裝,既可以安裝自動化,還可以在安裝的時候幹別的事情,消除等待的煩惱。一舉兩得。

#!/bin/sh
#由於系統環境不同,具體安裝請參考文件!
#by liangz at 2010-08-14
#環境:
# 1. Radhad或者CentOS最小化安裝,配置好IP地址,主機名等資訊
# 2. 需要構建本地YUM源倉庫
# 3. 必須的軟體:gcc,gcc-c++,make,及unzip
# yum install -y gcc-c++
# yum install -y make
# yum install -y unzip
# 4. 關閉selinux---vi /etc/selinux/config---修改“SELINUX=disabled”

# 5. 刪除CentOS預設iptables策略---iptables -F
#1.apr: http://apache.freelamp.com/apr/apr-1.4.2.tar.gz
#2.apr-util: http://apache.freelamp.com/apr/apr-util-1.3.9.tar.gz
#3.pcre: http://sourceforge.net/projects/pcre/files/
#4.apache: http://labs.renren.com/apache-mirror/httpd/httpd-2.3.6.tar.gz
#5.mysql: http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.48-linux-i686-glibc23.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/
#6.php: http://cn2.php.net/get/php-5.3.2.tar.gz/from/cn.php.net/mirror

#1. 安裝apr
function aprinstall() {
tar -xzf apr-1.4.2.tar.gz
cd apr-1.4.2
./configure --prefix=/usr/local/apr
make && make install
cd .. && rm -rf apr-1.4.2
}
echo "10% completed......"
#2.安裝apr-util
function aprutilinstall() {
tar -xzf apr-util-1.3.9.tar.gz
cd apr-util-1.3.9
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
cd .. && rm -rf apr-util-1.3.9
}
echo "20% completed......"
#3. 安裝pcre
function pcreinstall() {
unzip pcre-8.10.zip
cd pcre-8.10
./configure --prefix=/usr/local/pcre
make && make install
cd .. && rm -rf pcre-8.10
}
echo "30% completed......"
#4. 安裝apache
function apacheinstall() {
#請先搭建好YUM源!!!!!!
yum install -y openssl-devel
tar -xzf httpd-2.3.6.tar.gz
cd httpd-2.3.6
./configure --prefix=/usr/local/apache2 \
--enable-so --enable-rewrite --enable-cgi \
--enable-suexec --enable-ssl --enable-charset-lite \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre
make && make install
cd .. && rm -rf httpd-2.3.6
cp -pf /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
service httpd start
echo "service httpd start" >> /etc/rc.local
}
echo "50% completed......"
#5. 安裝mysql mysql在官方網站上下載的已編譯好的檔案
function mysqlinstall() {
tar -xzf mysql-5.1.48-linux-i686-glibc23.tar.gz -C /usr/local/
pushd /usr/local/
mv mysql-5.1.48-linux-i686-glibc23/ mysql
useradd -M -s /sbin/nologin mysql
cd mysql
cp support-files/my-medium.cnf /etc/my.cnf
chown -R mysql.mysql .
./scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
cp support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on
chmod a+x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 35 mysqld on
echo "/usr/local/mysql/lib" >> /etc/ld.so.conf
ldconfig
./bin/mysqld_safe --user=mysql &
mysqladmin -u root password "123.com"
popd
}
echo "70% completed......"
#6. 安裝PHP
function phpinstall() {
yum install -y libxml2-devel
tar -xzf php-5.3.2.tar.gz
cd php-5.3.2
./configure --prefix=/usr/local/php5 \
--enable-mbstring --enable-sockets \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php5
make && make install
cp php.ini-production /usr/local/php5/php.ini
./build/shtool install -c ext/phar/phar.phar /usr/local/php5/bin
ln -s -f /usr/local/php5/bin/phar.phar /usr/local/php5/bin/phar
cd .. && rm -rf php-5.3.2
echo "ServerName `hostname`" >> /usr/local/apache2/conf/httpd.conf
echo "AddTyep application/x-httpd-php .php" >> /usr/local/apache2/conf/httpd.conf
echo "AddType p_w_picpath/x-icon .ico" >> /usr/local/apache2/conf/httpd.conf
echo "DirectoryIndex index.php" >> /usr/local/apache2/conf/httpd.conf
service httpd restart
}
echo "90% completed......"
#建立測試頁
function testinfo() {
cat > /usr/local/apache2/htdocs/test.php << END
<?php
phpinfo();
?>
END
cat > /usr/local/apache2/htdocs/testdb.php << END
<?php
\$link=mysql_connect('localhost','test','');
if(!\$link) echo "Fail !!";
else echo "Success !!";
mysql_close();
?>
END
_IPADDR=`grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 | cut -d "=" -f 2`
echo "請開啟瀏覽器訪問
http://$_IPADDR/test.php
http://$_IPADDR/testdb.php
如果頁面正常,恭喜您,安裝成功!"
}

安裝還之後,如果成功,頁面如下;

轉載於:https://blog.51cto.com/yueyuanyuan/376153