共计 7316 个字符,预计需要花费 19 分钟才能阅读完成。
本脚本主要介绍 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 来调用。
CentOS 6.4 下的 LNMP 生产环境搭建及安装脚本 http://www.linuxidc.com/Linux/2013-11/92428.htm
生产环境实用之 LNMP 架构的编译安装 +SSL 加密实现 http://www.linuxidc.com/Linux/2013-05/85099.htm
LNMP 全功能编译安装 for CentOS 6.3 笔记 http://www.linuxidc.com/Linux/2013-05/83788.htm
CentOS 6.3 安装 LNMP (PHP 5.4,MyySQL5.6) http://www.linuxidc.com/Linux/2013-04/82069.htm
在部署 LNMP 的时候遇到 Nginx 启动失败的 2 个问题 http://www.linuxidc.com/Linux/2013-03/81120.htm
Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm