Apache 2:从 Linux 中的 bash 脚本调用“a2ensite”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14390764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:17:17  来源:igfitidea点击:

Apache 2: calling 'a2ensite' from a bash script in Linux

linuxbashapache2

提问by Gabi Barrientos

I am currently writing an admin page for my webserver, to make it easier on myself to create new apache domains from my browser. Everything is pretty much working as I want it to, except for one thing.

我目前正在为我的网络服务器编写一个管理页面,以便我自己更轻松地从我的浏览器创建新的 apache 域。除了一件事,一切都如我所愿。

To elaborate: I have a cron job on my server running a bash script as root that checks a file containing a list of domain names that I want to be created. If the file contains a domain name, it automatically creates a new virtual host for this domain, edits my hosts file, and restarts the server. This all works perfectly, however what I would like for the script to do, is that it activates the domain that it automatically creates before it restarts the server. I tried doing this using apache 2's a2ensitecommand, however the script returns an error saying the command is not found.

详细说明:我的服务器上有一个 cron 作业,它以 root 身份运行 bash 脚本,该脚本检查包含我要创建的域名列表的文件。如果文件包含域名,它会自动为这个域创建一个新的虚拟主机,编辑我的主机文件,并重新启动服务器。这一切都很完美,但是我希望脚本做的是,它在重新启动服务器之前激活它自动创建的域。我尝试使用 apache 2 的a2ensite命令执行此操作,但是脚本返回一个错误,指出找不到该命令。

Is there a way to call this command from a bash script, or is there an alternative to this command that I can call?

有没有办法从 bash 脚本调用这个命令,或者我可以调用这个命令的替代方法?

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Perleone

$ which a2ensite
/usr/sbin/a2ensite

Usually, cron has a quite restrictive $PATH, not including /usr/sbinor /sbin, which are system binaries (for use by root). It's always a good idea to use fully qualified path names. So either call /usr/bin/a2ensitein your script, or define a variable:

通常,cron 有一个非常严格的 $PATH,不包括/usr/sbin/sbin,它们是系统二进制文件(供 root 使用)。使用完全限定的路径名​​总是一个好主意。所以要么调用/usr/bin/a2ensite你的脚本,要么定义一个变量:

A2ENSITE=/usr/sbin/a2ensite
...
${A2ENSITE} new-domain.com