How to Create a New Account (Domain) via SSH?

4

Some time ago I asked a question - already resolved - about how to do an automatic update remotely using PHP ( See here )

Now I would like to create via SSH a new account on the server in order to replicate the system.

In other words, the client would buy the system on the site, and the PHP script would create a new account for this client (using exec, that is, SSH commands, if there is not a better way), install the system and leave the store ready for use.

My server uses the WHM / CPanel system. That is, when the new account is created, CPanel must be installed in this new account correctly.

How to do this?

Only the SSH command (or command sets) for creating a new account / domain would be enough.

    
asked by anonymous 04.07.2014 / 20:58

1 answer

5

Create cPanel account via terminal

cPanel comes with a script called createacct / sup> to create accounts from the command line. You can use it to resolve the account creation issue:

/scripts/createacct example.com utilizador password

The problem with this command when we want to use the same from a script is that it asks for confirmation of some things when it is executed.

This can be bypassed as we read in this topic (English) of the cPanel forum that tells us a link to:

How to make non-interactive /scripts/createacct (English)

PKGRESTORE=1 /scripts/createacct example.com utilizador password

└─────┬────┘ └─────────────────────────┬────────────────────────┘
executar em                     o comando para
  modo não                   criação de uma conta
interactivo                  a partir do terminal

PHP and SSH

To make use of SSH through PHP, assuming to be your idea, you need to install the following PECL extension:

PHP SSH2 Installing / Configuring (English)

Then just establish a connection and execute the script that contains the above command.

See the PHP Secure Shell2 manual (English) for you will see how to do each of the steps. I would put it here, but it's extensive and if you do not know, you'll always need to read the manual.

PHP and SSH (without installing the extension)

If you need to connect to a server via SSH but you can not install the ssh2 extension, the following pure PHP implementation can solve your question:

PHP Secure Communications Library - Net_SSH (English)

Example:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.example.com');
if (!$ssh->login('utilizador', 'password')) {
    exit('Login Falhou');
}

echo $ssh->exec('meuScript');
echo $ssh->exec('ls -la');
?>
    
05.07.2014 / 02:12