闲来无事,写脚本,install_wordpress
发布于: 2021 年 04 月 25 日
脚本特点
应用 bash 的高级变量赋值,关联数组,变量切片等特性实现一键安装 wordpress。
安装的 wordpress 基于 lamp 架构实现。(linux apache mysql/mariadb php)
安装完成后,使用者可以根据自己需求进行调试,优化。
例如 mysql 数据库的备份等。
wordpress 介绍
应用最广泛的个人博客软件。
脚本内容
#!/bin/bash
yum install gcc pcre-devel openssl-devel expat-devel libaio gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel lbzip2 -y
SWdir=/var/software/
APuser=apache
APgroup=apache
WPname=wordpress
declare -A soft_name
declare -A tar_name
declare -A dir_name
declare -A Enddir_name
#soft_name[mysql]='https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-8.5.4/bintar-linux-x86_64/mariadb-10.5.4-linux-x86_64.tar.gz'
soft_name[apr]='https://downloads.apache.org//apr/apr-1.7.0.tar.bz2'
soft_name[apru]='https://downloads.apache.org//apr/apr-util-1.6.1.tar.bz2'
soft_name[http]='https://downloads.apache.org//httpd/httpd-2.4.46.tar.bz2'
soft_name[php]='http://mirrors.sohu.com/php/php-7.4.7.tar.gz'
soft_name[wordpress]='https://cn.wordpress.org/wordpress-5.4.2-zh_CN.tar.gz'
#取后缀文件名
#第一种方式
#echo ${soft_name[*]}
#SoftTar=${SoftURL##*/}
#SoftDir=${SoftTar%%.tar*}
#第二种方式
for Name in ${!soft_name[@]};do
tar_name[$Name]=${soft_name[$Name]##*/}
dir_name[$Name]=${tar_name[$Name]%%.tar*}
if [ $Name = 'apru' ];then
Enddir_name[$Name]=${dir_name[$Name]%\-*}
else
Enddir_name[$Name]=${dir_name[$Name]%%\-*}
fi
done
function Fwget(){
[[ -d $SWdir ]] || mkdir $SWdir
wget -P $SWdir $1
}
function Uadd(){
for user in $@;do
useradd -r -s /sbin/nologin $user
done
}
function Unzip(){
tar -xf ${SWdir}$1 -C /usr/local/
}
function Ln(){
if [[ $2 =~ (apr|apr-util) ]];then
mv $1 /usr/local/httpd/srclib/$2
else
ln -s $1 /usr/local/$2
fi
}
function Source(){
Cpunum=$(lscpu|awk -F' ' '/CPU\(s\)/{print $2}'|head -n1)
cd ${1}${2}
if [[ $2 == 'httpd' ]];then
./configure \
--prefix=/apps/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork \
--with-included-apr
make -j ${Cpunum} && make install
cat > /usr/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
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/apps/httpd24/bin/apachectl start
#ExecStart=/apps/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/apps/httpd24/bin/apachectl graceful
#ExecReload=/apps/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
ln -s /apps/httpd24/bin/* /usr/local/bin/
elif [[ $2 == 'php' ]];then
./configure \
--prefix=/apps/php74 \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-zlib \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-mbstring \
--enable-xml \
--enable-sockets \
--enable-fpm \
--enable-maintainer-zts \
--disable-fileinfo
make -j ${Cpunum} && make install
cp ${1}${2}/php.ini-production /etc/php.ini
cp ${1}${2}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl daemon-reload
elif [[ $2 == 'mariadb' ]];then
Datadir=/data/mysql
[[ -d $Datadir ]] || mkdir -p $Datadir
# chown -R mysql.root ${Datadir}
cat > /etc/my.cnf <<MYSQL
[mysqld]
datadir=${Datadir}
skip_name_resolve=1
socket=${Datadir}/mysql.sock
log-error=${Datadir}/mysql_error.log
pid-file=${Datadir}/mysql.pid
[client]
port=3306
socket=${Datadir}/mysql.sock
MYSQL
sed -Ei "s@/usr/local/mysql@${1}${2}@g" ${1}${2}/support-files/mysql.server
cp ${1}${2}/support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
${1}${2}/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql --datadir=${Datadir}
ln -s /usr/local/mariadb/bin/* /usr/local/bin/
service mysqld start
mysqladmin -u root password 123.com
mysql -u root -p123.com -e "CREATE DATABASE wordpress CHARACTER SET=utf8mb4;
DROP USER ''@'localhost';
GRANT ALL ON wordpress.* TO wordpress@'%' IDENTIFIED BY 'wordpress';"
else
echo 附加包
fi
}
function ConfWP(){
site=/apps/httpd24/htdocs
mv /usr/local/${Enddir_name[wordpress]} ${site}/$WPname
cp ${site}/$WPname/wp-config-sample.php ${site}/$WPname/wp-config.php
sed -Ei "/DB_NAME/s@(.*, ').*('.*)@\1wordpress\2@g" ${site}/${WPname}/wp-config.php
sed -Ei "/DB_USER/s@(.*, ').*('.*)@\1wordpress\2@g" ${site}/${WPname}/wp-config.php
sed -Ei "/DB_PASSWORD/s@(.*, ').*('.*)@\1wordpress\2@g" ${site}/${WPname}/wp-config.php
sed -Ei "/DB_HOST/s@(.*, ').*('.*)@\1127.0.0.1\2@g" ${site}/${WPname}/wp-config.php
chown apache.root -R ${site}/${WPname}
}
function ConfPHP(){
Phpdir=/apps/php74/
cp -f ${Phpdir}etc/php-fpm.conf.default ${Phpdir}etc/php-fpm.conf
mkdir -p /etc/php.d/
cp -f ${Phpdir}etc/php-fpm.d/www.conf.default ${Phpdir}etc/php-fpm.d/www.conf
sed -Ei "/user /s@(user =).*@\1${APuser}@" ${Phpdir}etc/php-fpm.d/www.conf
sed -Ei "/group /s@(group =).*@\1${APgroup}@" ${Phpdir}etc/php-fpm.d/www.conf
cat > /etc/php.d/opcache.ini <<PHP
[opcache]
zend_extension=opcache.so
opcache.enable=1
PHP
}
function ConfApache(){
local Apachedir=/apps/httpd24
#取消下面两行的注释
#sed -i "s@#LoadModule proxy_module modules/mod_proxy.so@LoadModule proxy_module modules/mod_proxy.so@g" ${Apachedir}/conf/httpd.conf
#sed -i "s@#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so@LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so@g" ${Apachedir}/conf/httpd.conf
sed -Ei -e '/mod_proxy.so/s/#(.*)/\1/g' -e '/mod_proxy_fcgi.so/s/#(.*)/\1/g' ${Apachedir}/conf/httpd.conf
sed -Ei "s@(User ).*@\1apache@g" ${Apachedir}/conf/httpd.conf
sed -Ei "s@(Group ).*@\1apache@g" ${Apachedir}/conf/httpd.conf
#修改下面行
sed -Ei "/DirectoryIndex /s@([^ ]+)(.*)@\1 index.php\2@gp" ${Apachedir}/conf/httpd.conf
#加下面三行
sed -i "s@(DocumentRoot) \".*\"@\1 \"${Apachedir}/htdocs/${WPname}\"@" ${Apachedir}/conf/httpd.conf
sed -i "/AddType text\/html /a AddType application/x-httpd-php .php" ${Apachedir}/conf/httpd.conf
echo "ProxyRequests Off" >> ${Apachedir}/conf/httpd.conf
echo "ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000${Apachedir}/htdocs/${WPname}/\$1" >> ${Apachedir}/conf/httpd.conf
}
function Start(){
systemctl start $@
}
function main(){
Uadd mysql apache
#下载安装包
for paName in ${!soft_name[@]};do
Fwget ${soft_name[$paName]}
done
#解压安装包
for paName in ${!soft_name[@]};do
Unzip ${tar_name[$paName]}
done
#创建软连接
for paName in ${!soft_name[@]};do
Ln /usr/local/${dir_name[$paName]} ${Enddir_name[$paName]}
done
#编译安装
for paName in ${!soft_name[@]};do
Source /usr/local/ ${Enddir_name[$paName]}
done
#php 配置
ConfPHP
#wordpress 配置
ConfWP
#apache配置
ConfApache
#启用服务
Start httpd php-fpm
}
main
复制代码
划线
评论
复制
发布于: 2021 年 04 月 25 日阅读数: 8
版权声明: 本文为 InfoQ 作者【陈迪】的原创文章。
原文链接:【http://xie.infoq.cn/article/7ed948d09ecd4c1f5495c5c01】。文章转载请联系作者。
陈迪
关注
期望 2021.03.29 加入
容器云方向 正在学习Go......
评论