Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Install PHP Extension Modules in LNMP

In this tutorial, we will walk you through installing additional PHP extension modules in an LNMP (Linux, Nginx, MySQL, PHP) environment. PHP extensions add extra functionality to PHP, such as database support, encryption, and more. Depending on your requirements, you may need to install specific PHP extensions for your application.

Note: This tutorial assumes that you have already installed an LNMP stack on your server. If you haven't done so yet, follow the tutorial on one-click LNMP installation before proceeding.

  • Update the package index

Before installing additional PHP extension modules, update the package index:

For Debian-based distributions (Debian, Ubuntu, Linux Mint):

sudo apt update

For RHEL-based distributions (RHEL, CentOS):

sudo yum update

For Fedora:

sudo dnf update
  • Install PHP extension modules

To install additional PHP extension modules, use your system's package manager. Here are a few examples of commonly used PHP extensions:

  • MySQLi extension (for MySQL database support):

    Debian-based distributions:

    sudo apt install php-mysqli
    

    RHEL-based distributions and Fedora:

    sudo yum install php-mysqli
    
  • GD extension (for image processing):

    Debian-based distributions:

    sudo apt install php-gd
    

    RHEL-based distributions and Fedora:

    sudo yum install php-gd
    
  • cURL extension (for making HTTP requests):

    Debian-based distributions:

    sudo apt install php-curl
    

    RHEL-based distributions and Fedora:

    sudo yum install php-curl
    

Replace the package name with the desired extension's package name if you need to install a different extension.

  • Restart the PHP-FPM service

After installing the PHP extension modules, you need to restart the PHP-FPM service for the changes to take effect.

Debian-based distributions:

sudo systemctl restart php-fpm

RHEL-based distributions and Fedora:

sudo systemctl restart php-fpm
  • Verify the installation

To verify that the installed extensions are enabled and functioning, create a phpinfo() page in the Nginx web root directory (usually /usr/local/nginx/html). If you haven't already created one during the LNMP installation tutorial, follow these steps:

  • Open a text editor and create a new file called info.php:

    sudo nano /usr/local/nginx/html/info.php
    
  • Add the following content to the file:

    <?php
    phpinfo();
    ?>
    
  • Save and close the editor.

Open a web browser and navigate to http://your_server_IP/info.php. You should see the PHP information page with details about your PHP installation. Search for the installed extensions to verify that they are loaded and enabled.

That's it! You have successfully installed PHP extension modules in your LNMP environment. You can now use the added functionality in your PHP applications.

  1. How to enable PHP modules in LNMP:

    Enable PHP modules by editing the PHP configuration file (php.ini) and uncommenting or adding the relevant module configuration lines.

    Example code:

    sudo nano /etc/php/7.4/fpm/php.ini
    

    Uncomment or add lines like:

    extension=mysqli.so
    

    Restart PHP-FPM for changes to take effect:

    sudo service php7.4-fpm restart
    
  2. Install MySQL PHP module in LNMP:

    To enable MySQL support in PHP, install the MySQL PHP module.

    Example code (on Ubuntu):

    sudo apt-get install php7.4-mysql
    

    Restart PHP-FPM after installation.

  3. PHP extension installation on Nginx:

    Some PHP extensions may require specific Nginx configurations. Ensure that Nginx is configured to pass PHP requests to PHP-FPM.

    Example Nginx configuration:

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    

    Restart Nginx for changes to take effect.

  4. Adding custom PHP modules to LNMP:

    Custom PHP modules can be added by compiling them from source or using package managers. Follow the specific installation instructions provided by the module's documentation.

    Example code (installing a custom module):

    sudo apt-get install libexample-dev
    sudo pecl install example
    

    Add the extension to the PHP configuration file and restart PHP-FPM.

    Example PHP configuration:

    extension=example.so
    

    Restart PHP-FPM:

    sudo service php7.4-fpm restart