shell脚本 一键部署 Lamp 架构

Posted wl.88888888

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本 一键部署 Lamp 架构相关的知识,希望对你有一定的参考价值。

shell脚本一键部署

Lamp 一键部署

注:记得把所需要的软件包,先放到/opt目录中;以及该机子的内存大小、核数先设置好!

#!/bin/bash
#先配置好yum
echo -e "----正在验证当前为仅主机还是NAT模式----"
ping -c 2 -w 2 www.baidu.com &> /dev/null

if [ $? -eq 0 ];then echo -e "检测当前为NAT模式,为您配置在线yum源"
    mkdir -p /etc/yum.repos.d/repo.bak
    mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
    wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null

    yum clean all &> /dev/null
    yum list &> /dev/null

    echo -e "在线源已配置完成"
else
    echo -e "检测当前为仅主机模式,为您配置本地yum源"
    mount /dev/sr0 /mnt &> /dev/null
    cd /etc/yum.repos.d/
    mkdir -p /etc/yum.repos.d/repo.bak

    mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null

echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null

     df -h | grep "/mnt"

     if [ $? -ne 0 ];then
     echo -e "检测当前为仅主机模式,但光盘未挂载!"
else
     echo -e "本地yum源已配置完成"
     fi
fi

#接下来,正式进入LAMP安装模式的第一块(编译安装Apache服务),先关闭防火墙
read -p "输入你要配置的域名:" a
systemctl stop firewalld
setenforce 0
systemctl disable firewalld &> /dev/null

#安装环境依赖包
yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl
if [ $? = 0 ];then
  echo  -e "环境依赖包安装完成!"
fi

#把相应安装包的放到/opt目录下,进行解压,并配置软件模块
cd /opt
tar zxvf apr-1.6.2.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2

mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util

cd /opt/httpd-2.4.29/
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi

if [ $? = 0 ];then
echo -e "软件模块安装完成!"
fi

#编译及安装
make -j4 && make install
if [ $? = 0 ];then
echo -e "编译安装完成!"
fi

#优化配置文件路径
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

#添加Httpd系统服务
echo '[Unit]
Description=The Apache HTTP Server                                      
After=network.target                                                    
[Service]
Type=forking                                                            
PIDFile=/usr/local/httpd/logs/httpd.pid                                 
ExecStart=/usr/local/bin/apachectl $OPTIONS                             
ExecReload=/bin/kill -HUP $MAINPID                              
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/httpd.service

#systemctl start httpd.service
#systemctl enable httpd.service

if [ $? = 0 ];then
  echo -e "httpd系统服务添加成功"
fi

#修改httpd服务配置文件,先备份
cp /etc/httpd.conf{,.bak}
b=`ifconfig | grep "inet" | awk 'NR==1 {print $2}'`

sed -i '51c Listen '$b':80' /etc/httpd.conf.bak
sed -i '52 s/^/#/' /etc/httpd.conf.bak
sed -i '197c ServerName '$a':80' /etc/httpd.conf.bak

cat /etc/httpd.conf.bak > /etc/httpd.conf

httpd -t
if [ $? = 0 ];then
echo -e "httpd配置文件修改正确"
fi

systemctl start httpd.service
netstat -antp | grep ":80"

#访问验证
echo "$b $a" >> /etc/hosts
if [ $? = 0 ];then
echo -e "请去浏览器验证 访问IP和域名"
fi
#curl http://192.168.116.100
#curl http://www.nj.com

#编译安装mysqld服务
#将安装mysql所需软件包传到/opt目录下,并解压
cd /opt
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz

#安装环境依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake
if [ $? = 0 ];then
echo -e "环境依赖包安装完成"
fi

#配置软件模块
mv boost_1_59_0 /usr/local/boost
cd /opt/mysql-5.7.17/
cmake \\
-DCMAKE_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_EXTRA_CHARSETS=all \\
-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=/usr/local/boost \\
-DWITH_SYSTEMD=1


if [ $? = 0 ];then
echo "模块配置完成"
fi

#编译安装
make -j4  && make install
if [ $? = 0 ];then
echo "编译安装完成"
fi

#创建mysql用户
useradd -M -s /sbin/nologin  mysql

#修改mysql配置文件
echo '[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
auto-rehash

[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
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
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' > /etc/my.cnf

#更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

#设置路径环境变量
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile

#初始化数据库
cd /usr/local/mysql/bin
./mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data

#添加mysql系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
netstat -anpt | grep 3306

rpm -q expect
if [ $? = 1 ];then
yum -y install expect
fi

source /etc/profile
#修改mysql登录密码
/usr/bin/expect <<EOF
spawn mysqladmin -u root -p password "123456"
expect "Enter password:"
send "\\r"
expect eof
EOF

#授权远程登录
/usr/bin/expect <<EOF
spawn mysql -u root -p
expect "Enter password:" {send "123456\\r"}
expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by '123456';\\r"}
expect "mysql>" {send "show databases;\\r"}
expect "mysql>" {send "quit\\r"}
expect eof
EOF

source /etc/profile
echo "----mysql安装完成----"

#编译安装php解析环境
#将安装包传到/opt目录下,并解压
cd /opt
tar jxvf php-7.1.10.tar.bz2

#安装GD库和GD库相关联的程序,用来处理和生成图片
yum -y install \\
gd \\
libjpeg libjpeg-devel \\
libpng libpng-devel \\
freetype freetype-devel \\
libxml2 libxml2-devel \\
zlib zlib-devel \\
curl curl-devel \\
openssl openssl-devel

#配置软件模块
cd /opt/php-7.1.10/
./configure \\
--prefix=/usr/local/php7 \\
--with-apxs2=/usr/local/httpd/bin/apxs \\
--with-mysql-sock=/usr/local/mysql/mysql.sock \\
--with-config-file-path=/usr/local/php7 \\
--with-mysqli \\
--with-zlib \\
--with-curl \\
--with-gd \\
--with-jpeg-dir \\
--with-png-dir \\
--with-freetype-dir \\
--with-openssl \\
--enable-mbstring \\
--enable-xml \\
--enable-session \\
--enable-ftp \\
--enable-pdo \\
--enable-tokenizer \\
--enable-zip

#编译及安装
make -j4 && make install

#复制模版文件作为PHP 的主配置文件,并进行修改
cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini

sed -i '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php7/php.ini
sed -i '939c date.timezone = Asia/Shanghai' /usr/local/php7/php.ini

#优化把PHP 的可执行程序文件放入路径环境变量的目录中便于系统识别
ln -s /usr/local/php7/bin/* /usr/local/bin/
#php -m

#修改httpd 服务的配置文件,让apache支持PHP
cp /etc/httpd.conf{,.bak1}
sed -i '393a AddType application/x-httpd-php .php\\nAddType application/x-httpd-php-source .phps' /etc/httpd.conf.bak1
sed -i '256c DirectoryIndex index.html index.php' /etc/httpd.conf.bak1

cat /etc/httpd.conf.bak1 > /etc/httpd.conf

cat /etc/httpd.conf | grep "LoadModule php7_module"
cat /etc/httpd.conf | grep "modules/libphp7.so"

#验证PHP 测试页
rm -rf /usr/local/httpd/htdocs/index.html
echo '<?php
phpinfo();
?>' > /usr/local/httpd/htdocs/index.php

systemctl restart httpd.service
if [ $? = 0 ];then
echo -e "---服务重启成功,请前往浏览器验证php测试页---"
fi
#浏览器访问
#http://192.168.116.100

注:亲,写这种脚本服务架构,建议分开一块块的写以及测试,免得排错排到心态爆炸

以上是关于shell脚本 一键部署 Lamp 架构的主要内容,如果未能解决你的问题,请参考以下文章

单台主机一键编译部署LAMP+wordpress+discuz系统的shell脚本

centos一键部署Lamp,shell脚本和搭建教程

shell脚本 一键部署 Lnmp 及社区论坛 服务架构

Shell脚本自动部署(编译)LAMP平台

LNMP源码编译安装及一键部署LNMP架构

LAMP(apache/httpd+mysql+php)环境/架构 一键安装脚本