Install cURL Libraries for PHP on AWS EC2 From Start to Finish

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

How to Install cURL for PHP on AWS Amazon Linux EC2

cURL comes pre-installed on an AWS EC2 instance. You can follow these 2 steps if you find it missing. You will need to install the cURL module. Once installed, confirm that the cURL extension is enabled in your php.ini file.

Steps to Install cURL on an AWS EC2 with Amazon Linux EC2

  1. Connect into the AWS EC2 instance using SSH or Session Manager
  2. Update Linux packages with command: sudo yum update
  3. Install cURL package with command: sudo yum install php-curl
  4. Verify if cURL is installed with command: curl -v
  5. Confirm the PHP curl extension is enabled in your PHP.ini file
  6. Find the PHP.ini file on the server with command: php –ini
  7. cd to the directory of the PHP.ini file like: cd /etc
  8. Open the file using vim or your preferred editor: vi php.ini
  9. Find the following in the file: extension=curl.so
  10. Remove any semicolon before the extension (the semicolon is used as a comment)
  11. Save the PHP.ini
  12. Restart your webserver
  13. Save your updates as a custom AMI so you don’t have to do it again.
## Step 1: update YUM 
sudo yum update

## Step 2: Install Curll
sudo yum install php-curl

## Step 3: Verify installation
curl -V

Install PHP cURL on AWS EC2 from Start to Finish with Detail

We’ve shown the quick steps to install PHP cURL on an AWS EC2, but you may be like me and need a more detailed walk-through with a complicated task like this one. We will be diving deep into the details of the process to installing cURL on AWS EC2 Linux and enabling it in PHP.

Let’s Dive in.

Connect to AWS EC2 Instance

1. Connect to the AWS EC2 instance.

Install PHP cURL on AWS EC2
Connect to EC2 Instance

2. It will prompt you to the EC2 Linux shell.

Install PHP cURL on AWS EC2
Linux Shell

Update EC2 Linux Packages

It is always recommended to update your Linux packages as follows.

sudo yum update

Install cURL on AWS EC2

You can install cURL on AWS EC2 using the following command.

sudo yum install php-curl

Check if cURL is Installed or not in EC2

You can check if cURL is installed on your Amazon Linux EC2 instance by running the curl -V command

curl -V

Ok, we are done doing the installation part of cURL for AWS EC2 and Amazon Linux. Let’s switch gears to the second part to make sure that your PHP cURL extension is turned on.

Configure cURL for PHP on Amazon Linux EC2

Configuring PHP to use cURL is straightforward. It is a 3-step process that includes finding the right PHP.ini fule and then enabling it and restarting your webserver.

Steps to turn cURL on in an AWS EC2 with Amazon Linux

  1. SSH into your AWS EC2 instance
  2. Find the PHP.ini file on the server with command: php –ini
  3. cd to the directory of the PHP.ini file like: cd /etc
  4. Open the file using vim or your preferred editor: vi php.ini
  5. Find the following in the file: extension=curl.so
  6. Remove any semicolon before the extension (the semicolon is used as a comment)
  7. Save the PHP.ini
  8. Restart your webserver
  9. Save your updates as a custom AMI so you don’t have to do it again.

How to Find the PHP.ini file in an EC2 Instance

To find the PHP.ini file in AWS EC2, we use the following command.

php --ini

After running this command, we get the following output shown below.

PHP.ini & curl.ini files location.

As we can see in the above image shows the location of the PHP.ini file. The curl.ini comes is an additional ini file that we can find also.

Now, we change the directory using the following command.

cd /etc

To list all the files that existed in the etc directory, we use the ls command.

PHP.ini file location.

After listing all the files, we can see the exact location of the PHP.ini file.

Enable curl in PHP on an Amazon Linux EC2 Instance

By default, cURL in PHP is already enabled on the AWS EC2 Linux instance. But if it is disabled, we can enable it by updating the following extension in the php.ini file.

extension=curl.so [Enabled]
;extension=curl.so [Disable]

We already know the directory location of the curl.ini file. So, we use the following command to move to the directory where curl.ini exists.

cd /etc/php.d

After running the above command, we are now in the directory where curl.ini file exists. We will use the following command to edit the curl.ini file.

vim curl.ini
curl.ini Extension

In the above image, we can see curl.ini.so, and before the extension, there is no semi-colon. It means that cURL is already enabled.

When cURL is disabled, we will get a result like this ;extension=curl.so. The occurrence of a semi-colon means that cURL is disabled. We can enable it by just removing the semi-colon.

Restart Apache server on AWS EC2

sudo systemctl restart httpd

What is cURL in PHP

CURL stands for client URL in PHP. In PHP, the cURL library is the most powerful extension of PHP. It allows users to create HTTP requests in PHP and also send and receive data through the URL. It makes communication easy between different websites and domains.

The PHP cURL library supports multiple protocols, including HTTP, HTTPS, FTP, MQTT, etc.

cURL Request

Read more about using cURL in PHP

Now that we have cURL installed and ready check out the following content to learn more about how to use cURL

Send a Get Request with Curl

Before we install PHP cURL in Amazon Linux, we need to understand why we need to install the PHP cURL library.

Related Questions Regarding PHP & cURL

Does PHP come with cURL?

PHP generally ships with a cURL extension, but you will need to confirm that your server comes with the cURL package installed. Once that is installed, you will want to ensure the INI file’s PHP extension is enabled.

Why do we need to install cURL?

If we are running Linux on an Amazon EC2 instance and we are running a PHP website on it. We encounter the following error when we include a third-party library such as Facebook PHP SDK.

Error: facebook.php needs a PHP Curl plugin.

The above error shows that our PHP engine needs to have the cURL plugin installed because the Facebook SDK uses cURL. That’s why we need to install the PHP cURL library on our Linux system.

Does AWS Support PHP?

Yes, it does. There are a few ways you can deploy your PHP app into AWS. You can use Elastic Beanstalk, EC2, or run it in a docker container with Kubernetes or any other container service.

Learn how to deploy a PHP application to Elastic Beanstalk.

Click here to read our guide on AWS EC2 with PHP

Perfect! That’s it for this article – time to wrap it up.

Wrap Up

This article demonstrates how to install PHP cURL on AWS EC2. The article includes steps through which we can install the PHP cURL library. Also, it goes beyond the main topic and dives into basic ideas, like how cURL works and why we need to install PHP cURL.

Hope you’ve liked this article. Stay tuned for more at FuelingPHP.

Continue Learning About cURL

This article is part of a more extensive series on using cURL in PHP. Feel free to browse through the articles to dig deeper into the topic.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.