PHP Extension not working in LINUX - php

I have created one simple PHP Extension using the following link:
Sample Hello World PHP extension on Ubuntu
I have created a PHP Extension successfully as hello.so.
I have used XAMPP package, in this I have moved this hello.so to /opt/lampp/modules/
and I have included the line:
extension=hello.so in /opt/lampp/etc/**php.ini**
Eventhough, If I called the function from command prompt like:
$ php -r ‘hello_world();’
even I am getting the error as Function undefined.

First test your php.ini file whether the extension include successfully or not.For checking call phpinfo() function.
If it is there then
php -r 'echo hello_world();'
run this on command prompt.I think your missing echo while firing command.You have use echo because it return something.
Note: restart your apache service after extension inclusion.
sudo service apache2 restart

To build the PHP extension, after creating a C file in Linux`, you have to run the following commands:
$ phpize
$ ./configure --enable-hello
$ make
And do not forget to run:
$ sudo make install
If you run this command, by default your newly created extension module (.so) will be moved to the default dynamic extension path (the path will be displayed after running that command)
Then mention this dynamic extension path in the PHP.INI file in the dynamic_extension directory section and include your .so file in the Dynamic Extension section.
Then restart the Apache server and definitely this will work fine.

Related

How to execute PHP CLI script with extension on synology

I'm trying to execute a php script via command line on my synology NAS. Via web browser it's working fine. However, via CLI I'm getting an error although I loaded the extensions in /etc/php/php.ini.
Fatal error: Uncaught PDOException: could not find driver in /volume1/web/blabla.php:16
Any ideas?
Synology uses two different php intallations. One for the internal issues (like administrator panel) and the other from the package you've installed for the web server.
you can observe the differences running from command line:
php --ini
php56 --ini
php7 --ini
(depending of the php package installed)
use php56 or php7 to run your script
Update
Please use the following for more recent versions of PHP (assuming both are installed)
php72 --ini
php74 --ini
I was in the same boat–trying to get PHP 7.0 to work on the command-line of my Synology DiskStation DS218+.
I typed php70 --ini and I found out that PHP was using an INI file located at /usr/local/etc/php70/php.ini.
I went to the directory
cd /usr/local/etc/php70/
Made a backup of the .ini file just incase.
sudo cp php.ini php.ini.bak
Opened vi.
sudo vi php.ini
Hit i to enter Insert mode. I changed from this:
extension_dir = "/usr/local/lib/php70/modules"
to this:
extension_dir = "/volume1/#appstore/PHP7.0/usr/local/lib/php70/modules"
I also added this line:
extension = pdo_mysql.so
I then hit Esc, followed by : then wq and . This leaves insert mode, writes the .ini to disk, and quits.
That's it! After that, I was able to run command-line scripts by invoking php70 followed by the script name.
Just make sure to use the correct PHP version and PHP.ini. You can find the location of your php.ini by typing "php --ini". You can also do that on the executables php56/php70. They all have separate php.ini files. Additionaly make sure to load the correct extension directory in the applicable php.ini file.
for me (DSM 6.1.6-15266 Update 1) the following helped:
Add the following lines to /usr/local/etc/php70/php.ini
extension = pdo_mysql.so
extension = openssl.so
I struggled very long to get the correct values, finally I found them by comparing to the config files of the php56 instance.
Did you try: php56 path/to/script.php or php70 path/to/script.php ?
If I'm not wrong, /etc/php/php.ini is for DSM engine...
For symfony, I use php56 bin/console command. php or php70 doesn't work for me (DSM 6.1.3 on DS415+)
Hope it helps !

ubuntu litespeed php7.0 got a weird php.ini path

Ubuntu 16.04.2
LSPHP7.0
wget -O - http://rpms.litespeedtech.com/debian/enable_lst_debain_repo.sh | bash
apt install -y lsphp70
Then I added /usr/local/lsws/lsphp70/bin to $PATH and made a symlink for /usr/local/lsws/lsphp70/bin/php7.0 to /usr/local/lsws/lsphp70/bin/php. This way I can run php in terminal as cli version.
apt install composer
composer require geoip2/geoip2:~2.0
I got an error said the requested PHP extension curl is missing from your system.
And also it said
To enable extensions, verify that they are enabled in those .ini files:
- /etc/php/7.0/cli/php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
But when I run php -i | grep php.ini it shows the ini is not /etc/php/7.0/cli/php.ini but /usr/local/lsws/lsphp70/etc/php/7.0/litespeed/php.ini
Further more, I run php --ini, it shows:
Configuration File (php.ini) Path: /usr/local/lsws/lsphp70//etc/php/7.0/litespeed/
Loaded Configuration File: /usr/local/lsws/lsphp70/etc/php/7.0/litespeed/php.ini
Scan for additional .ini files in: /usr/local/lsws/lsphp70//etc/php/7.0/mods-available/
Additional .ini files parsed: /usr/local/lsws/lsphp70//etc/php/7.0/mods-available/curl.ini,
/usr/local/lsws/lsphp70//etc/php/7.0/mods-available/imap.ini,
/usr/local/lsws/lsphp70//etc/php/7.0/mods-available/json.ini,
/usr/local/lsws/lsphp70//etc/php/7.0/mods-available/mysqli.ini,
/usr/local/lsws/lsphp70//etc/php/7.0/mods-available/opcache.ini,
/usr/local/lsws/lsphp70//etc/php/7.0/mods-available/pdo_mysql.ini
How can php cli get /etc/php/7.0/cli/php.ini as its php config file? Anyone gives a clue? thanks.
Ubuntu normally installs system default php(currently php7.0) to /usr/bin/php
Composer will normally call the PHP defined in $PATH, usually /usr/bin/php.
The Error "the requested PHP extension curl is missing from your system" means Ubuntu system default php7.0 curl is missing. To resolve:
apt-get install php7.0-curl
So far, it should resolve your problem already.
LiteSpeed Web Server normally use LiteSpeed API to communicate with PHP engine, which is faster than other APIs. In LiteSpeed Ubuntu/Debian repo, it provides different versions of lsphp, which will be at different location other than system default location, normally at /usr/local/lsws/lsphpxx/bin/.
Composer will normally use system default php location as explain above. If you want to overwrite PHP path to use lsphp70 php binary, you will need to add this path to the beginning of $PATH.
export PATH="/usr/local/lsws/lsphp70/bin/:$PATH"
This way, when you run:
which php
System should use the php found first in the PATH, which is /usr/local/lsws/lsphp70/bin/php
In this case, when you see similar php-curl missing error message, you should run:
apt-get install lsphp70-curl
to fix the problem.
To make the new PATH permanently, you can edit ~/.profile or .bashrc, or any other similar way, which is beyond the discussion of this topic.

Installing curl to PHP cli

I'm trying to set up a cronjob which requires curl, and I'm calling it directly from crontab with
* * * * * /usr/bin/php myurl/my_cron.php
The problem is, it looks like the curl module isn't installed for my phpcli.
It works just fine when I hit the url from my browser, but when I run
php -q myfile.php
from the command line, it returns
PHP Fatal error: Call to undefined function curl_init() in my_cron.php on line 20
When I run php -m the curl module does not show up. However when I go to the browser and dump the php_info(), the module shows up and says its correctly installed.
The other kicker is i've been trying to install curl with apt-get onto the server (Ubuntu 12.04 php 5.4), it seems to take down my PHP as it begins to simply attempt to download the index.php file wherever I try to browse to.
Here are the attempts I've made to install curl that have taken down PHP:
sudo apt-get install php-curl
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
After each of these I restarted the apache2 server and still no dice, it attempted to download the file instead of opening the page.
How can I install php5-curl to just the cli, so that my server can run it and I don't have to go through a browser?
The other possibility is I could run the cronjobs through wget from the crontab file, but I've heard that's not the best option and potentially unreliable.
Any help is much appreciated. Thanks!
I had the same issue. But, finally I solved it by running the following command.
sudo apt-get install php7.0-curl
Restart the server after installing. This answer may not be useful for the user who asked because he asked it two months ago. But, this may be useful for the users who reading this in the future.
Here's how I've fixed this on ubuntu 14.04 when curl was working in php files run through apache, but not when called from the cli.
ssh to your server and cd to /
find / -name 'curl.so'
Run the above find command to locate where the curl binary is hanging out at. If you can't find the file, you might need to install curl and run the find command again.
apt-get install php5-curl
You'll now want to edit the php.ini being used for php files run from the cli (it's different than the one used by apache), and is likely at /etc/php5/cli/php.ini
nano /etc/php5/cli/php.ini
You can also run
php -i | grep 'php.ini'
To get the file path, just to be sure.
In your php.ini file search for [curl] by pressing ctrl + w
You'll now want to add the extension to the file and it should look something like the following, though your path to the curl.so file and such might be a little different:
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =
extension=/usr/lib/php5/20131226/curl.so
After doing the above, I was able to use curl in php scripts run from the cli.
first find the version of your php cli by:
php -v
for example if it was version 7 then:
sudo apt-cache search php7
this will give you the proper module names for your current version:
php7.0-curl - CURL module for PHP <---- the name of curl module.
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mysql - MySQL module for PHP
.
.
so on
so to add curl support, copy the name of curl module from the list above then do the following:
sudo apt-get install php7.0-curl
If you are using the command-line interface ('cli') for php5, instead of
php -q myfile.php
please use:
php5 -q myfile.php
php5-curl seems to enable the curl module for the cli php5 and not php and both (can) load different configurations and modules.
I use ubuntu 14.04 and php 5.3. After upgrading to php 5.6.29 I also has problem with php curl. My directory structure after updating to php 5.6.29:
/etc/php5 - old version (5.3)
/etc/php/5.6 - new version
The next command
sudo apt-get install php5-curl
didn't help (looks like it connects to old php version - 5.3).
I have found next article: php 5.6 for magento
It advice to use command
apt-get -y install php5.6-curl
instead of
apt-get -y install php5-curl
It works for me!
The first thing you should always check is your php.ini file. You should have a php.ini file in your web root. Curl is installed by default on most web servers; I haven't found a web server with PHP that hasn't already had curl installed. Its not always enabled, though.
Check your your php.ini file and search for php_curl.dll, it should look like this:
;extension=php_curl.dll
Just remove the semicolon (;) from before "extension" and save the file. It should work right away. According to your phpinfo.php its already installed, so it likely just needs to be enabled.
A similar question can be found here if you're interested: Call to undefined function curl_init()
In case someone reached here to find windows version of running curl.
Open php.ini and remove the ; before extension=php_curl.dll around line 656.
I am pretty much sure what Apache loads is C:\wamp\bin\apache\Apache2.2.17\bin\php.ini therefore you may find curl working from browser.
But when php is run from command line then it may show unknown function curl_init();
Run php -r "echo php_ini_loaded_file();" in the command line to see which ini file is being loaded.
Usually its found inside C:\wamp\bin\php\php5.3.5\php.ini its a different file from what Apache is using. So open it and then remove the ; before extension=php_curl.dll around line 656.
Hope it helps someone.

php -v command does not work

I have just installed the php5 package on my Debian system using the standard apt-get. The code:
<?php
phpinfo();
?>
works and other php pages work.
But the commands php -v and php -m do not work. It gives an error "-bash: php: command not found".
php5-cli is installed. My path is /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games. I looked into usr/bin and there is no php or php5 file. Not sure where else those can be located?
I ran:
whereis php5
php5: /etc/php5 /usr/lib/php5 /usr/lib64/php5 /usr/share/php5
Dont think any of those are the correct file?
Make sure that the PHP CLI package is installed. sudo apt-get install php5-cli.
Also, the PHP binary needs to be added to your path. If you run which php and get no output, then it is certain that it is not in your path.
The php command is probably not in your path. You can view your path with echo $PATH. If the directory where the php binary is installed is not in your path, you can set it with export PATH=$PATH:/path/to/php.

Enabling/Installing Curl Extension (OSX Server/PHP 5.3.3/Apache 2.2)

I've been having some trouble getting CURL working with PHP on a server I inherited.
So far I have enabled the extension in my php.ini by uncommenting the extension=php_curl.dll line, and restarting apache.
However, I now receive the following error when starting PHP:
PHP Warning: PHP Startup: Unable to load dynamic library '/opt/local/lib/php/extensions/php_curl.dll
The php_curl.dll file doesn't exist in the aforementioned directory, and I can't find anywhere legitimate to download it from(doesn't seem to be included in PHP, or Curl).
UPDATE
Following the steps provided in the answer here, I was able to compile a new php_curl.so file and install it to the extensions directory.
Cudos go to Francois Deschenes!!
To compile a curl.so (php_curl.dll) module from scratch:
Download and extract a new copy of your version of PHP.
Open a terminal window and go to the curl directory (Type cd php-5.3.3/ext/curl/).
Type phpize.
Type ./configure.
Type make.
Type sudo make install.
Uncomment extension=curl.so in your php.ini.
You should also make sure the extension is commented out before you start the processes, otherwise you may receive an error about the module already existing.
Why don't use
sudo port install php5-curl
(on php54: php54-curl)?
Download the 'port' using the following URL and install it:
http://www.macports.org/install.php
There are different versions and you can select one installation pack for your Mac Version.
After installed, open a terminal and type the following command to install directly,
$ sudo port install php5-curl
Otherwise
- Login to port terminal by just typing
$ sudo port
and type
> install php5-curl
It will get few minutes to install all the dependancies and finally you will see the following message
---> No broken files found.
Following the steps provided in the answer here, I was able to compile a new php_curl.so file and install it to the extensions directory.
Cudos go to Francois Deschenes!!
To compile a curl.so (php_curl.dll) module from scratch:
Download and extract a new copy of your version of PHP.
Open a terminal window and go to the curl directory (Type cd php-5.3.3/ext/curl/).
Type phpize.
Type ./configure.
Type make.
Type sudo make install.
Uncomment extension=curl.so in your php.ini.
You should also make sure the extension is commented out before you start the processes, otherwise you may receive an error about the module already existing.

Categories