乐趣区

关于shell:Centos7一键编译安装LNMP脚本

#!/bin/bash

#具体版本号
NGINX_V=1.20.0
PHP_V=7.4.2
MYSQL_V=5.7.37
TMP_DIR=/tmp 
INSTALL_DIR=/usr/local

function install_nginx() {
#下载依赖
    yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
    #装置配置
    #下载 Nginx
    cd ${TMP_DIR}
    yum install -y wget && wget -c wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz
    #解压源码
    tar -zxvf ${TMP_DIR}/nginx-${NGINX_V}.tar.gz
    mv nginx-${NGINX_V} nginx;cd nginx;
    #预编译配置
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
    sleep 2s
    #编译装置
    make && make install
    #以服务启动
    cd /usr/lib/systemd/system;
    cat > nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
  
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target
EOF
systemctl restart firewalld;firewall-cmd --reload;
systemctl start nginx;systemctl enable nginx;
systemctl status nginx.service;
}

function install_php() {

    cd $TMP_DIR
    yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
    wget -c http://mirrors.sohu.com/php/php-${PHP_V}.tar.bz2 &&     
    tar -jxvf php-${PHP_V}.tar.bz2 && cd php-${PHP_V} &&
    mv php-${PHP_V} $INSTALL_DIR/php
    cd $INSTALL_DIR/php;
    ./configure --prefix=$INSTALL_DIR/php --with-config-file-path=$INSTALL_DIR/php/etc
    sleep 1s;
    if [$? -eq 0];then
    make ZEND_EXTRA_LIBS="-liconv" &&make install
    echo -e "33[32m PHP 服务装置胜利! 33[0m"
    else
    echo -e "33[32m PHP 服务装置失败, 请检测配置文件! 33[0m"
    exit
    fi
}

function install_mysql(){
    MYSQL_BASE=/usr/local/mysql

    cd $TMP_DIR
        file="mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz"
    if [! -f $file]; then
        echo "File not found!"
        yum install -y wget && wget -c wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz;
        echo "下载实现, 正在解压.......";
        tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
        mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
        cd /usr/local/mysql
        #exit 0
    fi

    echo "创立用户组"
    userdel mysql;
    groupadd mysql;
    useradd -r -g mysql mysql;
    mkdir -p  /data/mysql;
    chown mysql:mysql -R /data/mysql;

cd /etc
echo "写入配置文件"
cat > my.cnf <<EOF
[mysqld]
bind-address=0.0.0.0                    #绑定地址运行近程连贯
port=3306                               #Mysql 凋谢的端口
user=mysql                              #数据库登录用户
basedir=/usr/local/mysql                #Mysql 装置的绝对路径
datadir=/data/mysql                     #Mysql 数据寄存的绝对路径
socket=/tmp/mysql.sock                  #套接字文件
log-error=/data/mysql/mysql.err         #mysql 生成的谬误日志寄存的门路
pid-file=/data/mysql/mysql.pid          #为 mysqld 程序指定一个寄存过程 ID 的文件
character_set_server=utf8mb4            #数据库字符编码
symbolic-links=0                        #是否开启链接符号
explicit_defaults_for_timestamp=true    #数据库 timestamp 类型的列自动更新
EOF

echo "初始化 Mysql"
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
sleep 2s

ehco "启动 mysql"
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start
service mysql status
ln -s  /usr/local/mysql/bin/mysql    /usr/bin

echo "获取 mysql 初始密码"
PASSWORD=`cat /data/mysql/mysql.err |grep "temporary password"|awk -F"root@localhost:" '{print $2}'`
echo "批改 mysql 明码"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock --connect-expired-password -uroot -p${PASSWORD} -e "ALTER USER'root'@'localhost'IDENTIFIED BY'root@.com';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "USE mysql;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "UPDATE user SET host ='%'WHERE user ='root';"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "FLUSH PRIVILEGES;"
$MYSQL_BASE/bin/mysql --socket=/tmp/mysql.sock -uroot -proot@.com -e "exit;"

echo "重启数据库"
service mysql restart;
service mysql status;

echo "以服务项启动"
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld;
chmod +x /etc/init.d/mysqld;
chkconfig --add mysqld;
chkconfig --list;
firewall-cmd --zone=public --add-port=3306/tcp --permanent;
firewall-cmd --reload;
firewall-cmd --list-all;

}


function install_info(){
    echo "=========> Nginx 信息 <========="
    echo "Nginx : $NGINX_V"
    echo "装置目录: /usr/local/"
        
    echo "=========> MYSQL 信息 <========="
    echo "数据库版本 : 5.7.37"
    echo "数据库明码 : root@.com"
    echo "数据库端口 : 3306"
    echo "BASEDIR 目录: /usr/local/mysql"
    echo "DATADIR 目录: /data/mysql"

    echo "==========> PHP 信息 <=========="
    echo "PHP 版本 : $PHP_V"
    echo "装置目录 : /usr/local/"
}

function main(){
    install_nginx
    install_mysql
    install_php
    install_info
}
main
退出移动版