Step 1: Update System Packages
Before installing PHP, it is recommended to update the system packages to their latest version. Open the terminal and run the following command:
sudo apt update && sudo apt upgrade -y
Step 2: Install PHP
To install PHP on Ubuntu 20.04 LTS, run the following command:
sudo apt install php libapache2-mod-php php-mysql
This command will install PHP along with some essential modules required for its proper functioning.
Step 3: Verify PHP Installation
To verify that PHP has been installed successfully, create a PHP file and save it in the Apache webroot directory:
sudo nano /var/www/html/info.php
Add the following code to the file:
Save and close the file. Now, open your web browser and enter the following URL:
http://localhost/info.php
This will display the PHP information page, which confirms that PHP has been installed successfully.
Step 4: Install Additional PHP Modules
Depending on your requirements, you may need to install additional PHP modules. To check the available PHP modules, run the following command:
sudo apt search php-
To install a specific module, run the following command:
sudo apt install php-[module_name]
Replace [module_name] with the name of the module you want to install.
Step 5: Configure PHP
PHP configuration files are located in the /etc/php/[version]/apache2/ directory. You can edit these files to configure PHP settings according to your requirements.
Step 6: Restart Apache
After making any changes to the PHP configuration files, you need to restart Apache for the changes to take effect. Run the following command to restart Apache:
sudo systemctl restart apache2
Step 7: Secure PHP Installation
To secure your PHP installation, it is recommended to disable the display of PHP errors. To do this, edit the /etc/php/[version]/apache2/php.ini file and set the display_errors directive to Off:
display_errors = Off
Save and close the file, and then restart Apache.
FAQs
- Q: Can I install PHP 7.x on Ubuntu 20.04 LTS?
- Q: Do I need to install Apache before installing PHP?
- Q: Can I install PHP modules manually?
- Q: How do I uninstall PHP from Ubuntu 20.04 LTS?
- Q: How do I check the PHP version installed on Ubuntu?
A: Yes, PHP 7.x is available in the Ubuntu 20.04 LTS repository. You can install it using the same method as described in this tutorial.
A: Yes, Apache is required to run PHP on Ubuntu. You can install Apache using the following command:
sudo apt install apache2
A: Yes, you can install PHP modules manually using the pecl command. However, it is recommended to install modules from the Ubuntu repository using the apt command.
A: To uninstall PHP, run the following command:
sudo apt remove php libapache2-mod-php php-mysql
A: To check the PHP version, open the terminal and run the following command:
php -v