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

Asked

Viewed 1,320 times

4

A while ago I asked a question - already solved - regarding 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.

That is, the client would buy the system on the site, and the PHP script would create (making use of exec, that is, SSH commands, if there is no better mode) a new account for this client, 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.

1 answer

5


Create cPanel account via terminal

cPanel comes with a script called createacct (English) 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 it from a script is that he asks for confirmation of some things when he is executed.

This can be bypassed as we read in this topic (English) from the cPanel forum which provides us with a link to:

How to make the /scripts/createacct non-interactive (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 it’s your idea, you need to install the following PECL extension:

PHP SSH2 Installing/Configuring (English)

Then just make a connection and run the script which contains the above command.

Consult the manual PHP Secure Shell2 (English) to see how to do each of the steps. I was going to put it here, but it’s long and if you don’t know it, you’ll always need to read the manual.


PHP and SSH (without installing extension)

If you need to connect to a server via SSH but cannot 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');
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.