Centos7 LNMP部署脚本(YUM)

 

#!/bin/bash

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
firewall-cmd --zone=public --add-port=80/tcp --add-port=443/tcp --permanent && firewall-cmd --reload
 
## Nginx Install ##
 
yum install epel-release -y
sleep 3
nginx_version_1=`yum info nginx.x86_64 |grep "版本" | awk -F ":" '{print$2}'`
 
echo -e "\e[37;4;42m===== 开始安装:Nginx-${nginx_version_1} =====\n\e[0m"
yum install nginx -y
 
systemctl start nginx && systemctl enable nginx
sleep 3
nginx_version_2=`nginx -v 2>&1 | awk -F "/" '{print $2}'`
nginx_status=`systemctl status nginx | awk -F ": " 'NR==3 {print$2}' | awk '{print$1}'`
 
if [[ ${nginx_version_1} == ${nginx_version_2} ]] && [[ ${nginx_status} == "active" ]];then
    echo -e "\e[1;33;44m===== nginx 已安装并成功运行 =====\n\e[0m"
else
    echo -e "\e[1;33;44m===== nginx 安装有误,已取消任务 =====\n\e[0m" && exit 0
fi
 
sleep 3
 
## PHP Install ##
yum -y install yum-utils https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --disable 'remi-php*'
yum-config-manager --enable remi-php74
sleep 3
 
php_version_1=`yum info php.x86_64|grep "版本" | awk -F ":" '{print$2}'`
 
echo -e "\e[37;4;42m===== 开始安装:PHP-${php_version_1} =====\n\e[0m"
yum -y install php php-cli php-common php-ldap php-devel php-bcmath php-embedded php-fpm php-gd php-mbstring php-mysqlnd php-pdo php-opcache php-xml php-soap
sleep 3
sed -i 's/user = apache/user = nginx/g;s/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf
sed -i 's/memory_limit = 128M/memory_limit = 1024M/g' /etc/php.ini
 
systemctl start php-fpm && systemctl enable php-fpm
sleep 3
php_version_2=`php -v | awk 'NR==1 {print$2}'`
php_status=`systemctl status php-fpm | awk -F ": " 'NR==3 {print$2}' | awk '{print$1}'`
 
if [[ ${php_version_1} == ${php_version_2} ]] && [[ ${php_status} == "active" ]];then
    echo -e "\e[1;33;44m===== PHP 已安装并成功运行 =====\n\e[0m"
else
    echo -e "\e[1;33;44m===== PHP 安装有误,已取消任务 =====\n\e[0m" && exit 0
fi
 
sleep 3
 
## PHP Access Nginx ##
echo -e "\e[37;4;42m===== 开始配置Nginx关联PHP ======\n\e[0m"
cat >> /etc/nginx/conf.d/lnmp.conf << 'EOF'
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;
        index index.php index.htm index.html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        location / {
        try_files $uri $uri/ /index.php?$query_string;
        }
        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.conf;
        }
    }
EOF
 
cat >> /usr/share/nginx/html/index.php << EOF
<?php
echo "Holle-World";
phpinfo();
?>
EOF
systemctl restart nginx
sleep 3
curl 127.0.0.1/index.php |grep "Holle-World"
if [[ $? -eq 0 ]];then
    echo -e "\e[1;33;44m===== Nginx关联PHP配置成功 ======\n\e[0m"
else
    echo -e "\e[1;33;44m===== Nginx关联PHP配置有误,请检查 =====\n\e[0m" && exit 0
fi
 
## Mysql Install ##
echo -e "\e[37;4;42m===== 开始安装Mysql =====\n\e[0m"
yum install wget -y
wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
rpm -ivh mysql80-community-release-el7-7.noarch.rpm
sed -i 's#http://repo.mysql.com/#https://mirrors.cloud.tencent.com/mysql/#g;s#/el/7/#-el7-#g' /etc/yum.repos.d/mysql-community.repo
yum makecache
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
yum install -y mysql-community-server
systemctl start mysqld
systemctl enable mysqld
 
mysql_status=`systemctl status mysqld | awk -F ": " 'NR==3 {print$2}' | awk '{print$1}'`
AuthPW=$(cat /var/log/mysqld.log  |grep root@localhost|awk -F ': ' '{print$2}')
if [[ ${mysql_status} == "active" ]];then
    echo -e "\e[1;33;44m==========恭喜!!!Mysql安装完成!==========\n初始密码是: ${AuthPW}\n\e[0m"
else
    echo -e "\e[1;33;44m===== Mysql 安装有误 =====\n\e[0m" && exit 0
fi
    
read -p "输入需要修改的Mysql密码:" new_pwd
if [ -z "$new_pwd" ];then
        echo -e "\e[1;33;44m===== 输入不能为空 =====\n\e[0m"
else
        echo -e "\e[1;33;44m===== 开始修改密码,请稍等。。。 =====\n\e[0m"
fi
 
mysql --connect-expired-password -uroot -p"${AuthPW}" -e "alter user 'root'@'localhost' identified by '${new_pwd}';"
 
if [ $? -eq 0 ];then
        echo -e "\e[1;33;44m===== Mysql 密码修改成功 =====\n\e[0m"
else
        echo -e "\e[1;33;44m===== 密码修改失败 =====\n\e[0m" && exit 0
fi

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值