Plesk之Hook(Event Handler)

进入Tools & Settings > Tools & Resources > Event Manager
点击Add Event Handler, 进入设置hook

  • Event: 选择触发事件
  • Priority: 选择优先级
  • User: 选择脚本执行用户
  • Command: 写入脚本路径(虽然也可以直接写shell命令,但是不推荐)

这里写图片描述

此次使用的脚本,选择的事件是Physical hosting created

#!/bin/bash
echo "NEW_DOMAIN_NAME = ${NEW_DOMAIN_NAME} ">> /tmp/test.log
echo "NEW_IP_ADDRESS  = ${NEW_IP_ADDRESS}  ">> /tmp/test.log
echo "NEW_IPV6_ADDRESS= ${NEW_IPV6_ADDRESS} ">> /tmp/test.log
echo "NEW_IP_TYPE     = ${NEW_IP_TYPE} "     >> /tmp/test.log
echo "NEW_SYSTEM_USER = ${NEW_SYSTEM_USER} ">> /tmp/test.log
echo "NEW_SYSTEM_USER_PASSWORD = ${NEW_SYSTEM_USER_PASSWORD} " >> /tmp/test.log
echo "NEW_SYSTEM_SHELL= ${NEW_SYSTEM_SHELL} ">> /tmp/test.log
echo "NEW_SSI_SUPPORT = ${NEW_SSI_SUPPORT} " >> /tmp/test.log
echo "NEW_PHP_SUPPORT = ${NEW_PHP_SUPPORT} " >> /tmp/test.log
echo "NEW_CGI_SUPPORT = ${NEW_CGI_SUPPORT} " >> /tmp/test.log
echo "NEW_MOD_PERL_SUPPORT = ${NEW_MOD_PERL_SUPPORT} "         >> /tmp/test.log
echo "NEW_APACHE_ASP_SUPPORT = ${NEW_APACHE_ASP_SUPPORT} "     >> /tmp/test.log
echo "NEW_ASP_SUPPORT = ${NEW_ASP_SUPPORT} "                   >> /tmp/test.log
echo "NEW_SSL_SUPPORT = ${NEW_SSL_SUPPORT} "                   >> /tmp/test.log
echo "NEW_WEB_STATISTICS = ${NEW_WEB_STATISTICS} "             >> /tmp/test.log
echo "NEW_CUSTOM_ERROR_DOCUMENTS = ${NEW_CUSTOM_ERROR_DOCUMENTS} " >> /tmp/test.log
echo "NEW_HARD_DISK_QUOTA = ${NEW_HARD_DISK_QUOTA} "           >> /tmp/test.log

一切就绪后,创建一个订阅后,ssh登陆服务器查看/tmp/test.log的内容如下

NEW_DOMAIN_NAME = wteuyhtbg.com
NEW_IP_ADDRESS  = 120.27.244.142
NEW_IPV6_ADDRESS=
NEW_IP_TYPE     =
NEW_SYSTEM_USER = hsdhjjmgsdksd
NEW_SYSTEM_USER_PASSWORD = Sefj03@0
NEW_SYSTEM_SHELL= /bin/false
NEW_SSI_SUPPORT = false
NEW_PHP_SUPPORT = true
NEW_CGI_SUPPORT = false
NEW_MOD_PERL_SUPPORT = false
NEW_APACHE_ASP_SUPPORT = false
NEW_ASP_SUPPORT =
NEW_SSL_SUPPORT = true
NEW_WEB_STATISTICS = webalizer
NEW_CUSTOM_ERROR_DOCUMENTS =
NEW_HARD_DISK_QUOTA = 0

这些参数就是可以在这个hook脚本中直接使用参数,所有的hook以及他们能使用的参数可以查阅Event Parameters Passed by Event Handlers

分享一个由Parallels的工程师Vladimir提供的,在创建订阅后自动安装WordPress的脚本(该脚本要求管理员创建一个叫WordPress autoService Plan)

#!/bin/bash

#define target plan name. All domains on this service plan will get Wordpress automatically (applies for subdomains too)

TARGET_PLAN="WordPress auto"

echo "--------------" >> /tmp/event_handler.log

/bin/date >> /tmp/event_handler.log # information on the event date and time

/usr/bin/id >> /tmp/event_handler.log # information on the user, on behalf of which the script was executed (to ensure control)

#we need random string generators. One simple for logins, and the other is more complex for secure passwords.
genpass_alphanum() {
        local l=$1
        [ "$l" == "" ] && l=16
        tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}
genpass() {
        local l=$1
        [ "$l" == "" ] && l=16
        tr -dc A-Za-z0-9\_\!\@\#\$\%\^\&\*\?< /dev/urandom | head -c ${l} | xargs
}

#We want to make we provide the latest Wordpress version. See http://docs.plesk.com/en-US/12.5/cli-linux/using-command-line-utilities/aps-application-catalog.63094/ for more details on “plesk bin aps” functionality

VER_RAW=(`plesk bin aps -gp|grep WordPress -A2 -B1|grep -v Vendor|awk '{print $2}'`)
VER_LEN=${#VER_RAW[*]}
VER=()
for i in {0,$((VER_LEN/3)),1}; do VER=(${VER[@]} ${VER_RAW[$((i*3-1))]}); done
IFS=$'\n' sorted=($(sort <<<"${VER[*]}"))
LVC=${#sorted[*]}
LATEST_VER=${sorted[$((LVC-1))]}
echo latest version is $LATEST_VER >>/tmp/event_handler.log
LATEST_PACKAGE_ID=`plesk bin aps -gp|grep $LATEST_VER -B3|head -1|awk '{print $2}'`
echo latest package id is $LATEST_PACKAGE_ID >>/tmp/event_handler.log

echo "domain ${NEW_DOMAIN_NAME}" >> /tmp/event_handler.log

# Find out plan name on which domain is created – see http://docs.plesk.com/en-US/12.5/cli-linux/using-command-line-utilities/site-sites.67067/ for reference

PLAN=`plesk bin site -i "${NEW_DOMAIN_NAME}"|grep -i plan|awk -F\" '{ print $2 }'`

echo $PLAN >> /tmp/event_handler.log

#if plan is right, we are installing WP, and for this we need to extract user name and email from domain owner, in order to provide WP with the same data, without having to make user correct it later

if [ "$PLAN" == "$TARGET_PLAN" ]; then
echo "${NEW_DOMAIN_NAME} is eligible for WP installation" >> /tmp/event_handler.log

USER=`plesk bin domain -i $NEW_DOMAIN_NAME|grep Owner|awk -F\( '{print $2}'|awk -F\) '{print $1}'`
EMAIL=`plesk bin user -i $USER|grep mail|awk '{print $2}'`

echo $USER $EMAIL >> /tmp/event_handler.log

#Generating password, login, database name and database user name
echo "installing WordPress version $LATEST_VER package ID $LATEST_PACKAGE_ID for domain ${NEW_DOMAIN_NAME}" >> /tmp/event_handler.log
echo "generating secure password" >> /tmp/event_handler.log
PASSWD=`genpass 8`_
#echo "Password $PASSWD" >> /tmp/event_handler.log
echo "generating db name and db user name" >> /tmp/event_handler.log
DBUSER=admin_`genpass_alphanum 6`
echo "DB User $DBUSER" >> /tmp/event_handler.log
DBNAME=wp_`genpass_alphanum 6`
echo "DB Name $DBNAME" >> /tmp/event_handler.log


# We need to generate an XML template for Plesk to provide an APS package. Each APS package has its own requirements to the template. In WP template we need to put user name, email address, password and locale – all this information will be used in Wordpress instance. User can edit it later.

echo "Generating template for WP" >> /tmp/event_handler.log

echo "<?xml version=\"1.0\"?>
<settings>
 <setting>
  <name>admin_email</name>
  <value>$EMAIL</value>
 </setting>
 <setting>
  <name>admin_name</name>
  <value>$NEW_SYSTEM_USER</value>
 </setting>
 <setting>
  <name>admin_password</name>
  <value>$PASSWD</value>
 </setting>
 <setting>
  <name>locale</name>
  <value>en-US</value>
 </setting>
 <setting>
  <name>title</name>
  <value>WordPress</value>
 </setting>
</settings>" > /tmp/template1.xml

#now ready to install Wordpress instance. First let’s log command to execute 
echo "plesk bin aps --import-package  /root/wordpress-4.7.2.zip -domain ${NEW_DOMAIN_NAME} -ssl false -url-prefix wordpress -db-name $DBNAME -db-user $DBUSER -passwd \"$PASSWD\" " >> /tmp/event_handler.log

#and fire it for real
plesk bin aps --import-package  /root/wordpress-4.7.2.zip -domain ${NEW_DOMAIN_NAME} -ssl false -url-prefix wordpress -db-name $DBNAME -db-user $DBUSER -passwd "$PASSWD" >> /tmp/event_handler.log 2>&1
#you might want to remove logging in the above line, as APS package installation produces a lot of output. 

echo "Finished installing WordPress for domain ${NEW_DOMAIN_NAME}" >> /tmp/event_handler.log
echo "Notifying user of successful installation" >> /tmp/event_handler.log
#Optional email notification sent out to the domain owner. Make sure your Plesk has correct FQDN and DNS is setup properly.
mail -s "Your WordPress is ready" "$EMAIL" <<EOF
Hello, $USER.
Your Wordpress installation is ready.
To access it, open ${NEW_DOMAIN_NAME}/wordpress
Login: $NEW_SYSTEM_USER
Password: $PASSWD
EOF


rm /tmp/template1.xml –f

#If plan name is not our target plan, skipping the installation
else echo  "${NEW_DOMAIN_NAME} does not need WP installation" >> /tmp/event_handler.log
fi

echo "--------------" >> /tmp/event_handler.log
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值