10.30 lamp作业

通过编写 shell 脚本实现二进制安装 LAMP 架构的 word press

. /etc/init.d/functions
START='\e[1;31m'
FINISH='\e[1;35m'
END='\e[0m'
IP=10.0.0.7
#mariadb
down_mariadb=https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.5.5/bintar-linux-x86_64/mariadb-10.5.5-linux-x86_64.tar.gz
wget_mariadb_dir=/usr/local
install_mariadb_dir=/data/mysql
#httpd
apr_down=https://mirror.bit.edu.cn/apache/apr/apr-1.7.0.tar.gz
apr_util_down=https://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
httpd_down=https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
install_httpd_dir=/apps/httpd
wget_dir=/usr/local/src

#php
install_php_dir=/apps/php
down_php=https://www.php.net/distributions/php-7.3.15.tar.xz
down_wordpress=https://wordpress.org/latest.tar.gz
wordpress_name=wordpress-5.4.2-zh_CN.zip

install_mariadb() {
echo -e "${START} 编译安装mariadb ${END}"
#检查是否以及安装mysql
[ -h ${wget_mariadb_dir}/mysql ] && {  echo "mysql 已经安装";exit; }

#安装包
 yum -y  install wget libaio ncurses-compat-libs  &> /dev/null

NAME=`echo ${down_mariadb} |awk -F/ '{print $NF}'`
FILE=`echo ${NAME} |sed -nr 's/(.*[0-9]).*/\1/p'`

#检查二进制文件
cd ${wget_mariadb_dir}
[ -f ${NAME} ] && action "mariadb文件" || wget ${down_mariadb} -P ${wget_mariadb_dir} &>/dev/null && action "mariadb文件"

#创建用户
id mysql &> /dev/null|| useradd -r  -d ${install_mariadb_dir} mysql
action "mysql用户"
#解压二进制文件
tar xf ${NAME}  
ln -s  ${FILE} mysql
chown -R root:root ${wget_mariadb_dir}/mysql/
action "二进制解压"

#安装mariadb
#配置文件
cd ${wget_mariadb_dir}/mysql
cat << EOF > /etc/my.cnf
[mysqld]
datadir=${install_mariadb_dir}
skip_name_resolve=on
EOF
action "配置文件"

#创建数据库文件
ln -s ${wget_mariadb_dir}/mysql/bin/* /usr/bin/
scripts/mysql_install_db --datadir=${install_mariadb_dir} --user=mysql &> /dev/null && action "编译完成" || { action "编译失败" false; exit; }
cp ${wget_mariadb_dir}/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld &> /dev/null
service mysqld start &> /dev/null
action "mysql安装"

#执行安全脚本
${wget_mariadb_dir}/mysql/bin/mysql_secure_installation  << EOF &> /dev/null


y
n
y
y
y
y
EOF
action "mysql安全脚本"

#关闭无秘登录,并设置密码
mysql -e 'ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("123456")'
mysql -uroot -p123456 -e 'create database wordpress'
mysql -uroot -p123456 -e 'create database discuz'
mysql -uroot -p123456 -e "grant all on wordpress.* to wpuser@'10.0.0.%' identified by 'wppass'"
mysql -uroot -p123456 -e "grant all on discuz.* to discuz@'10.0.0.%' identified by 'dispass'"
echo -e "${FINISH} 编译安装mariadb完成 ${END}"
}

install_httpd() {
echo -e "${START} 编译安装httpd ${END}"
apr_name=`echo ${apr_down}|sed -nr 's@.*/(.*)@\1@p'`
apr_util_name=`echo ${apr_util_down}|sed -nr 's@.*/(.*)@\1@p'`
httpd_name=`echo ${httpd_down}|sed -nr 's@.*/(.*)@\1@p'`
apr_dir=`echo ${apr_name}|sed -nr 's@(.*).tar.*@\1@p'`
apr_util_dir=`echo ${apr_util_name}|sed -nr 's@(.*).tar.*@\1@p'`
httpd_dir=`echo ${httpd_name}|sed -nr 's@(.*).tar.*@\1@p'`
 
#安装依赖包和所需的命令
yum -y install  gcc make pcre-devel openssl-devel expat-devel  &>/dev/null && action "安装依赖包"

#下载源码包
cd ${wget_dir}
[ -f ${httpd_name} ] && action "httpd文件" || wget  ${httpd_down} &>/dev/null ;action "httpd文件"
[ -f ${apr_name} ] && action "apr文件" || wget ${apr_down} &>/dev/null ;action "apr文件"
[ -f ${apr_util_name} ] && action "apr_util文件" || wget  ${apr_util_down} &>/dev/null ;action "apr_util文件"

#解压文件
tar xf ${httpd_name} && action "httpd解压完成" 
tar xf ${apr_name} && action "apr解压完成"
tar xf ${apr_util_name} && action "apr_util解压完成"

#将apr和apr-util源码和httpd源码合并
mv ${apr_dir} ${httpd_dir}/srclib/apr
mv ${apr_util_dir} ${httpd_dir}/srclib/apr-util


#编译安装
cd ${httpd_dir}
./configure --prefix=${install_httpd_dir} --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 -i "^cpu(s)" | awk '{print $NF}'` &> /dev/null  && make install &> /dev/null && action "编译安装"

#创建apache账号
id apache &> /dev/null|| useradd -r  -s /sbin/nologin apache && action "apache用户"

#修改配置文件
sed -i 's/^User.*/User apache/' ${install_httpd_dir}/conf/httpd.conf && sed -i 's/^Group.*/Group apache/' ${install_httpd_dir}/conf/httpd.conf &&action "配置文件"

# 创建软链接
ln -s ${install_httpd_dir}/bin/* /usr/bin/ 
action "软链接"

# 配置man帮助
echo "MANDATORY_MANPATH   ${install_httpd_dir}/man" >> /etc/man_db.conf && action "配置man帮助"

# 创建service unit文件,设置开机启动
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=${install_httpd_dir}/bin/apachectl start
ExecReload=${install_httpd_dir}/bin/apachectl graceful
ExecStop=${install_httpd_dir}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now httpd.service &>/dev/null && action "启动httpd"
echo -e "${FINISH} 编译安装httpd完成 ${END}"
} 
install_php() {
echo -e "${START} 编译安装php ${END}"
php_name=`echo ${down_php} |awk -F/ '{print $NF}'`
php_dir=`echo ${php_name} |sed -nr 's/(.*[0-9]).*/\1/p'`

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

#解压源码包
cd ${wget_dir}
[ -f ${php_name} ] && action "php文件" || wget ${down_php} &>/dev/null && action "php文件" 
tar xf ${php_name} && action "php源码包解压成功"

#编译
cd ${php_dir}
./configure --prefix=${install_php_dir}--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 -i "^cpu(s)" | awk '{print $NF}'` &> /dev/null  && { make install &> /dev/null; action "php安装完成"; }

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

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

#解压
cd ${wget_dir}
[ -f ${wordpress_name} ] && action "wordpress文件" || wget ${down_wordpress} &>/dev/null && action "wordpress文件" 
unzip  ${wordpress_name} &> /dev/null && action "wordpress解压成功"

#移动到wordpress到html文件下
mv wordpress ${install_httpd_dir}/htdocs/ && action "移动到wordpress到html文件下成功"

#修改wordpress文件权限
chown -R apache.apache ${install_httpd_dir}/htdocs/wordpress && action "修改wordpress文件权限成功"

echo -e "${FINISH} LAMP部署wordpress已完成,打开http://${IP}/wordpress页面进行安装 ${END}"
}
 
install_mariadb
install_httpd
install_php
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值