千家信息网

shell脚本一键部署LNMP架构

发表于:2024-11-22 作者:千家信息网编辑
千家信息网最后更新 2024年11月22日,shell脚本一键部署LNMP架构详细过程请看上篇文章:LNMP搭建过程软件包:链接:https://pan.baidu.com/s/1smqzl0usPj6rqsFUQCUAnA提取码:tbvt下面
千家信息网最后更新 2024年11月22日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.servicesystemctl stop firewalld.service setenforce 0echo "正在安装所需环境相关软件包"yum install -y gcc gcc-c++ make pcre pcre-devel zlib-devel &> /dev/null if test $? != 0then     echo "环境安装有误,准备退出..."    sleep 1    exit ficd $pkgpath tar zxf nginx-1.12.2.tar.gz -C /opt/#指定用户但不创建家目录egrep "^nginx" /etc/passwd >& /dev/nullif [ $? -ne 0 ]then    useradd -M -s /sbin/nologin nginxfi#参数配置echo "正在配置相关参数..."sleep 2cd /opt/nginx-1.12.2/./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module &> /dev/nullif test $? == 0then     echo "配置参数正确!"else    echo "配置参数有误!"    exit 1fiecho "正在编译..."make &> /dev/null if test $? == 0then     echo "编译成功!"else    echo "编译有误!"    exit 1fiecho "正在安装..."make install &> /dev/nullif test $? == 0then     echo "安装成功!"else    echo "安装失败!"    exit 1filn -s $nginxpathsbin/nginx /usr/local/sbin/cat  >$nginx_serpath <

3.mysql.sh

#!/bin/bashif test $# != 1then     echo "error!"    exitfi#手工编译安装MySQL脚本pkgpath=~/tar/LNMP-C7/mysqlpath=/usr/local/mysql #安装所需环境软件包echo "安装所需环境软件包..."yum install -y ncurses ncurses-devel bison cmake  expect &> /dev/nullif test $? != 0then     echo "环境安装有误,准备退出..."    sleep 1    exit fi#ncurses 字符终端包 bison 函数库 cmake 编译工具#创建非登录用户egrep "^mysql" /etc/passwd &> /dev/nullif [ $? -ne 0 ]then    useradd -s /sbin/nologin mysqlfi#解压缩mysql软件包cd  $pkgpathtar zxf mysql-boost-5.7.20.tar.gz -C /opt/#配置参数设置echo "正在执行配置参数设置..."sleep 2cd /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/nullif test $? == 0then     echo "配置参数正确!"else    echo "配置参数有误!"    exit 1fiecho "正在编译..."echo "编译所需时间较长,请耐心等待(20-45min)..."sleep 3make &> /dev/nullif test $? == 0then     echo "编译成功!"else    echo "编译失败!"    exit 1fiecho "正在安装..."make install &> /dev/null if test $? == 0then     echo "安装成功!"else    echo "安装失败!"    exit 1ficd /usr/localchown -R mysql.mysql mysql/cat >/etc/my.cnf <> /etc/profile#echo 'export PATH' >> /etc/profile#source /etc/profileln -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 <<-EOFspawn 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 eofEOFecho "数据库登录密码设置完成,请稍后手动创建自己的数据库..."

4.php.sh

#!/bin/bash#手工编译安装phppkgpath=~/tar/LNMP-C7/php_inifile=/usr/local/php/lib/php.iniecho "正在安装所需软件环境"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/nullif test $? != 0then     echo "环境安装有误,准备退出..."    sleep 1    exit fiecho "正在解压缩软件包(PHP)"cd $pkgpathtar 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/nullif test $? == 0then     echo "配置参数正确!"else     echo "配置参数有误!"    exit 1fiecho "正在编译,请耐心等待...(8-20min)"make &> /dev/nullif test $? == 0then     echo "编译成功!"else     echo "编译失败!"    exit 1fiecho "正在安装,马上就好..."make install &> /dev/null if test $? == 0then     echo "安装成功!"else     echo "安装失败!"    exit 1fi#进行相关优化和必要配置echo "php有三个配置文件 php.ini:核心配置文件 php-fpm.conf:进程服务配置文件 www.conf:扩展配置文件"echo "进行核心文件配置..."cp php.ini-development $php_inifilesed -i '/mysqli.default_socket =/ s/$/ \/usr\/local\/mysql\/mysql.sock/p' $php_inifilesed -i '/;date.timezone =/ s/^;//' $php_inifilesed -i '/date.timezone =/ s/$/ Asia\/Shanghai/p' $php_inifileecho "核心配置文件配置完毕!"echo "进行进程服务配置文件设置..."cd /usr/local/php/etc/cp php-fpm.conf.default php-fpm.confsed -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.confecho "配置完成!"echo "启动中..."/usr/local/php/sbin/php-fpm -c $php_inifileecho "初始化完成,创建命令软链接..."ln -s /usr/local/php/bin/* /usr/local/bin/echo "更改Nginx主配置文件,以支持PHP功能"cat > b.txt <index.php <EOFecho "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/bashecho "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/mydbcd /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架构

0