Home > Article > Backend Development > Solution to PHP Fatal error: Call to undefined function ssh2_exec()
PHP is a widely used server-side programming language that supports a variety of operating systems and web servers. When using the SSH (Secure Shell) protocol for remote connection in PHP, the error message "PHP Fatal error: Call to undefined function ssh2_exec()" may appear. This article will explain how to resolve this error.
1. Install the ssh2 extension
First, you need to install the ssh2 extension. This extension provides the functionality for PHP to interact with the SSH server. Can be installed via the following commands:
In Ubuntu and Debian, use the following commands:
sudo apt-get install libssh2-1-dev libssh2-php
In CentOS and Red Hat Enterprise In Linux, use the following command:
sudo yum install gcc php-devel php-pear libssh2 libssh2-devel
sudo pecl install ssh2
After the installation is complete, you need to add the ssh2 extension to the php.ini file. You can find the location of the php.ini file with the following command:
php -i | grep 'Loaded Configuration File'
Then, open the php.ini file and add the following content at the end:
extension=ssh2.so
Save and close the file.
3. Restart the Web server
After completing the above steps, you need to restart the Web server for the new settings to take effect. In Ubuntu and Debian, restart the Apache server using the following command:
sudo service apache2 restart
In CentOS and Red Hat Enterprise Linux, restart the Apache server using the following command:
sudo service httpd restart
4. Test ssh2 extension
To test whether the ssh2 extension has been installed correctly, you can create a simple PHP script:
if (!function_exists("ssh2_connect")) die("Unable to connect to the SSH server, please install the ssh2 extension.");
else echo "The ssh2 extension has worked normally.";
?>
Save as ssh2test.php and access the file in the browser. If you see the message "The ssh2 extension has worked properly.", it means that the ssh2 extension has been successfully installed and is working properly.
Summary
When using the SSH protocol for remote connection, when the error message "PHP Fatal error: Call to undefined function ssh2_exec()" appears, you can solve it by installing the ssh2 extension and reconfiguring the web server. . Please follow the steps described in this article to ensure that the ssh2 extension is working properly in your PHP application.
The above is the detailed content of Solution to PHP Fatal error: Call to undefined function ssh2_exec(). For more information, please follow other related articles on the PHP Chinese website!