两次创建一个Apache Virtualhost子域

问题描述

我有2个用于创建和删除虚拟主机及其子域的函数

创建功能

function create_sub_domain($username) {
$server   = SERVER;
$user = USER;
$password = PASSWORD;

$domain = DOMAIN;

$connection = ssh2_connect($server,22);
if (ssh2_auth_password($connection,$user,$password)) {
    $issues = 0;
} else {
    $issues = 1;
}

$command  = "chmod -R 755 /var/www/html/";
$stream = ssh2_exec($connection,$command);
stream_set_blocking($stream,false);

$text = "<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName $username.$domain
        DocumentRoot /var/www/html/user/
    </VirtualHost>";
$command  = "echo '$text' > /etc/apache2/sites-available/$username.conf";
$stream = ssh2_exec($connection,false);

$command  = "a2ensite $username.conf";
$stream = ssh2_exec($connection,false);

$command  = "apachectl -k graceful";
$stream = ssh2_exec($connection,false);

$command  = "printf '2' | certbot --apache -d $username.$domain";
$stream = ssh2_exec($connection,false);

ssh2_disconnect($connection);

if ($issues == 0) {
    return true;
} else {
    return false;
}
}

删除功能

function delete_sub_domain($username) {
$server   = SERVER;
$user = USER;
$password = PASSWORD;


$command  = "a2dissite $username.conf";
$connection = ssh2_connect($server,$password)) {
    $issues = 0;
} else {
    $issues = 1;
}
$stream = ssh2_exec($connection,false);

$command  = "a2dissite $username-le-ssl.conf";
$stream = ssh2_exec($connection,false);

$command  = "rm /etc/apache2/sites-available/$username.conf";
$stream = ssh2_exec($connection,false);

$command  = "rm /etc/apache2/sites-available/$username-le-ssl.conf";
$stream = ssh2_exec($connection,false);

ssh2_disconnect($connection);

$connection = ssh2_connect($server,$password)) {
    $issues = 0;
} else {
    $issues = 1;
}
$command  = "apachectl -k graceful";
$stream = ssh2_exec($connection,false);

ssh2_disconnect($connection);


if ($issues == 0) {
    return true;
} else {
    return false;
}
}

当我创建( create_sub_domain(“ test”))时,将设置端口80和443,并且子域正常工作。

port 443 namevhost test.example.com (/etc/apache2/sites-enabled/test-le-ssl.conf:2)
port 80 namevhost test.example.com (/etc/apache2/sites-enabled/test.conf:1)

test.example.com有效

当我删除 delete_sub_domain(“ test”)))时,所有内容都将删除

我的问题:

但是,当我尝试重新创建相同的子域( create_sub_domain(“ test”))时,仅设置了端口80,并且子域不起作用。

port 80 namevhost test.example.com (/etc/apache2/sites-enabled/test.conf:1)

test.example.com无法正常工作

如何多次删除并重新创建相同的子域?谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)