#!/bin/bash
#关闭防火墙和selinux
systemctl stop firewalld && setenforce 0 && echo "防火墙已关闭" || echo "请手动检查"
systemctl disable firewalld &> /dev/null
echo ">>> 1.nginx 安装进行中 ......"
#是否安装常用工具
read -p "是否安装常用工具:" num
case $num in
y)
             yum install -y sysstat elinks net-tools bash-completion
             echo "安装完成"
;;
n)
             break
;;
*)
             echo "请输入正确选项"
             exit
;;
esac
echo ">>> 2.正在加载yum源"
nginxflie="/etc/yum.repos.d/nginx.repo"
if [ ! -f $nginxfile ];then 
          touch /etc/yum.repos.d/nginx.repo
          else
          echo "文件已存在"
fi
#添加nginx.repo源
cat>/etc/yum.repos.d/nginx.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

#刷新yum库
yum makecache fast &> /dev/null
if [ $? -eq 0 ];then
         echo "刷新成功"
fi
#安装nginx并启动
rpm -qa | grep nginx &> /dev/null
if [ $? -ne 0 ];then
          yum -y install nginx
          systemctl start nginx
          systemctl enable nginx
fi
systemctl status nginx
if [ $? -ne 0 ];then
          systemctl start nginx
          systemctl enable nginx &> /dev/null
          echo "启动成功"
fi
#安装mysql
echo ">>> 3.正在升级mysql源"
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
mysql -v &> /dev/null
if [ $? -ne 0 ];then
          yum -y install mysql-community-server
          systemctl start mysqld
          systemctl enable mysqld
fi
systemctl status mysqld
if [ $? -ne 0 ];then
          systemctl start mysql-community-server
          systemctl enable mysqld
fi
sleep 3
echo ">>> 4.配置mysql文件"
echo "如有不明白,请参考:https://help.aliyun.com/document_detail/97251.html"
echo "输出初始密码:`grep 'temporary password' /var/log/mysqld.log | awk -F ' ' '{print $NF}'`"
echo "请记住密码!!!"
sleep 4
echo "运行命令配置MySQL的安全性"
echo "正在进入交互式设置,请按照自己的需要配置"
mysql_secure_installation

echo ">>> 5.安装PHP ......"
echo "添加epel源"
yum -y install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
echo "添加Webtatic源"
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
echo "安装PHP以及依赖包"
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb

if [ $? -ne 0 ];then
           echo "请检查脚本" || exit 4;
fi
echo ">>> 5.正在配置nginx文件"
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
cat > /etc/nginx/conf.d/php.conf << EOF
server {
          listen 80;
          server_name localhost;

          #access_log /var/log/nginx/host.access.log main;

          location / {
                index index.php index.html index.htm;
}
          location ~ .php$ {
                root /usr/share/nginx/html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
                include fastcgi_params;
     }
}
EOF
#测试网页
echo ">>> 6.启动PHP"
systemctl status php-fpm
if [ $? -ne 0 ];then
              systemctl start php-fpm
              systemctl enable php-fpm
fi
if [ -f /usr/share/nginx/html/phpinfo.php ];then
               echo "文件已存在"
else
               echo "<?php echo phpinfo(); ?>" > /usr/share/nginx/html/phpinfo.php
               systemctl reload nginx
fi

echo "部署已经完成!!!!浏览器:http://localhost/phpinfo.php"

 

 

###看完脚本!有需要的改

 

相关文章: