Installing PHP modules in Apache on Linux
Today I was doing some updates on the server and realized that not much is said about this topic, so I decided to talk about how to install PHP modules on Linux.
Modules
To install newer versions of some modules, you may need to install the following PPA (Personal Package Archive).
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get upgrade
Here we will see how to install the most commonly installed PHP modules. But if you want to see all the extensions that exist, you can take a look at this page.
If you don’t know where to look for your php.ini to enable them later, go to the end in the section PHP.INI.
CURL
It is used in command line or scripts for data transfer. It is used for many things such as naming them here but keep in mind that it is used in PayPal as well as in sending email messages.
In case you need curl, you will need to install it.
sudo apt-get install curl
then we can run this line:
sudo apt-get install phpX-curl
The php-curl module is not curl itself, but rather the module that allows PHP to communicate with curl.
DOM / XML
They require the libxml module, which is enabled by default.
The DOM module can be installed alone, but most of the time it is convenient to install XML directly since it will install multiple modules, including the DOM module.
sudo apt-get install phpX-xml
If you want to install it alone, you can use the following command:
sudo apt-get install phpX-dom
ZIP
This module allows you to read and write zip files as well as the files inside them.
It’s one of the modules that WordPress recommends along with curl and dom.
Install zip before running the module installation if it’s not already on your server (in this case, I’m talking about the PC).
apt-get update apt-get install zip unzip
The following command installs this module:
sudo apt-get install phpX-zip
GD
Note that libgd should come installed in Linux, but in any case, check the library’s website:
It allows us to create and manipulate images in different formats.
sudo apt-get install phpX-gd
MBSTRING
This is a PHP extension used to manipulate non-ASCII strings.
sudo apt-get install phpX-mbstring
Concatenation of Modules
In case you already know which PHP version you are going to install, you can concatenate the modules to install as well, just like it happens in the article about updating PHP on Apache (Linux).
PHP-INI
In Apache 2.4.29 the php.ini file is located in:
/etc/php/X/apache2
It is probably that you have multiple PHP versions installed, so it is recommended that you check which PHP version your app is using.
For example, if your app is using PHP 7.4 and you have PHP 7.4 and PHP 8 installed, you will have 2 php.ini files but you should only modify the one for 7.4. The path to your ini file would be as follows:
/etc/php/7.4/apache2
To find where the extensions are located, search for gd or mbstring since their names are extensions, and this way you can find all extensions quickly.
The modules are usually commented with a semicolon (;), so it is not necessary to add them.
To enable the module, you must uncomment it by removing the semicolon (;). Here is an example code snippet:
php.ini; Notes for Windows environments : ; ; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+) ; extension folders as well as the separate PECL DLL download (PHP 5+). ; Be sure to appropriately set the extension_dir directive. ; extension=bz2 extension=curl ;extension=ffi ;extension=ftp extension=fileinfo ;extension=gd extension=gettext ;extension=gmp ;extension=intl ;extension=imap ;extension=ldap extension=mbstring extension=exif ; Must be after mbstring as it depends on it extension=mysqli
If you want to enable curl, you need to remove the semicolon from ;extension=curl
, so it becomes extension=curl
. However, keep in mind that you need to have the curl module installed as well. Also, note that some modules come pre-installed with PHP.