Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

New Save and Scale This Summer. Get 30% OFF for the First 2 Months. Promo: SCALEINSUMMER AVAIL NOW

How to Manage Server Services Using Cloudways API

February 27, 2017

4 Min Read
Reading Time: 4 minutes

Cloudways API has been available for some time and the Cloudways team documented a number of use cases for the API. In the previous installments, I covered several important scenarios such as creating and deleting applications, clone GitHub repo and a Slack bot for Cloudways API. has Since Cloudways has revealed the platform API we are building different use cases to cater the user needs by using the API. Check out the complete list of the API use cases  for a more detailed look into the usage of Cloudways API.

manage server's services using Cloudways API

Cloudways API is a very handy way of communicating with the servers and applications via a custom application. To make sure that the developers have a head start with Cloudways API, our clients have built a very useful SDK. In this article, I will demonstrate how you can manage services such as Apache2, Memcached, Varnish and others directly from a custom PHP application.

Install Cloudways SDK Using Composer

Composer is the preferred way of installing a PHP SDK. For this, lunch your preferred terminal go to the project’s folder. Next, run the following Composer command:

composer require cloudwaysapi/phpsdk:1.0.0.x-dev

Alternatively, you can install the SDK by requiring it in the composer.json file. For this, create a JSON file in the project and add the following lines to it:


 "require": {

   "cloudwaysapi/phpsdk": "1.0.0.x-dev*"



Save the file and run the command: composer install. The SDK will be install in the project.
If you are a Git fan, you can also clone it from Bitbucket, using the clone command:

git clone

Now that the SDK has been installed, I will move on to the actual application.

Disclaimer: Cloudways doesn’t officially maintain this SDK, it is built and maintained by our clients.

Create a Form to Show the Servers

Note: You can find the complete code for the application on my GitHub.

First I will get the list of all servers and the services running on them. This will go into a form that will have proper dropdown menus for both servers and services. In addition, the user could select a service and perform specific actions such as start, stop and restart. The drop down menu for these action items will depend on specific service and will be JavaScript powered.

The basic HTML (bootstrap) code for the form is given below:



   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

   <meta name="viewport" content="width=device-width, initial-scale=1">


   <link rel="stylesheet" href="">

   <link rel="stylesheet" href="">

   <script src=""></script>

   <script src=""></script>



   <div class="container-fluid">

       <div class="row">

           <div class="col-md-10">

               <form class="form-horizontal" method="post" action="index.php">


                       <!-- Form Name -->

                       <legend>Manage Services</legend>

                       <!-- Select Basic -->

                       <div class="form-group">

                           <label class="col-md-4 control-label" for="server">Server</label>

                          <div class="col-md-4">

                               <select id="server" name="server" class="form-control">

                                   <option value="">Select Your Server</option>

                                   <?php foreach($servers->servers as $server) { echo "

                 <option value='".$server->id."'>".$server->label."</option>"; } ?>




                       <!-- Select Basic -->

                       <div class="form-group">

                           <label class="col-md-4 control-label" for="application">Services</label>

                           <div class="col-md-4">

                               <select id="app" name="app" class="form-control disable" onChange="changecat(this.value);">




                       <div class="form-group">

                           <label class="col-md-4 control-label" for="application">Action</label>

                           <div class="col-md-4">

                               <select name="action" id="action" class="form-control disable">




                       <!-- Button -->

                       <div class="form-group">

                           <label class="col-md-4 control-label" for=""></label>

                           <div class="col-md-4">

                               <button name="submit" class="btn btn-success">Do it</button>

                               <span class="bg-success"><?php //echo $success  ?> </span>










The view of this basic form will be:

Get Servers and Populate Dropdown Menus

I will now write the PHP code to get the servers from Cloudways. This code will also populate the services in the second drop down menu.  Since the SDK is already installed, I will need  to require the autoload.php file to use the SDK. Next, I will use some namespaces and call the server list. For this to work, just enter the email and the API Secret key of Cloudways, that are available from the Platform.

The code for the autoload.php is:


require "vendor/autoload.php";

use Cloudways\Server\Service\Service;

$service = new Service();

$service->SetEmail("Your email");

$service->SetKey("Your Api Key");

//get all servers

$servers = $service->getServers();

foreach($servers->servers as $server){

   $serverId = $server->id;


//get the services of the selected server

$value = ['server_id' => $serverId];

$ser = $service->getServices($value);

//unset values of post installed services


You can see that I have unset several values in the array because I first need to enable them first at the server. After that, you can remove these values from the unset array.
If a server is selected, the second dropdown menu should be populated with the related services. For this, I will use JQuery (already includes in the head). The function is given below:

< script type = "text/javascript" >

$("#server").change(function() {

 val = $(this).val();

 switch (val) {

  <?php foreach($servers -> servers as $server) { ?>

  case <?php echo "\"".$server -> id.

       "\""; ?>:





   <?php $i = "<option value=''>Select Your Application</option>";

         foreach($ser->services->status as $key => $value){

             $i .= "<option value='".$key."'>".$key."</option>";


           } ?>

   apps = "<?php echo $i;?>";




       } ?>








}); < /script>

Now in the next step, If I select any service, the actions drop down menu should be properly populated. The javascript functions for this is given below:


var actionbyservice = {

   apache2: ["restart"],

   elasticsearch: ["start", "stop"],

   memcached: ["restart"],

   varnish: ["start", "stop"],

  mysql: ["restart"],

  nginx: ["restart"],

  redis: ["start","stop","restart"],

   php-fpm: ["restart"],


   function changeaction(value) {

       if (value.length == 0) document.getElementById("action").innerHTML = "<option></option>";

       else {

           var catOptions = "";

           for (categoryId in actionbyservice[value]) {

               catOptions += "<option>" + actionbyservice[value][categoryId] + "</option>";


           document.getElementById("action").innerHTML = catOptions;




Submitting The Form

At this point, I have all the drop down menus populated with the proper entries. I will now submit the form and get the response from the server. On clicking the Submit button, I will get the values and pass them to manage Services() method. Remember that this method takes the array of the values, so the values must be saved in the in array.


echo $serverid = $_POST['server'];

echo $appservice = $_POST['app'];

echo $actions = $_POST['action'];

$value = ['server_id' => $serverid, 'service' => $appservice, 'state' => $actions];

$doaction = $service->manageServices($value);

echo "<pre>";


echo "</pre>";


Now, when you select a server, the active services and associated actions will be available in the next  drop down menus. Check out the following screenshot:
When the button is clicked, The request will be processed and the server’s response will be collected:

Final Words

In this article, I demonstrated a very important use case of Cloudways API in which I created a custom PHP application to get the server’s information and carry out service related actions. The best thing about this application is that you do not need to login to the server to execute the actions of the application.

If you have any question about the the code or would like to contribute to the discussion, do leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!