Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo apt update
- Install PHP
Install PHP along with some common PHP modules using theapt
package manager:
sudo apt install php libapache2-mod-php php-mysql
This command installs PHP, the Apache PHP module, and the PHP MySQL extension.
- Verify PHP Installation
Check the PHP version to ensure it is installed correctly:
php -v
You should see the PHP version information displayed.
- Test PHP with Apache
To test PHP with Apache, create a new PHP file in the web root directory:
sudo nano /var/www/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
?>
Save and close the file.
- Restart Apache
Restart Apache to apply the changes:
sudo systemctl restart apache2
- Access the PHP Info Page
Open your web browser and visithttp://your_server_ip/info.php
. You should see the PHP information page, indicating that PHP is working correctly with Apache. - Install Additional PHP Modules (Optional)
You can install additional PHP modules as needed. To search for available PHP modules, use:
sudo apt-cache search php-
To install a specific PHP module, use:
sudo apt install php-module_name
Replace module_name
with the name of the module you wish to install.
- Configure PHP Settings (Optional)
You can configure PHP settings by editing thephp.ini
file. The location of thephp.ini
file may vary depending on your PHP version. For PHP 7.4, the file is typically located at/etc/php/7.4/apache2/php.ini
:
sudo nano /etc/php/7.4/apache2/php.ini
After making changes, restart Apache to apply them:
sudo systemctl restart apache2
- Remove the PHP Info Page
For security reasons, it’s a good idea to remove the PHP info page after testing:
sudo rm /var/www/html/info.php