(七)利用shell脚本实现二进制安装LAMP架构

利用shell脚本实现二进制安装LAMP架构的word press

#!/bin/bash
. /etc/init.d/functions
pwd=`pwd`

#二进制安装mariadb
#-------------------------------------------------------------------
echo -e "\e[1;31m 二进制安装mariadb \e[0m"
#创建mysql用户
id mysql &> /dev/null || { useradd -r -d /data/mysql -s /sbin/nologin mysql ; action "创建mysql用户成功"; }

#安装依赖包
yum install  -y libaio perl-Data-Dumper ncurses-compat-libs mysql &> /dev/null && action "安装依赖包成功"

#创建mysql的家目录文件夹
mkdir -pv /data/mysql &> /dev/null && { chown mysql:mysql /data/mysql &> /dev/null; action "创建mysql的家目录文件夹成功"; }

#解压二进制程序
tar xvf mariadb-10.2.32-linux-glibc_214-x86_64.tar.gz -C /usr/local &> /dev/null && action "解压二进制程序成功"

#将原文件创建为软链接
ln -sv /usr/local/mariadb-10.2.32-linux-glibc_214-x86_64 /usr/local/mysql &> /dev/null && action "将原文件创建为软链接成功"
chown -R mysql.mysql /usr/local/mysql/

#准备配置文件
cp -a /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cat > /etc/my.cnf <<EOF
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock        
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
[ -a /etc/my.cnf ] && action "配置文件配置成功"

#创建数据库文件
cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql &> /dev/null && action "创建数据库文件成功"

#服务启动文件
cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start

#环境变量
{ echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh; chmod +x /etc/profile.d/mysql.sh; . /etc/profile.d/mysql.sh; } && action "环境变量创建成功"

#编译安装http2.4.46
#-------------------------------------------------------------------
echo -e "\e[1;31m 编译安装http2.4.46 \e[0m"
FILE=/apps/httpd

cd $pwd

#依赖包安装
yum -y install gcc make pcre-devel openssl-devel expat-devel bzip2 &> /dev/null && action "安装依赖包成功"

#解压相关源码包
tar xf httpd-2.4.46.tar.gz && action "httpd源码包解压成功"

tar xf apr-1.7.0.tar.bz2 && mv apr-1.7.0 httpd-2.4.46/srclib/apr && action "apr源码包解压成功"
tar xf apr-util-1.6.1.tar.bz2 && mv apr-util-1.6.1 httpd-2.4.46/srclib/apr-util && action "apr-util源码包解压成功"

#编译
[ -d $FILE ] || mkdir -p $FILE
cd httpd-2.4.46/
./configure --prefix=$FILE --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork  &> /dev/null && action "编译完成" || { action "编译失败" false; exit; }

#安装
make  -j `lscpu | grep "^cpu(s)" | awk '{print $NF}'` &> /dev/null  && { make install &> /dev/null; action "httpd安装完成"; }

#创建用户
id apache &> /dev/null || { useradd -s /sbin/nologin -r apache; action "创建apache用户成功"; }

#修改配置文件中的启动用户
{ sed -i 's/^User.*/User apache/' ${FILE}/conf/httpd.conf; sed -i 's/^Group.*/Group apache/' ${FILE}/conf/httpd.conf; } && action "启动用户修改成功"

#配置环境变量
{ echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh; source /etc/profile.d/httpd.sh; } && action "环境变量配置成功"

#配置man帮助
echo 'MANDATORY_MANPATH          /apps/httpd/man' >> /etc/man_db.conf && action "配置man帮助成功"

#创建服务启动文件
cat > /lib/systemd/system/httpd.service << EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=$FILE/bin/apachectl start
ExecReload=$FILE/bin/apachectl graceful
ExecStop=$FILE/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

[ -a /lib/systemd/system/httpd.service ] && action "创建服务启动文件成功"

#启动服务
systemctl daemon-reload
systemctl enable --now httpd.service &> /dev/null && action "httpd服务启动成功" || action "httpd服务启动失败,请检查配置文件" false

#编译安装httpd模块方式 php-7.3
#-------------------------------------------------------------------
echo -e "\e[1;31m 编译安装php-7.3 \e[0m"
FILE1=/apps/php

cd $pwd

#安装相关包
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel &> /dev/null && action "依赖包安装成功"

#解压源码包
tar xf php-7.3.15.tar.xz && action "php源码包解压成功"

#编译
[ -d $FILE1 ] || mkdir -p $FILE1
cd php-7.3.15/
./configure --prefix=$FILE1 --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/apps/httpd/bin/apxs --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-maintainer-zts --disable-fileinfo &> /dev/null && action "编译完成" || { action "编译失败" false; exit; }

#安装
make  -j `lscpu | grep "^cpu(s)" | awk '{print $NF}'` &> /dev/null  && { make install &> /dev/null; action "php安装完成"; }

#为php提供配置文件
cp php.ini-production /etc/php.ini && action "php配置文件配置成功"

#修改配置实现lamp架构
#-------------------------------------------------------------------
echo -e "\e[1;31m 修改配置实现lamp架构 \e[0m"

#编辑 apache 配置文件支持 php
{ echo -e "AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps" >> /apps/httpd/conf/httpd.conf; sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /apps/httpd/conf/httpd.conf; } && action "编辑apache配置文件支持php成功"
systemctl restart httpd

#在数据库中创建wordpress数据库和用户
{ mysql -e "create database wordpress;"; mysql -e "grant all on wordpress.* to wordpress@'10.0.0.%' identified by 'qwe123';"; } && action "wordpress数据库和用户创建成功"

#部署wordpress
#-------------------------------------------------------------------
echo -e "\e[1;31m 部署wordpress \e[0m"

cd $pwd

#解压
tar xf wordpress-5.4.2-zh_CN.tar.gz && action "wordpress解压成功"

#移动到wordpress到html文件下
mv wordpress /apps/httpd/htdocs/ && action "移动到wordpress到html文件下成功"

#修改wordpress文件权限
chown -R apache.apache /apps/httpd/htdocs/wordpress && action "修改wordpress文件权限成功"

echo -e "\e[1;33m LAMP部署wordpress已完成,打开http://`hostname -I`/wordpress页面进行安装 \e[0m"
上一篇:centos7安装配置docker-compose


下一篇:基于LAMP的Discuz论坛的实现(源码安装)