47

I've just installed php7 to my Ubuntu. At first, there was no problem, my web site was working. But suddenly, it started to return Call to undefined function curl_init() error. Now, my pages contain curl codes do not work.

In phpinfo(), it looks Curl is enabled. There were similar questions but none of them handled it in php7. I thought it should be something different than others.

Edit: When I try

php -i | grep curl       

in terminal, it returns

/etc/php/7.0/cli/conf.d/20-curl.ini,
curl
Sabri Karagönen
  • 2,212
  • 1
  • 14
  • 28

10 Answers10

104

I've had similar problem with curl after upgrade to XX (16.04). After reinstalling curl with:

sudo apt-get install php-curl

And server restart

sudo service apache2 restart

everything went back to normal :)

Deus777
  • 1,816
  • 1
  • 20
  • 18
  • The last upgrade from Ubuntu server 14.04 to 16.04 did not go as smooth as it should have. MySQL was left in a limbo because of a deprecated variable in the config file and now I found out that curl was magically removed and jobs were failing without me knowing for a couple of days. – Dimitar Darazhanski Dec 06 '16 at 00:21
  • 3
    Just FYI in my case I am running PHP 5.6 instead of the default 7, so `apt-get install php-curl` gave me the 7 version .. I had to `apt-get install php5.6-curl` – Neek Jan 25 '17 at 08:43
  • 20
    For some it may be solved by this: `sudo apt-get install php7.0-curl` – sj59 Jan 29 '17 at 11:09
  • unable to locate package php-curl – Ramesh Pareek Jul 16 '17 at 05:07
  • my php version is 5.5.9 – Ramesh Pareek Jul 16 '17 at 05:08
  • FYI on PHP 5.5 (yeah i know, legacy systems) do a `sudo apt-get install php5-curl`. You're welcome. – Sliq Sep 19 '17 at 00:33
  • 1
    for my case i had to install `sudo apt-get install php7.2-curl` and then restart my apache server – amdev May 27 '19 at 20:46
21

Assumption

You've installed the version of the module for the PHP version you need to use, and yet the problem is not going away.

What is going on here?

There could be multiple versions of PHP installed on your system and Apache is not using the version you are expecting it to use.

How do you know which version of PHP Apache is using?

To know this, the key idea is to learn the ROOT directory of your Apache's configuration files. In the command line, you can type:

apache2ctl -V  //sample output below

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Jul 15 2016 15:34:04
Server's Module Magic Number: 20120211:27
Server loaded:  APR 1.5.1-dev, APR-UTIL 1.5.3
Compiled using: APR 1.5.1-dev, APR-UTIL 1.5.3
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

In my case, my Apache's ROOT configuration directory is shown in the

HTTPD_ROOT="/etc/apache2"

Now that I know the location of the configs that Apache is using, I can now accurately determine the version of PHP it is using by examining the "mods-enabled" directory located inside the "/etc/apache2" directory.

In my case, when do an ls while inside the "mods-enabled", it showed the ff output:

access_compat.load  authz_user.load  filter.load       php5.load
...
authz_host.load     env.load         php5.conf

At this point, I now know for certain that Apache is using the 'php5' version of PHP installed on my system, whatever that may be.

Then I tried to reproduce the error above using this version of PHP (i.e., 'php5') by running the command below:

$ php5 -r "curl_init();"
PHP Fatal error:  Call to undefined function curl_init() in Command line code on line 1

Voila!

The version of PHP that I expected my Apache was using is "php5.6" and running the same command above with this version did not produce the said error.

Solution

To solve this problem, you either install the version of the module that corresponds to the PHP version that Apache is using (in my example php5.0-curl) or you may change the version of PHP that is being used in Apache to the version you want.

How do I tell Apache which version of PHP to use?

You can accomplish this using the a2enmod/a2dismod cli commands of Apache2.

Firstly, I disable the PHP module that is currently active on my server (i.e., "php5"):

a2dismod php5

Then I enabled the php module for the version of PHP that I want my Apache to use:

a2enmod php5.6

Then I restart Apache

service apache2 restart

After I refreshed the offending page on my website, the error is now gone.

ultrajohn
  • 2,527
  • 4
  • 31
  • 56
5

I did all of the above but did not solve the problem.

Env: Ubuntu, php7.1, Laravel 5.6

Solution

sudo add-apt-repository ppa:ondrej/php
sudo apt-get install php7.1-curl
Sheraz Ahmed
  • 564
  • 9
  • 29
3

For me, the resolution was to update apt-get with the following command, and then install php7.0-curl.

sudo add-apt-repository ppa:ondrej/php
Kevin Hill
  • 311
  • 1
  • 12
2

Your Filepath is probably incorrect

Check the Apache error log in

/var/log/apache2/error.log

if the called path or filename does match your real path in e.g.

/usr/lib/php/20151012/php_curl.so

In my case it's been the same path, but "the php_" was missing

/usr/lib/php/20151012/curl.so

So I changed the path / filename accordingly in

/etc/php/7.0/cli/conf.d/20-curl.ini 

from

extension=php_curl.so

into

extension=curl.so
leopold
  • 1,971
  • 1
  • 19
  • 22
  • I think the best way to get php config is to run web script with phpinfo() I got all informations about curl as in this situation curl was configured foranother version of php. hth – TexWiller Feb 08 '18 at 19:14
2

I know its very late answer but I think its useful because I faced same issue and tried all above given solution but not worked. Then I hit following command and now working perfectly :

apt-get install php7.0-curl
Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22
1

Your pages probably are not generated with the CLI SAPI. Check what phpinfo() returns when run from your webserver (its probably trying to read the wrong ini file).

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • Loaded configuration file is: /etc/php/7.0/apache2/php.ini Loaded config directory is: /etc/php/7.0/apache2/conf.d And all of these files are OK. There is a 20-curl.ini file in conf.d folder. So, what am I doing wrong? – Sabri Karagönen Jan 17 '16 at 22:26
  • Are you saying via the web sapi it did not load /etc/php/7.0/apache2/conf.d/20-curl.ini ? Suggest you review those files and the apache error_log. – symcbean Jan 17 '16 at 22:32
  • In my case cli was php 7.1 and apache was using 7.0... I resolved it with making alias in bash profile... – Aleksandar Pavić Jun 07 '17 at 18:16
0

ubuntu 18.04 php 7.4

if you have PHP old version and the new version as well as. you need to disable old curl mode so that. follow the steps.

sudo a2dismod php7.1 (your old version) sudo a2endmod php7.4 (your new version) sudo service apache2 restart sudo systemctl restart apache2

it works for me.

Mustak_Talukder
  • 125
  • 1
  • 8
0

Scratched my head over this for a while, curl module was loaded but still got the error.
This happen after I upgraded from Debian Stretch to Buster.

The reason was that php7.3-curl was installed and used, but apache run php7.0 even though 7.3 was installed. So I removed php7.0 and reinstalled libapache2 to get it working

apt-get remove php-7.0
apt-get remove libapache2-mod-php7.3
apt-get install libapache2-mod-php7.3
DomeDan
  • 11
  • 1
-1
  • step 1 : Download Php 7,
  • step 2 : copy libeay32.dll and ssleay32.dll and past them in C:\Windows\System32.
  • step 3 : Replace the php_openssl.dll and php_curl.dll in C:\php\ext with the latest dll. restart apache

This fixed my issues issue, hope someone benefits too

ikos23
  • 4,879
  • 10
  • 41
  • 60