Call to undefined function curl_init() even it is enabled in php7

Solution 1:

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 :)

Solution 2:

Assumption

You've installed the version of the module for the PHP version you are using, 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.