说明:centos6 64位,php5.2.17,nginx0.8.54,mysql5.1.60
脚本文件:
lnmp.sh
#!/bin/sh
softinstall_dir=/opt/softwore_install
nginx_dir=$softinstall_dir/nginx
php_dir=$softinstall_dir/php
mysql_dir=$softinstall_dir/mysql
softwore_dir=/opt/softwore
function check_nginx(){
if [ -d $nginx_dir ];then
echo "nginx already installed!"
exit 1
fi
}
function check_mysql(){
if [ -d $mysql_dir ];then
echo "mysql already installed!"
exit 1
fi
}
function check_php(){
if [ -d $php_dir ];then
echo "php already installed!"
exit 1
fi
}
function init(){
yum -y install gcc automake autoconf libtool make patch gcc-c++ pcre-devel openssl-devel ncurses-devel libxml2-devel curl-devel libjpeg-devel gd-devel libmcrypt-devel mhash-devel bison cmake
cp /usr/lib64/libjpeg.so* /usr/lib
cp /usr/lib64/libpng* /usr/lib
}
function init_dir(){
if [ ! -d $softinstall_dir ];then
mkdir -p $softinstall_dir
fi
if [ ! -d $softwore_dir ];then
mkdir -p $softwore_dir
fi
}
function download_soft(){
cd $softwore_dir
wget http://nginx.org/download/nginx-0.8.54.tar.gz
wget http://cn2.php.net/distributions/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.1/mysql-5.1.60.tar.gz
for softwore in $softwore_dir/nginx-0.8.54.tar.gz $softwore_dir/php-5.2.17.tar.gz $softwore_dir/php-5.2.17-fpm-0.5.14.diff.gz $softwore_dir/mysql-5.1.60.tar.gz
do
if [ ! -f $softwore ];then
$softwore not exits ; Please check all the software needed to download
exit 1
fi
done
echo "Required software has been downloaded"
}
function install_nginx(){
#read -p "Enter nginx install dir(Default install dir:$softinstall_dir/nginx):" nnginx_dir
#if [ -z $nnginx_dir ] ;then
#nginx_dir=$softinstall_dir/nginx
#else
#nginx_dir=$nnginx_dir
#fi
#echo "Your nginx will install :"$nginx_dir
check_nginx
cd $softwore_dir
tar zxf nginx-0.8.54.tar.gz
cd nginx-0.8.54
./configure --prefix=$nginx_dir --with-http_stub_status_module --with-http_ssl_module
make && make install
echo "----------------------------"
echo "nginx installed successfully"
echo "----------------------------"
sleep 3
}
function install_mysql(){
check_mysql
cd $softwore_dir
tar zxf mysql-5.1.60.tar.gz
cd mysql-5.1.60
./configure --prefix=$mysql_dir --with-charset=utf8 --with-extra-charsets=all
make && make install
groupadd mysql && useradd -g mysql mysql -s /sbin/nologin
cd $mysql_dir
chown -R mysql:mysql .
$mysql_dir/bin/mysql_install_db --basedir=$mysql_dir --datadir=$mysql_dir/var --user=mysql
echo "----------------------------"
echo "mysql installed successfully"
echo "----------------------------"
sleep 3
}
function install_php(){
check_php
cd $softwore_dir
tar zxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz |patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=$php_dir --with-config-file-path=$php_dir/etc --with-mysql=$mysql_dir --with-mysqli=$mysql_dir/bin/mysql_config --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic
make && make install
cp php.ini-dist $php_dir/etc/php.ini
echo "----------------------------"
echo "php installed successfully"
echo "----------------------------"
}
init
init_dir
cat << EOF
1 only install nginx
2 only install mysql
3 install all(nginx mysql php)
EOF
if [ ! -f $softwore_dir/nginx-0.8.54.tar.gz ];then
download_soft
fi
read -p "Select the option to install LNMP...1|2|3:" option
case $option in
1)
install_nginx
;;
2)
install_mysql
;;
3)
install_nginx
install_mysql
install_php
;;
esac
如果php是5.3以后的版本 install_php 函数修改如下
function install_php(){
check_php
cd $softwore_dir
tar zxf php-5.3.xx.tar.gz
cd 5.3.xx
./configure --prefix=$php_dir --with-config-file-path=$php_dir/etc --with-mysql=$mysql_dir --with-mysqli=$mysql_dir/bin/mysql_config --with-iconv-dir --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 --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext(WARNING: unrecognized options: --enable-discard-path, --enable-magic-quotes, --enable-safe-mode, --enable-fastcgi, --enable-force-cgi-redirect, --with-mime-magic原因“Fastcgi is the preferred SAPI to connect PHP and Lighttpd. Fastcgi is automagically enabled in php-cgi in PHP 5.3, but for older versions configure PHP with --enable-fastcgi.”)
make && make install
cp php.ini-dist $php_dir/etc/php.ini
echo "----------------------------"
echo "php installed successfully"
echo "----------------------------"
}
如果php是7以后的版本 install_php 函数修改如下
function install_php(){
check_php
cd $softwore_dir
tar zxf php-7.xx.tar.gz
cd 7.xx
./configure --prefix=$php_dir --with-config-file-path=$php_dir/etc --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --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 --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext(要去掉--with-mysql,php7已经不支持mysql扩展,而改成mysqli和pdo_mysql)
make && make install
cp php.ini-dist $php_dir/etc/php.ini
echo "----------------------------"
echo "php installed successfully"
echo "----------------------------"
}
提示:在安装php的时候如果提示找不到mhash.h和mcrypt.h 请下载下面的rpm包安装(虽然我们yum了libmcrypt-devel和mhash-devel这只是安装了依赖)
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/mhash-0.9.9-1.el6.rf.x86_64.rpm
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/mhash-devel-0.9.9-1.el6.rf.x86_64.rpm