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
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).
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.
sudo apt update
sudo yum update
or
sudo dnf update
To install PHP, use the package manager associated with your distribution:
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
sudo yum install php
or
sudo dnf install php
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.
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:
sudo apt install php-{extension}
For example, to install the MySQL extension, run:
sudo apt install php-mysql
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
If you have a web server installed (like Apache or Nginx), you will need to restart it to enable PHP:
sudo systemctl restart apache2
(Debian/Ubuntu) or
sudo systemctl restart httpd
(CentOS/RHEL)
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.
Installing PHP on Ubuntu/Linux: On Ubuntu, you can install PHP using the following commands:
sudo apt-get update sudo apt-get install php
Step-by-step PHP installation on CentOS:
CentOS uses the yum
package manager. To install PHP, use:
sudo yum install php
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
Installing PHP on Red Hat Linux:
Red Hat, like CentOS, uses yum
. Install PHP with:
sudo yum install php
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.