Step-by-Step Guide on Installing PHP on Linux
Step-by-step PHP installation and configuration on Linux with Apache integration
If you're planning to build dynamic web applications, PHP is one of the most reliable scripting languages you can use. Installing PHP on a Linux system (especially Ubuntu or Debian-based distributions) is simple when done correctly. In this guide, we’ll walk you through the step-by-step process of installing PHP on Linux, configuring it with Apache, and testing your setup.
PHP (Hypertext Preprocessor) is a popular open-source scripting language designed for web development. It runs on various operating systems, including Linux, Windows, and macOS, and works seamlessly with web servers like Apache and Nginx.
Some key benefits of using PHP include:
Easy integration with HTML and databases like MySQL.
Platform independence.
Support for popular frameworks (e.g., Laravel, CodeIgniter).
Excellent community support and documentation.
Before installing PHP, make sure you have:
A Linux-based system (Ubuntu/Debian recommended).
Sudo privileges or root access.
An internet connection for downloading packages.
If you don’t have Apache installed yet, you can install it using:
sudo apt update
sudo apt install apache2 -y
It’s always a good practice to update your package repository before installing new software:
sudo apt update && sudo apt upgrade -y
This ensures you get the latest version of PHP and its dependencies.
To install the default PHP version available in your distribution’s repository, use:
sudo apt install php libapache2-mod-php php-mysql -y
Here’s what each package does:
php → Installs the core PHP interpreter.
libapache2-mod-php → Enables PHP to run with Apache.
php-mysql → Allows PHP to communicate with MySQL databases.
To verify the installation, check the PHP version:
php -v
You should see output like:
PHP 8.2.10 (cli) (built: Sep 5 2024 12:00:00)
Apache automatically configures PHP after installation. However, to ensure it uses PHP files by default, you can modify the DirectoryIndex directive:
sudo nano /etc/apache2/mods-enabled/dir.conf
Change the line from:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
to:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Then, restart Apache:
sudo systemctl restart apache2
To confirm PHP is working with Apache, create a test file in your web directory:
sudo nano /var/www/html/info.php
Add the following line:
<?php phpinfo(); ?>
Now, open your browser and visit:
http://localhost/info.php
If PHP is properly configured, you’ll see a detailed page showing PHP version, configuration settings, and loaded modules.
Depending on your application requirements, you may need extra PHP extensions. You can search for available modules using:
apt search php- | less
To install a module, for example php-curl, use:
sudo apt install php-curl -y
After installing new modules, don’t forget to restart Apache:
sudo systemctl restart apache2
If you have multiple PHP versions installed, you can switch between them using the update-alternatives command:
sudo update-alternatives --config php
This will list all installed PHP versions and allow you to choose the default one.
To enhance security:
Disable display_errors in production:
sudo nano /etc/php/8.2/apache2/php.ini
Set:
display_errors = Off
Regularly update PHP:
sudo apt update && sudo apt upgrade -y
Once you’ve verified the installation, it’s important to remove the test file:
sudo rm /var/www/html/info.php
This prevents unauthorized users from viewing your server configuration details.
Installing PHP on Linux is a straightforward process that takes only a few minutes. Once configured, PHP becomes a powerful foundation for developing web applications, integrating seamlessly with Apache and MySQL.
By following this step-by-step PHP installation guide on Linux, you can quickly set up a reliable PHP environment for your next project.