LAMP环境是linux下web的常用组合,为了节省时间,不用每次都手动编译安装,一种方法是将安装好的包制作为RPM或则DEB包,另外一种方式就是采用脚本的形式自动编译安装。
脚本目录结构:
<a href="http://blog.51cto.com/attachment/201204/110120707.jpg" target="_blank"></a>
脚本分为LAMP的安装脚本,配置文件,全局变量,源码包
脚本功能演示:
安装httpd
<a href="http://blog.51cto.com/attachment/201204/112405825.jpg" target="_blank"></a>
安装mysql
<a href="http://blog.51cto.com/attachment/201204/113930712.jpg" target="_blank"></a>
安装php
<a href="http://blog.51cto.com/attachment/201204/114408849.jpg" target="_blank"></a>
安装memcached
<a href="http://blog.51cto.com/attachment/201204/114458915.jpg" target="_blank"></a>
脚本代码:
全局变量global_variables.sh
#! /bin/bash
#### global directory variables#########
SOURCE_DIR="./sources"
UNPACK_DIR="./unpack_sources"
INSTALL_ROOT="/opt"
CONF_SAMPLE="./conf_samples"
安装httpd脚本
###### variables ###################
GLOABLE_VARIABLE_FILE="./global_variables.sh"
HTTPD_SOURCE_FILENAME="httpd-2.2.19.tar.gz"
UNPACK_DIR_NAME="httpd-2.2.19"
#### source the global variable #####
source ${GLOABLE_VARIABLE_FILE}
#########OK and failed ########
GREEN_OK="\e[0;32m\033[1mOK\e[m"
RED_FAILED="\e[0;31m\033[1mFAILED\e[m"
#######lots of functions ##########
error()
{
local FORMAT="$1"
shift
printf "${RED_FAILED} - ${FORMAT}" "$@" >&1
}
info()
printf "INFO - ${FORMAT}" "$@" >&1
warning()
print "WARNING - ${FORMAT}" "$@" >&1
check_source()
info "Checking source file %s.\n" "${HTTPD_SOURCE_FILENAME}"
if [ -f ${SOURCE_DIR}/${HTTPD_SOURCE_FILENAME} ]
then
info "Httpd file %s is ${GREEN_OK}.\n" "${HTTPD_SOURCE_FILENAME}"
else
error "%s file not found.\n" "${HTTPD_SOURCE_FILENAME}"
exit $?
fi
unpack_file()
info "Unpack the source file %s .\n" "${HTTPD_SOURCE_FILENAME}"
tar -zxf ${SOURCE_DIR}/${HTTPD_SOURCE_FILENAME} -C ${UNPACK_DIR}
if [[ $? != 0 ]]
error "Can not unpack file %s.\n" "${HTTPD_SOURCE_FILENAME}"
else
info "Unpack %s done.${GREEN_OK}\n" "${HTTPD_SOURCE_FILENAME}"
install_file()
cd ${UNPACK_DIR}/${UNPACK_DIR_NAME}
local CONFIG_VARIABLE="--prefix=${INSTALL_ROOT}/httpd --enable-so --enable-cgi --enable-proxy --enable-vhost-alias --enable-cache --enable-disk-cache --enable-mem-cache --enable-rewrite --enable-mods-shared=all --enable-usertrack --enable-ssl"
local USER="www"
info "Add the www user ...\n"
groupadd -g 501 ${USER}
useradd -g ${USER} -u 501 -s /sbin/nologin ${USER}
info "Add user www done.\n"
info "Now configure the httpd,this will take serveral minutes...\n"
./configure ${CONFIG_VARIABLE} 1>/dev/null
if [[ $? == 0 ]]
info "Httpd configure ${GREEN_OK}. Now make and make install.this will take serveral minutes... \n"
make 1>/dev/null && make install 1>/dev/null
if [[ $? == 0 ]]
then
info "HTTPD installed ${GREEN_OK}.\n"
else
error "Httpd not installed.\n "
exit $?
fi
error "Configure is not complete.\n"
cd -
################## main ##########################
#Check the httpd file
info "Check the httpd file.\n"
check_source
#Unpack the httpd file
unpack_file
#install the file
info "Begin to install the httpd service.\n"
install_file
mkdir -p /opt/www/html
本文转自 waydee 51CTO博客,原文链接:http://blog.51cto.com/waydee/847099,如需转载请自行联系原作者