This guide walks through installing PHP on Ubuntu 20.04 LTS, covering the default version, adding a PPA for newer releases, and installing common extensions. You’ll end up with a working PHP CLI and, if needed, Apache or Nginx integration.
What You’ll Need
- Ubuntu 20.04 LTS server or desktop with sudo access
- Internet connection
- Terminal access (Ctrl+Alt+T on desktop, SSH for servers)
Step-by-Step
- Update package lists
Open a terminal and run:sudo apt update
This refreshes the package index from Ubuntu’s repositories. - Install PHP (default version 7.4)
Run:sudo apt install php
This installs PHP 7.4, the default for Ubuntu 20.04, along with the CLI. Confirm withphp -v. - Install PHP extensions
Most web apps need extensions. Install common ones with:sudo apt install php-mysql php-xml php-mbstring php-curl php-zip php-gd
Adjust the list based on your project’s needs. - Install PHP-FPM (for Nginx or Apache with mod_php)
For Nginx, install PHP-FPM:sudo apt install php-fpm
For Apache, you can use mod_php (installed automatically with thelibapache2-mod-phppackage) or PHP-FPM with proxy modules. For simplicity, if using Apache, run:sudo apt install libapache2-mod-php
Then restart Apache:sudo systemctl restart apache2. - Install a specific PHP version (optional)
To get PHP 8.x, add the Ondřej Surý PPA:sudo add-apt-repository ppa:ondrej/phpsudo apt update
Then install, e.g., PHP 8.2:sudo apt install php8.2 php8.2-cli php8.2-mysql
You can have multiple versions installed and switch between them usingupdate-alternatives. - Verify the installation
Create a test file in your web root (e.g.,/var/www/html/info.phpfor Apache) with content<?php phpinfo(); ?>, then open it in a browser. You should see the PHP info page. For CLI, runphp -vto see the version. - Configure PHP (optional)
Edit thephp.inifile (location shown byphp --ini) to adjust settings likememory_limit,upload_max_filesize, anddate.timezone. After changes, restart PHP-FPM or Apache.
Common Mistakes
- Forgetting to install extensions – Many apps fail with cryptic errors like “Class not found” because required extensions are missing.
- Using the wrong PHP version – The default 7.4 may be outdated for some modern apps. Check your app’s requirements and install a newer version via the PPA.
- Not restarting services – After installing or configuring PHP, you must restart Apache or PHP-FPM for changes to take effect.
- Mixing mod_php and PHP-FPM – Having both can cause conflicts. Choose one method for serving PHP.
Tips
- Use
php -mto list loaded extensions and verify what you need is present. - For development, consider installing Xdebug for debugging:
sudo apt install php-xdebug. - Keep PHP updated with
sudo apt upgraderegularly to get security patches. - If you’re using Nginx, ensure the server block has
location ~ \.php$ { ... }configured to pass requests to PHP-FPM.
Troubleshooting
PHP not working in browser
- Check if Apache or Nginx is running:
sudo systemctl status apache2ornginx. - Verify the test file is in the correct web root and has proper permissions (644).
- For Nginx, ensure PHP-FPM is running and the server block includes the correct fastcgi_pass (e.g.,
unix:/run/php/php7.4-fpm.sock).
Command not found: php
- Check if PHP is installed:
dpkg -l | grep php. If not, runsudo apt install php. - If you installed a specific version, the command may be
php8.2instead ofphp. Useupdate-alternatives --config phpto set the default.
Extension not loading
- Install the extension package (e.g.,
php-mysqlfor MySQL). - Check if the extension is enabled in
php.ini(uncommentextension=mysqliif needed). - Restart the web server or PHP-FPM after changes.
PPA not found
- Ensure
software-properties-commonis installed:sudo apt install software-properties-common. - If the PPA fails to add, check your internet connection and try again.
Frequently Asked Questions
What PHP version does Ubuntu 20.04 default to?
Ubuntu 20.04 ships with PHP 7.4 in its default repositories. You can install newer versions via the ondrej/php PPA.
How do I switch between PHP versions?
Use update-alternatives --config php to choose the default CLI version. For web servers, you may need to adjust the PHP-FPM socket or Apache module.
Do I need PHP-FPM if I use Apache?
No, Apache can use mod_php (via libapache2-mod-php). However, PHP-FPM is often recommended for better performance and is required for Nginx.
How do I install PHP extensions?
Use sudo apt install php-, e.g., php-mysql, php-curl. For a specific version, prefix with the version number, like php8.2-mysql.
Where is the php.ini file?
Run php --ini to see the loaded configuration file path. Typically it’s /etc/php/7.4/cli/php.ini for CLI and /etc/php/7.4/apache2/php.ini for Apache.
Can I install PHP without a web server?
Yes, installing the php package alone gives you the CLI. You can run PHP scripts from the terminal without any web server.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Windows 10 Wi-Fi Not Connecting: Step-by-Step
- Scaling Headless Chrome: A Practical Guide
- Fix Windows Upgrade Error 0x80090011
- Fix Microsoft Store Apps Not Updating
- v0 Alternatives: Which Fits Your Web App Build?9.26 yd³
- Fix Hard Disk Error 0x80071ac3