前端之家收集整理的这篇文章主要介绍了
Shell编程之if语法练习-实战LNMP全过程,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/bin/bash
# DATE:Wed Jan 17 1# ywyankerp@163.com
#this is a yankerp
# The Nginx service is being installed
ROOT_ID=0
ML=/usr/local/src
Nginx_ML=/usr/local/src/Nginx-1.12.2
PML=/usr/local/src/PHP-5.6.27
QD=`netstat -anpt | grep 80 | awk '{print $7}' | cut -d "/" -f2 | cut -d ":" -f1`
ADDR=`ip a | grep eno16777728$ | awk '{print $2}' | cut -d "/" -f1`
MMM=`netstat -anput | grep MysqLd | awk '{print $4}' | cut -d ":" -f4`
MAR=`rpm -qa | grep mariadb`
cd $ML && {
wget http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
mv Centos-7.repo /etc/yum.repos.d/Centos-Base.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
}
if [ $? -eq 0 ]
then
echo "Yum源更新完毕,开始安装!!!"
else
echo "Yum源error"
exit 1
fi
echo "Yum源配置成功"
if [ "$UID" -eq "$ROOT_ID" ]
then
echo "root用户运行中" && systemctl stop firewalld && yum install -y gcc gcc-c++ pcre-devel zlib-devel openssl-devel &> /dev/null
else
echo "请使用Root用户运行"
exit 1
fi
echo "依赖包安装完成!"
cd $ML || {
echo "进入目录失败!!!"
exit 1
}
wget http://Nginx.org/download/Nginx-1.12.2.tar.gz &> /dev/null && {
tar zxf Nginx-1.12.2.tar.gz && groupadd www && useradd -g www www -s /sbin/nologin
}
echo "Nginx包下载成功"
cd $Nginx_ML && {
./configure --prefix=/usr/local/Nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www &> /dev/null && make &> /dev/null && make install &> /dev/null
}
if [ $? -eq "0" ]
then
ln -s /usr/local/Nginx/sbin/Nginx /usr/local/sbin ;Nginx && echo "Nginx启动成功,请访问http://$ADDR:80"
else
echo "Nginx启动失败!!!"
exit 1
fi
#Start installing MysqL5.7!
echo "开始下载MysqL二进制安装包,请稍等..."
if [ -z "$MAR" ]
then
echo "开始安装MysqL"
fi
if [ -n "$MAR" ]
then
rpm -e mariadb-libs --nodeps && echo "卸载mariadb成功!正式安装MysqL"
fi
cd $ML || {
echo "进入目录失败!!!"
exit 1
}
wget http://mirrors.sohu.com/MysqL/MysqL-5.7/MysqL-5.7.18-linux-glibc2.5-x86_64.tar.gz &> /dev/null && {
tar zxf MysqL-5.7.18-linux-glibc2.5-x86_64.tar.gz && mv MysqL-5.7.18-linux-glibc2.5-x86_64 /usr/local/MysqL
cd /usr/local/MysqL/ && mkdir data && mkdir log && echo "export PATH=$PATH:/usr/local/MysqL/bin" >> /etc/profile
source /etc/profile && groupadd MysqL && useradd -r -g MysqL -s /bin/false MysqL
}
cat << EOF >/etc/my.cnf
[client]
socket=/usr/local/MysqL/MysqL.sock
[MysqLd]
basedir=/usr/local/MysqL
datadir=/usr/local/MysqL/data
pid-file=/usr/local/MysqL/data/MysqLd.pid
socket=/usr/local/MysqL/MysqL.sock
log_error=/usr/local/MysqL/log/MysqL.err
EOF
if [ ! -d /etc/my.cnf ]
then
chmod 750 data/ && chown -R MysqL . && chgrp -R MysqL . && bin/MysqLd --initialize --user=MysqL && cp /usr/local/MysqL/support-files/MysqL.server /etc/init.d/MysqLd && service MysqLd start &> /dev/null
else
echo "错误!!!"
fi
if [ $? -eq "0" ]
then
echo "MysqL正式安装完成杀青!"
else
echo "MysqL安装失败!!!"
exit 1
fi
#PHP install sccessy
yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel &> /dev/null
if [ $? -eq 0 ]
then
echo "依赖包安装成功!!!"
else
echo "依赖包安装失败!!!"
fi
cd $ML || {
echo "进入目录失败!!!"
exit 1
}
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz &> /dev/null && {
tar zxf libmcrypt-2.5.7.tar.gz && cd libmcrypt-2.5.7/ && ./configure --prefix=/usr/local/libmcrypt &> /dev/null && make &> /dev/null && make install &> /dev/null
}
if [ $? -eq 0 ]
then
echo "Libmcrypt安装成功!"
else
echo "Libmcrypt-ERROR!"
exit 1
fi
# PHP install
cd $ML || {
echo "进入目录失败!!!"
exit 1
}
wget http://cn2.PHP.net/distributions/PHP-5.6.27.tar.gz &> /dev/null && {
tar zxf PHP-5.6.27.tar.gz && cd PHP-5.6.27/ && ./configure --prefix=/usr/local/PHP5.6 --with-MysqL=MysqLnd --with-pdo-MysqL=MysqLnd --with-MysqLi=MysqLnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/PHP.d --with-bz2 --enable-maintainer-zts &> /dev/null
}
if [ $? -eq 0 ]
then
make &> /dev/null && make install &> /dev/null && echo "PHP编译安装成功,正在配置请稍等...."
else
echo "PHP安装失败"
fi
cd $PML || {
echo "进入目录失败!!!"
exit 1
}
cp PHP.ini-production /etc/PHP.ini && {
cp sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm && chmod +x /etc/init.d/PHP-fpm && chkconfig --add PHP-fpm && chkconfig PHP-fpm on
}
if [ $? -eq 0 ]
then
cp /usr/local/PHP5.6/etc/PHP-fpm.conf.default /usr/local/PHP5.6/etc/PHP-fpm.conf && echo "复制主配置成功"
else
echo "PHP权限失败,请重试!"
fi
if [ ! -d /usr/local/PHP5.6/etc/PHP-fpm.conf ]
then
sed -i 's#;pid = run/PHP-fpm.pid#pid = run/PHP-fpm.pid#g' /usr/local/PHP5.6/etc/PHP-fpm.conf
sed -i 's/listen = 127.0.0.1:9000/listen = 127.0.0.1:9000/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
sed -i 's/pm.max_children = 5/pm.max_children = 300/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
sed -i 's/pm.start_servers = 2/pm.start_servers = 20/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 20/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 100/g' /usr/local/PHP5.6/etc/PHP-fpm.conf
else
echo "NONONO"
fi
if [ $? -eq 0 ]
then
systemctl start PHP-fpm && echo "PHP启动成功"
else
echo "启动PHP失败"
fi
#配置Nginx解析PHP
if [ ! -d /usr/local/Nginx/conf/Nginx.conf ]
then
sed -i 's/index index.html index.htm;/index index.PHP index.html index.htm;/g' /usr/local/Nginx/conf/Nginx.conf
sed -i 's/# root html;/ root html;/g' /usr/local/Nginx/conf/Nginx.conf
sed -i 's/# fastcgi_pass 127.0.0.1:9000;/ fastcgi_pass 127.0.0.1:9000;/g' /usr/local/Nginx/conf/Nginx.conf
sed -i 's/# fastcgi_index index.PHP;/ fastcgi_index index.PHP;/g' /usr/local/Nginx/conf/Nginx.conf
sed -i 's*# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;* fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;*g' /usr/local/Nginx/conf/Nginx.conf
sed -i 's/# include fastcgi_params;/ include fastcgi.conf;/g' /usr/local/Nginx/conf/Nginx.conf
sed -i '71d' /usr/local/Nginx/conf/Nginx.conf && sed -i 'N;70a}' /usr/local/Nginx/conf/Nginx.conf
sed -i '65d' /usr/local/Nginx/conf/Nginx.conf && sed -i 'N;64alocation ~ \\.PHP$ {' /usr/local/Nginx/conf/Nginx.conf
else
echo "解析PHP失败!!!"
fi
touch /usr/local/Nginx/html/yankerp.PHP
cat << EOF >/usr/local/Nginx/html/yankerp.PHP
<?PHP
PHPinfo();
?>
EOF
if [ ! -d /usr/local/Nginx/html/yankerp.PHP ]
then
Nginx -s reload && systemctl restart PHP-fpm
else
echo "测试目录文件失败"
fi
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null && yum install -y sl &> /dev/null
sl
if [ $? -eq 0 ]
then
echo "LNMP安装成功,请您访问http://$ADDR/yankerp.PHP"
else
echo "安装失败"
fi
if [ $? -eq 0 ]
then
echo "LNMP安装成功,请您访问http://$ADDR/yankerp.PHP"
else
echo "安装失败"
fi
###################################################################################################This is a yankerp~