Create subdomains for php is possible?
I try but the page is blank or generates error message IPv4/IPv6 address
Code:
// http://www.linode.com/api/
// Create a domain record
$api_key = "MY_API";
$domain_id = 9485;
$subdomain = $_GET['subdomain'];
$json_url = 'https://api.linode.com/?api_key=' .
$api_key .
'&api_action=batch&api_requestArray=[{' .
'"api_action": "domain.resource.create",' .
'"domainid": ' . $domain_id . ', ' .
'"Name": "' . $subdomain . '", "Type":"A", "TTL_sec":0' .
'}]';
$json_file = file_get_contents($json_url,0,null,null);
$data = json_decode($json_file, true);
print_r($data);
Even if I can, I'll have to run a script to enable the new site created, where do not know if exec () you can run it on apache ...
Look:
A friend gave me a script to adapt but do not know in which directory to let these scripts. He did not specify that.
Code:
#!/usr/bin/php
<?php
require_once 'vhost.php';
if ( exec('whoami') !== 'root' )
die ("\n" . 'Erro: É necessário ter privilégios de administrador para executar esta ferramenta.' . "\n");
$action = readline("\n" . 'Ação (criar/deletar): ');
$vhost = new Vhost;
switch ($action) {
case 'criar':
$vhost->readName()
->readPath()
->create();
break;
case 'deletar':
$vhost->readName()
->delete();
break;
default:
die("\n" . 'Erro: Ação inválida!' . "\n\n");
}
vhost.php
Code:
<?php
class Vhost
{
protected $name;
protected $path;
public function getName()
{
return $this->name;
}
public function readName()
{
$this->name = readline("\n" . 'Digite o nome do VirtualHost: ');
return $this;
}
public function readPath()
{
$this->path = readline("\n" . 'Digite o caminho da pasta web do VirtualHost: ');
if ( ! is_dir($this->path) )
die ("\n" . 'Erro: O caminho informado não é um diretório válido.' . "\n\n");
return $this;
}
public function create()
{
$vhostFile = fopen('/etc/apache2/sites-available/' . $this->name, 'w');
fwrite($vhostFile, $this->getStructure());
$this->enable();
exec('sudo -u username google-chrome http://' . $this->name);
exit("\n" . 'VirtualHost criado!' . "\n\n");
}
private function getStructure()
{
return "
<VirtualHost *:80>
DocumentRoot $this->path
ServerName $this->name
# This should be omitted in the production environment
SetEnv APPLICATION_ENV development
<Directory $this->path>
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
";
}
private function enable()
{
$hostsFile = fopen('/etc/hosts', 'a');
fwrite($hostsFile, "\n" . '127.0.0.1 ' . $this->name);
exec('a2ensite ' . $this->name);
exec('service apache2 reload');
}
public function delete()
{
if ( unlink('/etc/apache2/sites-available/' . $this->name) ) {
$this->disable();
exit("\n" . 'VirtualHost deletado!' . "\n\n");
} else
die("\n" . 'Erro: Este VirtualHost não existe!' . "\n\n");
}
private function disable()
{
exec('a2dissite ' . $this->name);
exec('service apache2 reload');
}
}