DEV Community

Payal Gupta for AWS Community Builders

Posted on • Updated on

Connect to the Capella Cluster from Amazon EC2 running Red Hat Enterprise Linux using Couchbase PHP SDK

This post will provide you step by step guidelines on how to setup connection with Capella Cluster from Amazon EC2 running Red Hat Enterprise Linux [RHEL 8]

Steps are as follows:

Spin up an EC2 instance running Red Hat Enterprise Linux (ami-0ba62214afa52bec7)

SSH into the EC2 instance. The default user name for a RHEL AMI is ec2-user or root so make sure you are using the correct user name.
$ ssh -i /path/my-key-pair.pem ec2-user@my-instance-public-dns-name

Use below command to install nano editor
$ sudo yum install nano

Install Couchbase C SDK (libcouchbase) which is a pre-requisite to use Couchbase PHP SDK. To do so,
Create a couchbase.repo file in your /etc/yum.repos.d directory.

$ cd /etc/yum.repos.d
$ sudo nano couchbase.repo

paste the below content in couchbase.repo file

[couchbase]
enabled = 1
name = libcouchbase package for centos8 x86_64
baseurl = https://packages.couchbase.com/clients/c/repos/rpm/el8/x86_64
gpgcheck = 1
gpgkey = https://packages.couchbase.com/clients/c/repos/rpm/couchbase.key

Now, the repository has been configured, refresh the cache by using below command
$ sudo yum check-update
$ sudo yum search libcouchbase

Install libcouchbase3, and any other packages that you need for development:
sudo yum install libcouchbase3 libcouchbase-devel libcouchbase3-tools

Enable EPEL and Remi Repository

To install EPEL(Extra Packages for Enterprise Linux), run the below command:

$ sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

To install Remi (a third-party repository that provides a wide range of PHP versions for RedHat Enterprise Linux), run below command:

$ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Install PHP 7.4 on RHEL
First, list the available php module by running this command
$ sudo dnf module list php

You should see remi-7.4 in the list. In order to enable it, run below command:
$ sudo dnf module enable php:remi-7.4 -y

Install the php-devel and php-bear packages
The php-devel package contains the files needed for building PHP extensions and php-pear package contains the basic PEAR components for reusable PHP components.

Run the below commands to install both packages:
$ sudo dnf install php
$ sudo yum install php-devel
$ sudo yum install php-pear

Install the Couchbase PHP SDK using PHP distribution’s pecl command:
$ pecl install couchbase
$ sudo pecl install https://packages.couchbase.com/clients/php/couchbase-3.2.2.tgz

Load the Couchbase SDK as an extension
Use below command to locate the php.ini file
$ php --ini

Insert a line in the php.ini file specifying the extension to be loaded; this should be in the [PHP] section.
extension=couchbase.so

Also, insert the json extension since The Couchbase SDK depends on the JSON module, which must be loaded before the SDK. To do so, you can add the below extension in php.ini file:
extension=json.so

Now, add your ec2 instance public IP address to the allow list of Capella Cluster and run the below sample script to connect with Capella Cluster

<?php
 $connectionString = "couchbases://<connect_string>?ssl=no_verify";
 $options = new \Couchbase\ClusterOptions();
 $options->credentials("<user>", "<password>");
 //$options->timeout(3000 /* milliseconds */);
 $cluster = new \Couchbase\Cluster($connectionString, $options);

 $opts = new \Couchbase\GetOptions();
 $opts->timeout(8000 /* milliseconds */);

 // get a bucket reference
 $bucket = $cluster->bucket("travel-sample");

 // get a default collection reference
 //$collection = $bucket->defaultCollection();

 // or for named collection
 $scope = $bucket->scope("_default");
 $collection = $scope->collection("_default");

 // upsert document
 $upsertResult = $collection->upsert("my-document", ["name" => "mike"]);

 // get document
 $getResult = $collection->get("my-document", $opts);

 echo "Results:\n";
 var_dump($getResult);
?>
Enter fullscreen mode Exit fullscreen mode

where
<connect_string> : Your cluster WAN endpoint
<user>, <password> : your database username/password you created to give access to the cluster buckets
"travel-sample" : It is the sample bucket available in the Capella cluster to use. You could create your own bucket as well. Make sure that your database user has access to that bucket.

Hope the provided steps are helpful. Thank you.

Discussion (0)