Getting started with PHP on IBM Cloud
This guide will take you through the steps to get started with a simple PHP application in IBM Cloud and help you:
- Set up a development environment
- Download sample code
- Run the application locally
- Run the application on IBM Cloud Cloud Foundry
- Add a IBM Cloud Database service
- Connect to the database from your local application
You'll need the following:
1. Clone the sample app
Now you're ready to start working with the app. Clone the repo and change the directory to where the sample app is located.
git clone https://github.com/IBM-Cloud/get-started-php cd get-started-php
2. Run the app locally
php composer.phar install
Run the app
php -S localhost:8000
View your app at: http://localhost:8000
3. Prepare the app for deployment
To deploy to IBM Cloud, it can be helpful to set up a manifest.yml file. One is provided for you with the sample. Take a moment to look at it.
The manifest.yml includes basic information about your app, such as the name, how much memory to allocate for each instance and the route. In this manifest.yml random-route: true generates a random route for your app to prevent your route from colliding with others. You can replace random-route: true with host: myChosenHostName, supplying a host name of your choice. Learn more...
applications: - name: GetStartedPHP random-route: true memory: 128M
4. Deploy the app
You can use the Cloud Foundry CLI to deploy apps.
Choose your API endpoint
cf api <API-endpoint>
Replace the API-endpoint in the command with an API endpoint from the following list.
Login to your IBM Cloud account
From within the get-started-php directory push your app to IBM Cloud
This can take a minute. If there is an error in the deployment process you can use the command
cf logs <Your-App-Name> --recent to troubleshoot.
When deployment completes you should a message indicating that your app is running. View your app at the URL listed in the output of the push command. You can also issue the
command to view your apps status and see the URL.
5. Add a database
Next, we'll add a NoSQL database to this application and set up the application so that it can run locally and on IBM Cloud.
- Log in to IBM Cloud in your Browser. Browse to the
Dashboard. Select your application by clicking on its name in the
- Click on
- In the
Data & Analyticssection, select
Cloudant NoSQL DBand
Restagewhen prompted. IBM Cloud will restart your application and provide the database credentials to your application using the
VCAP_SERVICESenvironment variable. This environment variable is only available to the application when it is running on IBM Cloud.
Environment variables enable you to separate deployment settings from your source code. For example, instead of hardcoding a database password, you can store this in an environment variable which you reference in your source code. Learn more...
6. Use the database
We're now going to update your local code to point to this database. We'll create a file that will store the credentials for the services the application will use. This file will get used ONLY when the application is running locally. When running in IBM Cloud, the credentials will be read from the VCAP_SERVICES environment variable.
- Create a file called
get-started-phpdirectory with the following content:
CLOUDANT_HOST= CLOUDANT_USERNAME= CLOUDANT_PASSWORD=
Back in the IBM Cloud UI, select your App -> Connections -> Cloudant -> View Credentials
Copy and paste just the
urlfrom the credentials to the
CLOUDANT_URLfield of the
.envfile and save the changes. The result will be something like:
CLOUDANT_HOST=abc...yz.cloudant.com CLOUDANT_USERNAME=abc...yz CLOUDANT_PASSWORD=445d...d1a
- Run your application locally.
php -S localhost:8000
View your app at: http://localhost:8080. Any names you enter into the app will now get added to the database.
Your local app and the IBM Cloud app are sharing the database. View your IBM Cloud app at the URL listed in the output of the push command from above. Names you add from either app should appear in both when you refresh the browsers.