Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu Mar 29, 2012 11:02 pm 
Offline
Newbie

Joined: Wed Mar 14, 2012 2:47 pm
Posts: 4
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');
    }
   
}


Top
   
 Post subject:
PostPosted: Fri Mar 30, 2012 3:50 pm 
Offline
Junior Member

Joined: Mon Apr 11, 2011 9:49 pm
Posts: 49
Depends, look at your DNS, and check to see if the subdomain works via DIG, example;

[user@hostname ~]$ dig mail.yahoo.com +short
login.yahoo.com.
login-global.lgg1.b.yahoo.com.
login.lga1.b.yahoo.com.
66.163.169.186

^ Valid ip address. Remember you can setup a * (wildcard) record for A records. :)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group