【发布时间】:2017-05-20 03:24:04
【问题描述】:
我需要在Ubuntu 16.04上设置多个虚拟主机,可以手动创建。
但我想动态地使用 php 执行此操作。为此,我尝试使用 php 的 fopen 函数在 /tmp 或 /www 目录中创建文件。所以我可以创建文件但无法将此文件移动到 /etc /apache2/sites-available 目录使用 php shell_exec() 函数。
要移动临时创建的文件,我使用了shell_exec(mv temp_file path_to_move);
但是命令不是通过 php 代码运行的。然后我尝试直接在 /etc/apache2/sites-available 中创建文件,但它显示错误 Cannot open file
这是我用过的代码
<?php
$myfile = fopen("example.com.conf", "w");
$template ='<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>';
fwrite($myfile, $template );
fclose($myfile);
$cmd= 'mv'.$myfile.' /etc/apache2/sites-available';
shell_exec($cmd);
?>
它创建文件但移动命令不起作用
【问题讨论】:
标签: php virtualhost shell-exec