Generally, source code is used to install php. The process of installing php specifies the extensions to be installed, but the problem of missing an extension that is not installed and causing an error when running the program cannot be avoided. Taking fileinfo as an example, we will introduce how to add php extension.
Usually you encounter an error: xxx function undefined, and if this function is a system function, it must be that you are missing an extension. Adding extensions is also very simple. The first step is to confirm which version of php is currently used. There will be slight differences in different versions of php extensions. Then find the Php source code directory, which is the php file package downloaded from the Internet and unzipped. When adding an extension, you need to execute the command in the source code package.
1. Confirm the php version
php -v # PHP 7.4.4 (cli)
2. Find the location of the php executable file
whereis php # php: /usr/local/php /usr/local/php/bin/php
You can see the php execution The file is in the /usr/local/php/bin directory
Then try to execute phpize to ensure that the file exists. The phpize command needs to be used when adding extensions
/usr/local/php/bin/phpize -v # Configuring for: # PHP Api Version: 20190902 # Zend Module Api No: 20190902 # Zend Extension Api No: 320190902
3. Enter the php source code Directory
My habit is that software downloaded from the Internet will be placed under /usr/local/src.
cd /usr/local/src/php-7.4.4 cd /etc cd fileinfo
Extensions are all under etc in the source code directory, which contains curl, gd, ftp, etc. Waiting for the extension directory, I want to install fileinfo, so go to the fileinfo directory
4. Start the installation
Make sure there is a config.m4 file under the directory. If not, there should be a config0.m4 file. Copy config0.m4 to config.m4. If there is no such file, an error will be reported: can not found config.m4
/usr/local/php/bin/phpize ./configure make && make install
If no error is reported after running these lines of commands, it means that the expansion file has been generated. , and has been automatically moved into the corresponding position. The next step is to modify php.ini and add extension=fileinfo.so to php.ini.
5. Modify php.ini and load the extension file
View the location of php.ini
php -i | grep php.ini #Configuration File (php.ini) Path => /usr/local/php/etc #Loaded Configuration File => /usr/local/php/etc/php.ini
You can see the location of php.ini: /usr/local/php /etc/php.ini
vim /usr/local/php/etc/php.ini #添加extension=fileinfo.so,保存 php -m | grep fileinfo #fileinfo
See the output fileinfo. At this point, the fileinfo extension is installed! In addition, you can use the pecl command to install, which is more convenient and faster!
Recommended tutorials: "Linux Operation and Maintenance", "PHP Tutorial"
The above is the detailed content of How to install PHP extension in Linux? (Method introduction). For more information, please follow other related articles on the PHP Chinese website!