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 In Linux

PHP is a widely-used open-source server-side scripting language that is especially suited for web development. This tutorial will guide you through the process of installing PHP on a Linux system using package managers like apt (for Debian/Ubuntu-based distributions) and yum or dnf (for CentOS/RHEL-based distributions).

  • Update Package Lists

Before you install PHP, it's a good idea to update the package lists on your system. This ensures that you're using the latest available packages and dependencies.

  • For Debian/Ubuntu-based distributions, use:
sudo apt update
  • For CentOS/RHEL-based distributions, use:
sudo yum update

or

sudo dnf update
  • Install PHP

To install PHP, use the package manager associated with your distribution:

  • For Debian/Ubuntu-based distributions, use:
sudo apt install php

This will install the latest version of PHP available in the repository. To install a specific version, you can use php{version}, like php7.4 or php8.0:

sudo apt install php7.4
  • For CentOS/RHEL-based distributions, use:
sudo yum install php

or

sudo dnf install php
  • Verify PHP Installation

After the installation is complete, you can verify that PHP is installed correctly by running:

php -v

This will display the version of PHP installed on your system.

  • Install Additional PHP Extensions (Optional)

You may need to install additional PHP extensions for specific functionality, such as MySQL support or image processing. To do this, use the package manager associated with your distribution:

  • For Debian/Ubuntu-based distributions, use:
sudo apt install php-{extension}

For example, to install the MySQL extension, run:

sudo apt install php-mysql
  • For CentOS/RHEL-based distributions, use:
sudo yum install php-{extension}

or

sudo dnf install php-{extension}

For example, to install the MySQL extension, run:

sudo yum install php-mysqlnd

or

sudo dnf install php-mysqlnd
  • Restart Web Server (if applicable)

If you have a web server installed (like Apache or Nginx), you will need to restart it to enable PHP:

  • For Apache:
sudo systemctl restart apache2

(Debian/Ubuntu) or

sudo systemctl restart httpd

(CentOS/RHEL)

  • For Nginx:
sudo systemctl restart nginx

That's it! You've successfully installed PHP on your Linux system. You can now start using PHP to create dynamic web applications or run PHP scripts.

  1. Installing PHP on Ubuntu/Linux: On Ubuntu, you can install PHP using the following commands:

    sudo apt-get update
    sudo apt-get install php
    
  2. Step-by-step PHP installation on CentOS: CentOS uses the yum package manager. To install PHP, use:

    sudo yum install php
    
  3. PHP installation and configuration in Debian: Debian, similar to Ubuntu, uses the apt package manager. Install PHP with:

    sudo apt-get update
    sudo apt-get install php
    
  4. Installing PHP on Red Hat Linux: Red Hat, like CentOS, uses yum. Install PHP with:

    sudo yum install php
    
  5. Setting up PHP on a Linux server: After installing PHP, configure your web server (e.g., Apache or Nginx) to handle PHP files. For Apache, you might need to install the PHP module and restart the server:

    sudo apt-get install libapache2-mod-php
    sudo service apache2 restart
    

    On Nginx, you may need to include the PHP configuration in your server block:

    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;
    }
    

    Don't forget to restart Nginx after making changes:

    sudo service nginx restart
    

    Finally, create a test PHP file in your web server's document root to verify the installation:

    <?php
    phpinfo();
    ?>
    

    Open this file in a web browser, and it should display PHP information.