shell脚本一键部署LNMP架构

shell脚本一键部署LNMP架构

详细过程请看上篇文章:LNMP搭建过程
软件包:链接:https://pan.baidu.com/s/1smqzl0usPj6rqsFUQCUAnA
提取码:tbvt

做网站、网站建设,成都做网站公司-创新互联公司已向上千企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。

下面给出脚本(适用于尚未搭建过的服务器):

软件包路径:

[root@localhost ~]# ls tar/LNMP-C7/
Discuz_X3.4_SC_UTF8.zip php-7.1.10.tar.bz2
MySQL-boost-5.7.20.tar.gz php-7.1.20.tar.bz2
ncurses-5.6.tar.gz php-7.1.20.tar.gz
nginx-1.12.2.tar.gz zend-loader-php5.6-linux-x86_64_update1.tar.gz
php-5.6.11.tar.bz2

1.main.sh(将三个脚本放入main.sh中逐一执行)

#!/bin/bash
./nginx.sh
./mysql.sh 123456  //mysqladmin 免交互时所需的密码可以自己设置
./php.sh

2.nginx.sh

#!/bin/bash
#该脚本是实现Nginx的手工编译安装
pkgpath=~/tar/LNMP-C7/
nginxpath=/usr/local/nginx/
nginx_serpath=/lib/systemd/system/nginx.service

systemctl stop firewalld.service 
setenforce 0
echo "正在安装所需环境相关软件包"
yum install -y gcc gcc-c++ make pcre pcre-devel zlib-devel &> /dev/null 
if test $? != 0
then 
    echo "环境安装有误,准备退出..."
    sleep 1
    exit 
fi

cd $pkgpath 
tar zxf nginx-1.12.2.tar.gz -C /opt/
#指定用户但不创建家目录
egrep "^nginx" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -M -s /sbin/nologin nginx
fi
#参数配置
echo "正在配置相关参数..."
sleep 2
cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module &> /dev/null
if test $? == 0
then 
    echo "配置参数正确!"
else
    echo "配置参数有误!"
    exit 1
fi

echo "正在编译..."

make &> /dev/null 
if test $? == 0
then 
    echo "编译成功!"
else
    echo "编译有误!"
    exit 1
fi

echo "正在安装..."
make install &> /dev/null
if test $? == 0
then 
    echo "安装成功!"
else
    echo "安装失败!"
    exit 1
fi
ln -s $nginxpathsbin/nginx /usr/local/sbin/

cat  >$nginx_serpath <

3.mysql.sh

#!/bin/bash
if test $# != 1
then 
    echo "error!"
    exit
fi
#手工编译安装MySQL脚本
pkgpath=~/tar/LNMP-C7/
mysqlpath=/usr/local/mysql 

#安装所需环境软件包
echo "安装所需环境软件包..."
yum install -y ncurses ncurses-devel bison cmake  expect &> /dev/null
if test $? != 0
then 
    echo "环境安装有误,准备退出..."
    sleep 1
    exit 
fi
#ncurses 字符终端包 bison 函数库 cmake 编译工具

#创建非登录用户
egrep "^mysql" /etc/passwd &> /dev/null
if [ $? -ne 0 ]
then
    useradd -s /sbin/nologin mysql
fi

#解压缩mysql软件包
cd  $pkgpath
tar zxf mysql-boost-5.7.20.tar.gz -C /opt/

#配置参数设置
echo "正在执行配置参数设置..."
sleep 2
cd /opt/mysql-5.7.20/
cmake \
-DCMKAE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 &> /dev/null
if test $? == 0
then 
    echo "配置参数正确!"
else
    echo "配置参数有误!"
    exit 1
fi
echo "正在编译..."
echo "编译所需时间较长,请耐心等待(20-45min)..."
sleep 3
make &> /dev/null
if test $? == 0
then 
    echo "编译成功!"
else
    echo "编译失败!"
    exit 1
fi
echo "正在安装..."
make install &> /dev/null 
if test $? == 0
then 
    echo "安装成功!"
else
    echo "安装失败!"
    exit 1
fi
cd /usr/local
chown -R mysql.mysql mysql/
cat >/etc/my.cnf <> /etc/profile
#echo 'export PATH' >> /etc/profile
#source /etc/profile
ln -s $mysqlpath/bin/* /usr/local/bin/ 
ln -s $mysqlpath/lib/* /usr/local/bin/
cd $mysqlpath 
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data >& /dev/null 

cp usr/lib/systemd/system/mysqld.service /lib/systemd/system/

systemctl start mysqld.service 

passwd=$1
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password 
expect {
    "Enter password:"
    { send "\r";exp_continue }
    "New password:"
    { send "$1\r";exp_continue }
    "Confirm new password:"
    { send "$1\r"; }
}
expect eof
EOF

echo "数据库登录密码设置完成,请稍后手动创建自己的数据库..."

4.php.sh

#!/bin/bash
#手工编译安装php
pkgpath=~/tar/LNMP-C7/
php_inifile=/usr/local/php/lib/php.ini

echo "正在安装所需软件环境"
yum install -y \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel &> /dev/null
if test $? != 0
then 
    echo "环境安装有误,准备退出..."
    sleep 1
    exit 
fi

echo "正在解压缩软件包(PHP)"
cd $pkgpath
tar jxf php-7.1.10.tar.bz2 -C /opt/
echo "正在配置相关参数..."
cd /opt/php-7.1.10/
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip &> /dev/null
if test $? == 0
then 
    echo "配置参数正确!"
else 
    echo "配置参数有误!"
    exit 1
fi
echo "正在编译,请耐心等待...(8-20min)"
make &> /dev/null
if test $? == 0
then 
    echo "编译成功!"
else 
    echo "编译失败!"
    exit 1
fi
echo "正在安装,马上就好..."
make install &> /dev/null 
if test $? == 0
then 
    echo "安装成功!"
else 
    echo "安装失败!"
    exit 1
fi
#进行相关优化和必要配置
echo "php有三个配置文件 php.ini:核心配置文件 php-fpm.conf:进程服务配置文件 www.conf:扩展配置文件"
echo "进行核心文件配置..."
cp php.ini-development $php_inifile
sed -i '/mysqli.default_socket =/ s/$/ \/usr\/local\/mysql\/mysql.sock/p' $php_inifile
sed -i '/;date.timezone =/ s/^;//' $php_inifile
sed -i '/date.timezone =/ s/$/ Asia\/Shanghai/p' $php_inifile
echo "核心配置文件配置完毕!"
echo "进行进程服务配置文件设置..."
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
sed -i '/;pid/ s/^;//' /usr/local/php/etc/php-fpm.conf 
echo "进程服务配置文件配置完毕!"

echo "进行扩展配置文件配置..."
cd /usr/local/php/etc/php-fpm.d/ 
cp www.conf.default www.conf
echo "配置完成!"

echo "启动中..."
/usr/local/php/sbin/php-fpm -c $php_inifile
echo "初始化完成,创建命令软链接..."
ln -s /usr/local/php/bin/* /usr/local/bin/
echo "更改Nginx主配置文件,以支持PHP功能"
cat > b.txt <index.php <
EOF
echo "php测试文件写入完毕!"

5.创建自己的数据库

mysql -uroot -p (交互输入密码脚本给的密码是123456)
create database mydb;
grant all on mydb. to 'mydbuser'@'%' identified by '123123';
grant all on mydb.
to 'mydbuser'@'localhost' identified by '123123';
flush privileges;
quit

6.Discuz论坛搭建配置脚本discu.sh

#!/bin/bash
echo "discuz论坛设置..."
cd ~/tar/LNMP-C7/
unzip Discuz_X3.4_SC_UTF8.zip -d /opt/  &> /dev/null 
cd /opt/dir_SC_UTF8/

cp -r upload/ /usr/local/nginx/html/mydb
cd /usr/local/nginx/html/mydb/
chown -R root:nginx ./config/
chown -R root:nginx ./data/
chown -R root:nginx ./uc_client/
chown -R root:nginx ./uc_server/
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/

之后的步骤操作参照上篇文章:手工编译搭建LNMP架构


标题名称:shell脚本一键部署LNMP架构
网页链接:http://azwzsj.com/article/pijsoj.html