Skip to content

Servers::create

Danny Li edited this page Feb 4, 2018 · 2 revisions

Parameters

Parameter Type Description
$config Configs\Server Server configuration

Returns

ServerCreation object

Example

<?php
use Exploriment\HetznerCloud;

$creation = HetznerCloud\Servers::create($config);

// returns the generated root password (string or null)
// it will be null if you have specified SSH key(s) in your config
$password = $creation->getRootPassword();

/**
 * returns the Server object
 * @see https://github.com/Exploriment/hcloud-php/wiki/resources:-servers#server-object
 */
$server = $creation->getServer();

echo $server->getPublicNet()->getIpv4();

/**
 * returns the Action object
 * @see https://github.com/Exploriment/hcloud-php/wiki/resources:-actions#action-object
 */
$action = $creation->getAction();

echo $action->getStatus();

Server configuration object

Creating a new server requires the Configs\Server config object, an example below:

<?php
use Exploriment\HetznerCloud;

/**
 * new server configuration
 *
 * You may also set the name, server type and image immediately like so:
 * $config = new HetznerCloud\Configs\Server('my-server', 1, 1);
 */
$config = new HetznerCloud\Configs\Server();

// set the (host)name for the server (required)
$config->setName('my-server');

/**
 * set server type of the server (required)
 *
 * $server_type can be either the server type ID, server type name or the ServerType object
 * @see https://github.com/Exploriment/hcloud-php/wiki/resources:-servertypes#servertype-object
 */
$config->setServerType($server_type);

/**
 * image the server is created from (required)
 *
 * $image can be either the image ID, image name or the Image object
 * @see https://github.com/Exploriment/hcloud-php/wiki/resources:-images#image-object
 */
$config->setImage($image);

/**
 * datacenter to create the server in (optional)
 *
 * $datacenter can be either the datacenter ID, datacenter name or the Datacenter object
 * @see https://github.com/Exploriment/hcloud-php/wiki/resources:-datacenters#datacenter-object
 */
$config->setDatacenter($datacenter);

/**
 * location to create the server in (optional)
 *
 * $location can be either the location ID, location name or the Location object
 * @see https://github.com/Exploriment/hcloud-php/wiki/resources:-locations#location-object
 */
$config->setLocation($location);

// array of SSH key IDs which should be injected into the server (optional) (recommended)
$config->setSshKeys([ 1 ]);

// start server right after creation (optional) (default = true)
$config->setStartAfterCreate(true);

// Cloud-Init user data to use during server creation
$config->setUserData('#cloud-config');

/**
 * Your configuration for the server is now ready.
 */
Clone this wiki locally