Facing an error with user_data. After droplet creation nginx is not installed. I wanted the following coding to run on my server after droplet creation with the use user data
#!/bin/bash
apt-get -y update
apt-get -y install nginx
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: $HOSTNAME, IP Address: $PUBLIC_IPV4 > /usr/share/nginx/html/index.html
so i used that code in
$userData = "#!/bin/bash
apt-get -y update
apt-get -y install nginx
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: $HOSTNAME, IP Address: $PUBLIC_IPV4 > /usr/share/nginx/html/index.html";
For passing the data to the api i use this code below
$data = array("name" => "NewDroplet", "region" => "ams3", "size" => "512mb", "image" => "ubuntu-14-04-x64", "user_data" => $userData);
I am new to coding with api :-) I dont know what i am doing wrong.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
It’s hard to know exactly where your error is without seeing the rest of the code or the error logs. In general, you seem to going in the right direction. Here’s a working example:
<?php
$user_data = <<<EOD
#!/bin/bash
apt-get -y update
apt-get -y install nginx
export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname)
export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
echo Droplet: \$HOSTNAME, IP Address: \$PUBLIC_IPV4 > /usr/share/nginx/html/index.html
EOD;
$data = array(
'name' => 'nginx-droplet',
'region' => 'nyc3',
'size' => '512mb',
'image' => 'ubuntu-14-04-x64',
'user_data' => $user_data
);
$data_string = json_encode($data);
$ch = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer YOUR_TOKEN_API_HERE',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);
?>
Note that the $HOSTNAME is escaped like \$HOSTNAME in order to prevent PHP from attempting to treat it as a variable.
For more information on the DigitalOcean metadata service see: An Introduction to Droplet Metadata
You might also want to check out the community contributed PHP bindings for DgitialOcean’s APIv2.
Thanks . I got an error with this coding though as i dint get the hostname and ip to show on the html file this was the coding used in v 1 api now i am using v2 export HOSTNAME=$(curl -s http://169.254.169.254/metadata/v1/hostname) export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address) echo Droplet: $HOSTNAME, IP Address: $PUBLIC_IPV4 > /usr/share/nginx/html/index.html
as i am using v2 api now ,How can i get the hostname with the new API . The new api is not easy to understand for me as i am beginner in coding. Any help would be appreciated Thanks.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.