温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

shell脚本一键部署LNMP架构

发布时间:2020-07-07 23:33:02 来源:网络 阅读:317 作者:wx5d8a17c45cb5b 栏目:系统运维

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 <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 $nginx_serpath
systemctl restart nginx.service
if test $? == 0
then 
    echo "nginx 服务启动完成!"
else
    echo "nginx 服务启动失败!"
    exit 1
fi

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 <<EOF
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
EOF

chown mysql.mysql /etc/my.cnf
#echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /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 <<EOF
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html\$fastcgi_script_name;
            include        fastcgi_params;
        }
EOF
sed -i '/# pass/ r b.txt' /usr/local/nginx/conf/nginx.conf 
sleep 3
rm b.txt
echo "Nginx配置文件设置完毕!"
echo "重启Nginx服务..."
systemctl restart nginx.service

echo "创建PHP首页..."
cd /usr/local/nginx/html/
mv index.html index.php
cat >index.php <<EOF
<?php
    phpinfo();
?>
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架构

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI