This guide shows you how can you create and deploy a PHP web application on IBM Cloud and connect and manage a Cloudant database instance on IBM Cloud. You will also learn how to fetch the required data from Cloudant using search index queries.
- An IBM Cloud Account. (You can create one here, and get 30 days free trial)
- IBM Cloud Command Line Interface
- GIT Command Line Interface
These steps will take you approximately 60 minutes. This does not include creating an IBM Cloud account(if you don’t have one) or downloading the IBM Cloud CLI. It includes 10 minutes of creating Cloudant service and generating credentials. Creating and the initial starting of PHP Service instance takes 10 minutes. Cloning the sample code and doing the modifications can take you around 15-25 minutes. Deploying the code to the PHP Service using IBM Cloud CLI can take 10 minutes.
In this guide you will perform the following steps: Create a Cloudant Service on IBM Cloud and generate credentials. Creating Database and Search Index in Cloudant DB Creating a PHP Service on IBM Cloud. Cloning the sample from GitHub. Making necessary modifications in the sample code before deployment. Deploying local code to the PHP service using IBM Cloud CLI.
-
After giving suitable app name and hostname click Create button to create a PHP Service.
-
Once the service is running you can access it by clicking Visit App URL.
-
Go to Catalog from IBM Cloud homepage.
-
After giving suitable Service Name, in available authentication methods select Use both legacy credentials and IAM and then click Create button below to create the DB instance.
-
Once the service is created you can access the Cloudant Dashboard by clicking LAUNCH CLOUDANT DASHBOARD button.
-
For creating service credentials select Service Credentials.
-
After giving the suitable name to credentials, click Add button. This will generate credentials with the given name which will be used later to communicate with the DB.
-
Go to Manage and Launch Cloudant Dashboard, this will take you to Cloudant dashboard. From here you can completely manage your database instance.
- You will not be able to see any databases there when you open it for the first time.
- For this application we will be creating
-
After giving a suitable name for database click Create button.
-
Click on the database which you have created to open the database
-
Go to Plus Icon near Design Documents and Click New Search Index
-
After giving suitable name to design document and search index, create following function definition in Search index Function:
function (doc) {
index("username", doc.username);
}
- This creates a search index for the database. We will be using this search index to check if the username already exists in the DB or not.
- Accessing Git CLI
- For Windows Users: Open Git CMD
- For Linux and Mac Users: Open Terminal
- Use the following command to clone the sample repository
- $ git clone https://github.com/Ritwikjoshi/ibmcloudantsample
- Open Credentials.php file in the cloned folder.
- Replace Username, Password, Host and Database Name in the file.
- Now, open the manifest.yml file located in the cloned folder and replace with your application name ( Name of the PHP Service you created in the beginning i.e “ibmsample”).
- Running the code locally
- Accessing IBM Cloud CLI
- For Windows Users: Open Windows Command Prompt
- For Linux and Mac Users: Open Terminal
- For Confirming the installation of Ibmcloud CLI use the following command:
$ ibmcloud -v
- Log in to IBM Cloud
$ ibmcloud login
- Authenticate using IBM Cloud Username and Password.
- Switch to the directory having sample code and run the following command there:
$ ibmcloud app push
- This uses the manifest.yml file in the folder to target the application and the organisation.
- If you get an error saying “No CF API endpoint set” use the following command to correct it:
$ ibmcloud target --cf
- After setting the API Endpoint repeat step 4.
- If you don't see the status as running then the push was not successful. In such a case, you need to repeat the Step4.
In this how-to guide you learned how to:
- Creating PHP and Cloudant Services on IBM Cloud.
- Creating Database and Querying Cloudant DB using Search Index.
- Deploying application using IBM Cloud CLI.