Home>Article>Backend Development> Getting Started with PHP: PHP Extension Installation
When developing with PHP, we may need to use some PHP extensions. These extensions can provide us with more functions and tools, making our development work more efficient and convenient. But before using these extensions, we need to install them first. This article will introduce you to how to install PHP extensions.
1. What is a PHP extension?
PHP extensions refer to components that provide additional functions and services for the PHP programming language. These components can be installed and used through PHP's extension mechanism. PHP extensions can help us handle more types of data, view processing, improve performance, etc. At the same time, PHP extensions also make it easier to interact with other applications and tools. Common PHP extensions include but are not limited to: mysqli, PDO, gd, zip, etc.
2. How to install PHP extensions
Many operating systems come with installation packages for PHP extensions. Therefore, you can use your system's package management tools to install PHP extensions. For example, Debian or Ubuntu can use apt-get, CentOS or Red Hat can use yum.
Taking installing the mysqli extension in Ubuntu as an example, you can use the following command:
$ sudo apt-get update
$ sudo apt-get install php7.0-mysqli
This will automatically install and enable the mysqli extension.
PHP Extension Community Library (PECL) is the officially recommended extension source code manager for PHP. Using PECL, you can install PHP extensions available on the PHP official website. At the same time, you can also compile and install the PHP extension yourself from the source code.
PECL can be installed in Ubuntu with the following command:
$ sudo apt-get install php-pear
After installing PECL, you can install the mysqli extension using the following command:
$ sudo pecl install mysqli
If neither the installation package nor the source code is available, you can manually compile from the source code PHP extension.
Taking manual installation of redis extension as an example, you can use the following command:
$ wget https://github.com/phpredis/phpredis/archive/5.0.2.zip
$ unzip 5.0.2.zip
$ cd phpredis-5.0.2
$ phpize
$ ./configure
$ make
$ sudo make install
This is done Manually compile and install the redis extension.
3. Enable PHP extension
After the installation is complete, we need to set the extension to be loaded into PHP. Extensions can be enabled by changing the php.ini file.
Taking enabling the mysqli extension in Ubuntu as an example, you can use the following command to open the php.ini file:
$ sudo nano /etc/php/7.0/apache2/php.ini
Find and uncomment the following line:
extension=mysqli.so
After saving and closing the file, restart the Apache service:
$ sudo service apache2 restart
Now, you have successfully installed and enabled the mysqli extension.
4. Conclusion
The installation of PHP extensions may vary depending on the operating system and installation program, but in most cases it is simple. If you are unable to install a PHP extension, you can check the PHP logs and error messages to find out what the problem is and resolve various issues. This article introduces you to three commonly used extension installation methods, hoping to help you successfully complete the installation of PHP extensions.
The above is the detailed content of Getting Started with PHP: PHP Extension Installation. For more information, please follow other related articles on the PHP Chinese website!