lnmp+FastCGI平台搭建脚本

本脚本主要介绍lnmp+FastCGI平台的搭建脚本,需要准备的安装包如下:

cmake-2.8.10.2.tar.gz

libevent-2.0.21-stable.tar.gz

libmcrypt-2.5.8.tar.gz

mhash-0.9.9.9.tar.gz

nginx-1.2.0.tar.gz

php-5.4.9.tar.gz
libiconv-1.13.tar.gz

mysql-5.5.13.tar.gz

由于脚本需要,将上述安装包统一打包为lnmp+memcached.zip,使用时将该脚本放在该安装包相同的目录。

先安装依赖包,需要将yum源配好,在这里不再赘述,接下来是脚本内容:

#!/bin/bash
echo "++++++++++++++++++++++++++++++++++++++++++"
echo "+                  检查yum仓库配置                     +"
echo "++++++++++++++++++++++++++++++++++++++++++"
yum clean all > /dev/null
yum list > /dev/null
if [ $? -eq 0 ];then
    echo  "yum仓库配置正常"
    sleep 1
else
    echo "yum仓库配置异常,请检查yum配置"
fi
echo "++++++++++++++++++++++++++++++++++++++++++"
echo "+           安装依赖软件包                +"
echo "++++++++++++++++++++++++++++++++++++++++++"
yum -y install gcc gcc-c++ autoconf automake bison flex freetype freetype-devel fontconfig-devel gettext-devel libjpeg libjpeg-devel libpng libpng-devel libxml2 libxml2-devel libtool libtool-ltdl libtool-ltdl-devel libtiff-devel libXpm-devel libicu libicu-devel libidn libidn-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre pcre-devel pam-devel vim-enhanced mysql-devel

unzip lnmp+memcached.zip


installnginx() {
read -p "即将开始安装nginx,是否开始安装?(Y/N)" command_nginx_install
if [ $command_nginx_install == N ] || [ $command_nginx_install == n ];then
    echo "安装已取消!"
    exit
elif [ $command_nginx_install == Y ] || [ $command_nginx_install == Y ];then
    echo "安装正在开始……"
    sleep 2
fi
cd lnmp+memcached
useradd -s /sbin/nologin -M nginx
tar -zxf nginx-1.2.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.2.0/
 ./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/nginx.pid --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/client --http-proxy-temp-path=/usr/local/nginx/proxy --http-fastcgi-temp-path=/usr/local/nginx/fcgi --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --with-pcre
if [ $? -eq 0 ];then
    echo "配置完成,准备编译安装"
else
    echo "配置失败,请检查"
    exit
fi
make && make install
if [ $? -eq 0 ];then
    echo "nginx安装完成"
else
    echo "nginx安装失败,请检查"
    exit
fi
}
installmysql() {
echo "##############################"
echo "#    开始安装mysql数据库……   #"
echo "##############################"
read -p "即将开始安装mysql数据库,是否继续?(Y/N)" command_mysql_install
if [ $command_mysql_install == N ] || [ $command_mysql_install == n ];then
    echo "安装已取消!"
    exit
elif [ $command_mysql_install == Y ] || [ $command_mysql_install == Y ];then
    echo "安装正在开始……"
    sleep 2
fi
cd /root/lnmp+memcached
useradd -s /sbin/nologin -M mysql
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "~           安装cmake工具            ~"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
tar -zxf cmake-2.8.10.2.tar.gz -C /usr/src/
cd /usr/src/cmake-2.8.10.2/
./bootstrap --prefix=/usr/local/cmake
make && make install
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "~           安装mysql数据库          ~"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cd /root/lnmp+memcached
tar -zxf mysql-5.5.13.tar.gz -C /usr/src/
cd /usr/src/mysql-5.5.13/
/usr/local/cmake/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1
if [ $? -eq 0 ];then
    echo "配置完成,即将开始编译安装"
else
    echo "配置失败,请检查!"
    exit
fi
make && make install
if [ $? -eq 0 ];then
    echo "mysql安装完成,准备初始化"
else
    echo "mysql安装失败,请检查!"
    exit
fi
cd /usr/local/mysql/
chown -R mysql.mysql .
./scripts/mysql_install_db --user=mysql
cd /usr/src/mysql-5.5.13/support-files/
cp my-medium.cnf /etc/my.cnf
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start > /dev/null
cd /usr/local/mysql/
./scripts/mysql_install_db --user=mysql
export PATH=/usr/local/mysql/bin/:$PATH
echo "export   PATH=/usr/local/mysql/bin:$PATH" >> /etc/bashrc
echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf
ldconfig
if [ $? -eq 0 ];then
    echo "mysql初始化完成"
else
    echo "mysql初始化失败,请检查!"
    exit
fi
}
installphp() {
echo "##############################"
echo "#       开始安装PHP服务        #"
echo "##############################"
read -p "即将安装PHP服务,是否继续?(Y/N)" command_php_install
if [ $command_php_install == n ] || [ $command_php_install == N ];then
    echo "安装取消"
    exit
elif [ $command_php_install == y ] || [ $command_php_install == Y ];then
    cd /root/lnmp+memcached
    echo "开始安装PHP"
fi
echo "##############################"
echo "#        安装PHP的依赖包        #"
echo "##############################"
tar -zxf mhash-0.9.9.9.tar.gz -C /usr/src
cd /usr/src/mhash-0.9.9.9
./configure
make && make install
if [ $? -eq 0 ];then
    echo "mhash安装完成"
else
    echo "mhash安装失败"
    exit
fi
sleep 2
cd /root/lnmp+memcached
tar -zxf libmcrypt-2.5.8.tar.gz -C /usr/src
cd /usr/src/libmcrypt-2.5.8
./configure
make && make install
cd libltdl/
./configure --with-gmetad --enable-gexec –enable-ltdl-install
make && make install
if [ $? -eq 0 ];then
        echo "libmcrypt安装完成"
else
        echo "libmcrypt安装失败"
        exit
fi
sleep 2
cd /root/lnmp+memcached
tar -zxf libiconv-1.13.tar.gz -C /usr/src
cd /usr/src/libiconv-1.13
./configure
make && make install
if [ $? -eq 0 ];then
        echo "libconv安装完成"
else
        echo "libconv安装失败"
        exit
fi
sleep 2
cd /root/
ln -sv /usr/local/lib/libmcrypt* /usr/lib/
ln -sv /usr/local/lib/libmhash.* /usr/lib/
ldconfig -v
echo "##############################################"
echo "#                开始安装PHP主程序                 #"
echo "##############################################"
cd /root/lnmp+memcached
tar -zxf php-5.4.9.tar.gz -C /usr/src/
cd /usr/src/php-5.4.9
./configure --prefix=/usr/local/php5nginx --with-config-file-path=/usr/local/php5nginx/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
if [ $? -eq 0 ];then
    echo "php配置完成"
else
    echo "php配置失败,请检查!"
    exit
fi
make ZEND_EXTRA_LIBS='-liconv'
make install
if [ $? -eq 0 ];then
    echo "php安装完成,准备初始化"
else
    echo "php安装失败,请检查!"
    exit
fi
cp php.ini-production /usr/local/php5nginx/etc/php.ini
cp /usr/local/php5nginx/etc/php-fpm.conf.default /usr/local/php5nginx/etc/php-fpm.conf
cd /usr/src/php-5.4.9/sapi/fpm/
cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
service php-fpm start
if [ $? -eq 0 ];then
    echo "php初始化完成"
else
    echo "php初始化失败,请检查!"
    exit
fi
}

case $1 in
nginx)
    installnginx ;;
mysql)
    installmysql ;;
php)
    installphp ;;
lnmp)
    installnginx
    installmysql
    installphp ;;
*)
    echo "本脚本可安装nginx、mysql、php以及整体搭建lnmp平台,请输入要安装的服务。例如:sh nginx.sh lnmp";;
esac

该脚本使用时可以给脚本添加执行权限:

chmod +x lnmp.sh

此时直接使用./lnmp.sh 安装部分 即可执行安装,注意,此脚本需要指定要安装的部分,比如要安装mysql,则在执行时候输入./lnmp.sh mysql。

若在一台裸机上安装lnmp平台,则可以使用./lnmp.sh lnmp来全部搭建lnmp平台。

由于我们在安装时可能多次执行脚本,所以我们可以将脚本中使用yum安装部分单独保存为一个脚本,或者封装为一个函数,在脚本后添加一个case来调用。