在 bash 脚本中重新启动 httpd 服务不起作用
restarting httpd service in bash script not working
我创建了一个 bash 脚本来在使用 Apache 和 Varnish 之间切换
但是重新启动 httpd 服务的命令最近不起作用
几个月前脚本运行良好
#!/bin/bash
echo "Switching between Apache and Varnish cache"
if grep -Fxq "apache_port=0.0.0.0:80" /var/cpanel/cpanel.config
then
sed -i '/apache_port/c\apache_port=0.0.0.0:8080' /var/cpanel/cpanel.config
else
sed -i '/apache_port/c\apache_port=0.0.0.0:80' /var/cpanel/cpanel.config
fi
/usr/local/cpanel/whostmgr/bin/whostmgr2 –updatetweaksettings &&
/scripts/rebuildhttpdconf &&
service httpd restart &&
service varnish restart &&
echo "Done"
我不知道为什么重新启动 oof httpd 不能正常工作
使用 iptables 端口重定向怎么样?
基本上,您的 varnish 和 apache 运行 同时在它们自己的非特权端口上运行,并将所有流量重定向到端口 80 到内核级别的 varnish 或 apache。
让 运行 在 0.0.0.0:6081 上清漆并在 0.0.0.0:8080 上使用 apache 并使用这些2 个命令集(在 root 或 sudo 下):
将流量切换到 apache(假设我们已经将其定向到 varnish):
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 && \
iptables -t nat -D PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 6081
# first command adds rule to redirect all traffic on interface eth0 (adjust as needed) from port 80 to port 8080, rule is added to the end of rules list, so already active rule redirecting traffic to 6081 is still in charge with higher priority
# second line deletes rule redirecting traffic from port 80 to port 6081, to new rule can come into effect. moreover, it's executed only if previous command (-A) was finished successfully.
要将其切换回清漆:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 6081
iptables -t nat -D PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
# inverted rules from above, adds redirection to 6081 and removes redirection to 8080 if addition was successful
优点:
- 没有清漆重新启动,所以没有冷缓存。
- 如果您先添加新的重定向规则然后删除仍然有效的旧规则,则总体上不会出现端口 80 中断。
- 更安全,如果添加第一条规则失败,跳过删除仍然有效的规则并报告问题。您仍在 运行ning 之前失败的尝试。只需根据需要使用脚本即可。
缺点:
- 没有缓存逐出,因为没有清漆重启。但我想这不是您尝试将流量切换到 apache 的原因。如果你需要的话,你可以单独驱逐清漆。 :)
我创建了一个 bash 脚本来在使用 Apache 和 Varnish 之间切换
但是重新启动 httpd 服务的命令最近不起作用
几个月前脚本运行良好
#!/bin/bash
echo "Switching between Apache and Varnish cache"
if grep -Fxq "apache_port=0.0.0.0:80" /var/cpanel/cpanel.config
then
sed -i '/apache_port/c\apache_port=0.0.0.0:8080' /var/cpanel/cpanel.config
else
sed -i '/apache_port/c\apache_port=0.0.0.0:80' /var/cpanel/cpanel.config
fi
/usr/local/cpanel/whostmgr/bin/whostmgr2 –updatetweaksettings &&
/scripts/rebuildhttpdconf &&
service httpd restart &&
service varnish restart &&
echo "Done"
我不知道为什么重新启动 oof httpd 不能正常工作
使用 iptables 端口重定向怎么样?
基本上,您的 varnish 和 apache 运行 同时在它们自己的非特权端口上运行,并将所有流量重定向到端口 80 到内核级别的 varnish 或 apache。
让 运行 在 0.0.0.0:6081 上清漆并在 0.0.0.0:8080 上使用 apache 并使用这些2 个命令集(在 root 或 sudo 下):
将流量切换到 apache(假设我们已经将其定向到 varnish):
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 && \
iptables -t nat -D PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 6081
# first command adds rule to redirect all traffic on interface eth0 (adjust as needed) from port 80 to port 8080, rule is added to the end of rules list, so already active rule redirecting traffic to 6081 is still in charge with higher priority
# second line deletes rule redirecting traffic from port 80 to port 6081, to new rule can come into effect. moreover, it's executed only if previous command (-A) was finished successfully.
要将其切换回清漆:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 6081
iptables -t nat -D PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
# inverted rules from above, adds redirection to 6081 and removes redirection to 8080 if addition was successful
优点:
- 没有清漆重新启动,所以没有冷缓存。
- 如果您先添加新的重定向规则然后删除仍然有效的旧规则,则总体上不会出现端口 80 中断。
- 更安全,如果添加第一条规则失败,跳过删除仍然有效的规则并报告问题。您仍在 运行ning 之前失败的尝试。只需根据需要使用脚本即可。
缺点:
- 没有缓存逐出,因为没有清漆重启。但我想这不是您尝试将流量切换到 apache 的原因。如果你需要的话,你可以单独驱逐清漆。 :)