#!/bin/bash
src="/usr/local/src/"
cd $src
findPortKill (){
processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`
for i in $processe
do
kill -9 $i
done
}
createLink(){
path=${1}
linkPath='/link'
if test ! -d ${linkPath}
then
mkdir ${linkPath}
fi
fileName=`echo ${path} | awk -F '/' '{print $1 $NF}'`
if test -d ${linkPath}/${fileName}
then
rm -rf ${linkPath}/${fileName}
fi
ln -s ${path} ${linkPath}
}
addToPATH(){
bin=${1}
echo $PATH|grep ${bin} >/dev/null
if [ $? -ne 0 ]; then
echo "export PATH=\$PATH:${bin}">>/etc/profile
fi
}
installNginx(){
yum -y install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perlddd
fileName="nginx-1.9.9"
package="${fileName}.tar.gz"
installDir="/usr/local/nginx"
if test ! -f ${package}
then
wget http://nginx.org/download/${package}
fi
tar zxvf $package
cd $fileName
./configure --prefix=${installDir}
make && make install
echo "安装完成"
/usr/local/nginx/sbin/nginx 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 80
/usr/local/nginx/sbin/nginx
fi
usleep 100000
pid=`cat /usr/local/nginx/logs/nginx.pid`
echo "nginx 已经启动,进程号为${pid}"
bin="${installDir}/sbin"
addToPATH ${bin}
createLink ${installDir}
}
installPHP(){
yum -y install expect
wget http://www.atomicorp.com/installers/atomic
expect <<EOF
set timeout -1
spawn sh ./atomic
expect "yes"
send "yes\r"
expect eof
EOF
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel php-mcrypt libmcrypt libmcrypt-devel
fileName="php-7.0.8"
package="${fileName}.tar.gz"
installDir="/usr/local/php7_fpm"
if test ! -f ${package}
then
wget http://cn2.php.net/distributions/${package}
fi
tar zxvf $package
cd $fileName
./configure --prefix=${installDir} \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--with-mcrypt \
--enable-fpm \
--with-config-file-path=${installDir}/etc
make && make install
echo "安装完成"
cp php.ini-development ${installDir}/etc/php.ini
cp ${installDir}/etc/php-fpm.conf.default ${installDir}/etc/php-fpm.conf
cp ${installDir}/etc/php-fpm.d/www.conf.default ${installDir}/etc/php-fpm.d/www.conf
cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x -R /etc/init.d/
/etc/init.d/php-fpm restart
echo "php-fpm 已经启动,进程号为9000"
echo "调用/etc/init.d/php-fpm控制服务"
bin="${installDir}/bin"
addToPATH ${bin}
createLink ${installDir}
}
function installMysql(){
cat > /etc/yum.repos.d/MariaDB.repo <<EOF
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1.11/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
yum install -y MariaDB-server MariaDB-client MariaDB-devel
echo "安装完成"
mysqladmin -u root password 'root'
service mysql start 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 3306
service mysql start
fi
chkconfig mysql on
echo "mysql 已经启动,进程号为3306 默认密码为root"
echo "调用service mysql start|stop|restart控制服务或则"
echo "/etc/init.d/mysql start|stop|restart"
}
function init(){
case $1 in
1)
installNginx
;;
2)
installMysql
;;
3)
installPHP
;;
4)
installMysql
installNginx
installPHP
;;
*)
echo 'You do not select a number between 1 to 4'
;;
esac
}
echo 'Input a number between 1 to 4'
echo '1.安装nginx 2.安装mysql'
echo '3.安装PHP 4.一键安装LNMP'
read mode
init ${mode}
source /etc/profile