WordPress 命令行自动部署的完整脚本 [附中文语言包的阿里云网站更新]

前文介绍 WP CLI 自动部署的脚本有一些小缺陷,主要是需要先创建用户数据库授权,才能创建 wp-config.php,否则报 MySQL 1045 错误,也就是用户名或者密码的权限错误。

感兴趣的读者可以直接点击下载本脚本: https://download.yj777.cn/wordpress/auto-deploy/deploy-wp.sh

该脚本针对中国用户做了特别的优化。特别是中文语言包的下载,由于 GFW 的缘故,把中文语言包放置到了国内阿里云服务器。待优化的工作主要是 Apache DocumentRoot 的判定以及根据 Site URL 设置虚拟主机,还有就是 .htaccess 的创建。希望能在 V2.0 实现。

上海甬洁网络科技有限公司专注于企业的互联网转型,承接各类电子商务网站的建设,社会化媒体,特别是微信公众号的运营,微信小程序开发等;也承接中小企业的 IT 业务外包。

我们计划把 tech.yj777.cn 建设成国内 WordPress/WooCommerce 的社群。

先从下载服务开始。

#!/usr/bin/env bash
# WordPress 自动化安装
# Author: Albert Xu (albertxu AT freelamp.com)
# 创建日期: 2017-5-31
# Last Modified: 2017-6-15

# 需要配置的参数

# =========================================
APACHE_ROOT=/var/www/html

clear
echo -e “\n\t\tWordpress 自动部署系统 V1.0 (作者:徐永久 albertxu@freelamp.com)\n”
read -p “请输入站点安装目录: ” dir

WP_URL=http://localhost/report/$dir

read -p “请输入数据库名: ” DBNAME
read -p “请输入数据库用户名: ” DBUSER
# read -p “请输入数据库口令: ” DBPASS
# read -p “请输入站点 URL: ” WP_URL
echo
read -p “请输入站点名字: ” WP_TITLE
read -p “请输入站点管理员登录用户名: ” WP_ADMIN
read -p “请输入站点管理员邮箱: ” ADMIN_EMAIL
DBPASS=$(date +%s | sha256sum | base64 | head -c 32 ; echo)
WP_PASS=$(date +%s | sha256sum | base64 | head -c 10 ; echo)

WP_FOLDER=$APACHE_ROOT/$dir
# WP_FOLDER=$dir

# [ -d $WP_FOLDER ] && echo -e “\n文件夹已经存在!请尝试运行 rm -rf ${WP_FOLDER} ” && exit 1

WP=”/usr/bin/wp –allow-root –path=$WP_FOLDER”
echo -e “1. 自动下载/解压最新版本的 WordPress … \c”

mkdir ${WP_FOLDER}
[ ! -d $WP_FOLDER ] && echo “Folder not exist!” && exit 1
cd ${WP_FOLDER}
$WP core download

mysql “$DBNAME” -e “exit” 2>/dev/null
[ $? == 0 ] && echo -e “\n数据库已经存在!请尝试运行 mysql -e \”drop database ${DBNAME}\”” && exit 1

echo “2. 创建数据库账号 … ”
mysql mysql -e “grant all on ${DBNAME}.* to ${DBUSER}@localhost identified by ‘${DBPASS}’;flush privileges;”
[ $? != 0 ] && echo “创建失败! ” && exit 2

echo -e “3. 创建配置文件 …\c”
$WP core config –dbname=${DBNAME} –dbuser=${DBUSER} –dbpass=${DBPASS}
[ $? != 0 ] && echo “创建配置文件失败!” && exit 3

echo -e “4. 创建数据库 …\c”
$WP db create
[ $? != 0 ] && echo “创建失败!” && exit 4

echo -e “5. 创建实例 … \c”
$WP core install –url=$WP_URL –title=”${WP_TITLE}” –admin_user=”${WP_ADMIN}” –admin_email=”${ADMIN_EMAIL}” –admin_password=”${WP_PASS}”
[ $? != 0 ] && echo “创建 WordPress 实例 \”${WP_TITLE}\” 失败!” && exit 5

echo -e “6. 检查 站点 $WP_URL 的可用性 … \c”
TMP=/var/tmp/wp_test.out
wget -o $TMP $WP_URL
[ -z $TMP ] && echo “实例创建有问题,请检查 $WP_URL!” && rm $TMP && exit 6
rm $TMP
echo “成功”

echo “7: 其他任务, 安装激活中文语言包、主题、插件等 …”

echo -e “\t安装中文语言包 …”
# $WP core language install zh_CN –activate
LDIR=${WP_FOLDER}/wp-content/languages
[ ! -d ${LDIR} ] && mkdir ${LDIR}
cd ${LDIR}
L=core-default-4.7.5-zh_CN.zip
wget -q https://download.yj777.cn/wordpress/translation/$L
unzip -q $L
$WP language core activate zh_CN
rm $L

echo -e “\t安装主题 …”
# echo “结束: 请访问 $WP_URL 验证安装”
# $WP theme install https://downloads.wordpress.org/theme/skin.0.8.6.zip –activate
$WP theme install https://download.yj777.cn/wordpress/themes/skin.0.8.6.zip –activate

echo -e “\t安装配置插件 …”

$WP plugin delete hello
$WP plugin delete akismet

# Add and Remove Base Plugins
$WP plugin install kirki tabbed-login antispam-bee admin-menu-tree-page-view contact-form-7 advanced-custom-fields –activate

# Delete installed posts and create homepage
$WP post delete $($WP post list –post_type=’page’ –format=ids) –force
$WP post delete $($WP post list –post_type=’post’ –format=ids) –force
$WP post create –post_type=’page’ –post_title=”Home” –post_status=”publish”
$WP option update page_on_front 3
$WP option update show_on_front page

# Replace Uncategorized with a new Category as default
$WP term create category 新闻
$WP option update default_category 2
$WP term delete category 1

# Set Your Timezone – Most of you will want to change this
TIMEZONE=”Asia/Shanghai”
$WP option update timezone_string $TIMEZONE
$WP option update blogdescription “”
$WP option update date_format “Y年n月j日”
$WP option update time_format “ag:i”

# Options checkboxes the way I like them
$WP option update default_pingback_flag 0
$WP option update default_ping_status 0
$WP option update default_comment_status 0
$WP option update comment_registration 1
$WP option update comment_moderation 1
$WP option update comments_notify 0
$WP option update show_avatars 0

# 写 htaccess
# 设置 permalink
# Update rewrite (You’ll still need to resave the Settings > Permalinks Page)
# $WP rewrite structure ‘/%year%/%monthnum%/%postname%’
$WP rewrite structure ‘/%postname%/’ –hard
$WP rewrite flush –hard

# create a navigation bar
$WP menu create “Main Navigation”

# Create necessary Apache configs
# cd /etc/apache2/sites-available/
# sudo cp default.conf $url.conf
# sudo sed -i “s/localpress/$url/g” $url.conf
# cd ../sites-enabled/
# sudo ln -s ../sites-available/$url.conf $url.conf
# sudo service apache2 restart

# Update /etc/hosts – You’ll need to do this manually for your own machine
# sudo sed -i “s/#addmore/$url #addmore/g” /etc/hosts

# Spit out username and password details
echo “”
echo “”
echo “新站点 $WP_TITLE 已经建立”
echo “URL: $WP_URL/wp-admin/”
echo “用户名: $WP_ADMIN”
echo “口令: $WP_PASS”
echo “”

作者: 甬洁网络

--移动互联网&物联网技术提供商