As a developer, managing PHP modules on your Ubuntu system can be an essential part of your workflow. Modules are packages that extend the functionality of PHP, and they are a key aspect of creating dynamic and powerful web applications. This article provides a comprehensive guide on enabling and disabling PHP modules in Ubuntu, streamlining your experience and optimizing your development environment.
All the installed PHP modules configuration files are available under /etc/php/{php_version}/mods-available directory. You can see the number of files with extension .ini. You must have installed specific PHP modules, you need to enable before using this tutorial. The php-common package provides followings commands to manage PHP modules.
- phpenmod – Used to enable modules in PHP
- phpdismod – Used to disable modules in PHP
- phpquery – Used to view status of modules of PHP
There are 3 types of SAPI (Server API) available – CLI, FPM, and Apache2 being the most commonly used. You can define SAPI using -s
switch to enable/disable module for that only.
Enabling PHP Modules on Your System
PHP modules enhance the functionality of your PHP installations, allowing for a more versatile development environment. The phpenmod command is a utility that simplifies the process of enabling these modules across different PHP versions and Server API (SAPI) environments. This guide provides detailed instructions on using phpenmod to enable PHP modules effectively.
1. Basic Command to Enable PHP Modules
To enable a PHP module, the basic syntax of the phpenmod command is as follows:
phpenmod MODULE_NAME
For instance, to enable the mbstring module, which is essential for handling multibyte string functions in PHP, you would use:
phpenmod mbstring
This command enables the mbstring module for all installed PHP versions and all SAPIs on your system.
2. Specifying PHP Versions
If you wish to enable a module for a specific PHP version, the phpenmod command can be modified using the -v switch followed by the version number. This is particularly useful when different projects on the same system require different PHP versions.
Syntax
phpenmod -v <PHP VERSION> <MODULE NAME>
Examples
To enable the mbstring module specifically for PHP 8.2 and 7.4, the commands would be:
phpenmod -v 8.2 mbstring
phpenmod -v 7.4 mbstring
These commands ensure that the mbstring module is enabled only for the specified PHP versions.
3. Targeting Specific Server APIs (SAPI)
The -s switch allows you to specify the SAPI type for which you want to enable the module. This feature is helpful when you need to enable a module for specific server environments such as CLI, FPM, or Apache2.
Syntax
phpenmod -s <SAPI> <MODULE NAME>
Examples
To enable the mbstring module for different SAPIs, use the following commands:
- For Command Line Interface (CLI):
phpenmod -s cli mbstring
- For FastCGI Process Manager (FPM):
phpenmod -s fpm mbstring
- For Apache 2:
phpenmod -s apache2 mbstring
These commands activate the mbstring module specifically for the chosen SAPI environments, ensuring that your PHP configuration is optimized for your development and deployment needs.
4. Combining PHP Version and SAPI
For even more precise module management, you can specify both the PHP version and the SAPI when enabling a module. This approach provides the most granular control, allowing you to target specific combinations of PHP versions and server environments.
Example: To enable the mbstring module for PHP 7.4 running under the FPM SAPI, the command would be:
phpenmod -v 7.4 -s fpm mbstring
This command ensures that the mbstring module is enabled only for PHP 7.4 with FPM, making it a powerful tool for fine-tuning your PHP environment.
Disabling PHP Modules
You can also disable any un-necessary PHP modules from your system using phpdismod command. For example, disable mbstring module for ALL PHP versions and all SAPI.
phpdismod mbstring
To disable any module for a specific PHP version use the command below.
phpdismod -v 8.2 mbstring
To disable any module for specific SAPI on all PHP versions, use the command below.
phpdismod -s apache2 mbstring
Restarting the Web Server
Once you have enabled or disabled a PHP module, you need to restart your web server for the changes to take effect. Depending on your web server, you can use the following commands:
For Apache:
sudo systemctl restart apache2
For Nginx with PHP-FPM:
sudo systemctl restart php8.x-fpm
sudo systemctl restart nginx
Replace 8.x with your PHP version (e.g., php8.2-fpm).
Verify PHP Module Status
After restarting the web server, you can verify if a PHP module is enabled or disabled by running the following command:
php -m | grep <module-name>
If the command returns the module name, it is enabled. Otherwise, the module is disabled.
Conclusion
Managing PHP modules on your Ubuntu system is a crucial part of optimizing your development environment. By following this guide, you can now easily enable and disable PHP modules on your Ubuntu system. Remember to restart your web server after making changes to ensure the new settings take effect. With this knowledge in hand, you’re now ready to enhance your PHP development experience on Ubuntu.
4 Comments
Missing phpquery example here, could you please fill it
Thanks mate.
Great pal!
Thanks for writing useful article… Finally I found an article to understand about php module management…