PHP modules, or extensions, can greatly extend the core functionality of PHP. These modules are compiled libraries that can help you implement different methods to connect to other services without the need to “reinvent the wheel”. By enabling these modules, your php code can become more versatile and adaptable, allowing you to seamlessly interact with things such as MySQL. The focus of this article is to explain how you can enable these modules in a CentOS 7 server so that you can take your php code to the next level. This article implies that you have already installed PHP on your CentOS 7 server, however, if you have not done this yet please take a look at our article: How to install PHP (CentOS 7).
How To Install PHP Modules In CentOS 7
Before getting started you’ll need to login to your server using SSH. If you’re not familiar with how to do this, or you find yourself really enjoying our articles, please take a moment and read through the following: Connecting to Your Server via SSH. Once you have connected to your server via SSH you can move on with the installation. Like any other installable software in CentOS 7, you’ll be installing PHP modules using the “yum” package manager. The basic syntax to install modules is identical to installing other packages, essentially it would be yum install <package-name>. But what if you’re not sure what PHP modules you’d like to install? By using Yum you can actually perform a search for different packages, in this case PHP modules like so:
yum search php-
This will search for anything that has “php-” in its name and display a list of matching packages on your monitor. This tells you what packages are available, but what if you need to know what they do? In this instance you can either put your “Google fu” to work or you can use the yum info command with the following syntax:
yum info <package-name>
For example, let’s pretend that you want to know more about the php-cli module, you’d issue the following:
yum info php-cli
This would generate output similar to the following:
Installed Packages Name : php-cli Arch : x86_64 Version : 5.3.3 Release : 48.el6_8 Size : 6.2 M Repo : installed From repo : updates Summary : Command-line interface for PHP URL : http://www.php.net/ License : PHP Description : The php-cli package contains the command-line interface : executing PHP scripts, /usr/bin/php, and the CGI interface.
As you can see the test server that I am using already has this module installed, however if I needed to install it I would do so via the following command:
yum install php-cli
You can of course install multiple modules at once by using the following syntax:
yum install package-one package-two package-three
A practical example of this would be:
yum install php-cli php-fpm php php-punic
How To View Current PHP Modules
When logged into your server via SSH, type in the following command
yum list installed *php*
This will populate a list, like the following, showing you all the current PHP modules installed.
Repository information has been removed from the above example
How To Remove A PHP Module
After installing any PHP module, you of course will have the opportunity to delete them. To delete a PHP module, type the following command
yum remove php <php-module-name-here>
You can remove multiple PHP modules by typing any and all the names you wish to remove, example:
yum remove php <php-module-name-here> <php-other-module-name-here> <php-another-module-name-here>
A list of PHP modules will be listed for removal and typing these commands will require a [Y/N] (yes or no) response via a prompt in order to complete in the end.
Now your PHP modules chosen to be removed are uninstalled.
Like most things, moderation is the key and it is best to only install modules that you need. I hope that after reading this article you now feel more comfortable installing PHP modules. If you’re interested in testing your servers PHP processing, we have an article that you might enjoy which covers that: How To Create A PHP Info Page