How to Manage Server Services Using Cloudways API

by Shahroze Nawaz  February 27, 2017

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:

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:

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:

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:

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:

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:

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:

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.

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.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...