Skip to content
Follow
Ubuntu Linux

How to Install PHP on Ubuntu Linux

Richard
Written by
Richard
Sep 15, 2021 Updated Oct 10, 2026 5 min read
Enable Automatic Suspension in Ubuntu Linux Easily
Enable Automatic Suspension in Ubuntu Linux Easily

Installing PHP on Ubuntu Linux lets you run dynamic websites and web applications like WordPress on your server. PHP is a server-side scripting language. It turns plain text files into interactive web pages that can show live data from a database.

Advertisement

Most modern web projects run best on PHP 8.0 or newer, which gives you speed improvements and current security updates. Setting up PHP takes a few terminal commands. It links the language to your web server, usually Apache or Nginx, so your browser can read and display PHP pages instead of downloading them as text.

Before you start, make sure you have an Ubuntu system with a user account that can use sudo, and an internet connection. The steps work on current Ubuntu LTS releases. Each release ships a different default PHP version, so the version you get from the default sources depends on your Ubuntu release.

⚡ Quick Answer

Install PHP on Ubuntu by opening your terminal and running `sudo apt update` followed by `sudo apt install php libapache2-mod-php` for Apache or `sudo apt install php-fpm` for Nginx. Restart your web server afterward.

Advertisement

How to install PHP on Ubuntu with Apache support

To install PHP with Apache support, follow these steps:

  1. Open the terminal. You can press Ctrl+Alt+T on most Ubuntu desktops, or connect to your server over SSH.
  2. Run the update command first, so Ubuntu refreshes its list of available packages.
  3. Run the install command for PHP and the Apache PHP module. Type Y if apt asks you to confirm, then wait for it to finish.
  4. Restart Apache so it loads the new module.

If the terminal asks for a password, type your own account password. Nothing shows on screen while you type, and that is normal. Installing the Apache module also installs PHP itself, so you do not need a separate step for it.

🐧Bash / Shell
sudo apt update
sudo apt install php libapache2-mod-php

After installing PHP, restart the Apache web server so it picks up the PHP module. PHP is tightly integrated with Apache. Whenever you change PHP settings or install a new extension and want the change to apply, restart or reload Apache.

Advertisement

A reload applies configuration changes without dropping active connections. A restart stops and starts the service, which is the safer choice right after an install. On a busy production server, prefer a reload when it is enough.

Run the commands below in the order shown. If a command returns an error, read the message before you go on. It usually names the package or service that failed, and it is much easier to fix at this point than after you have changed more settings.

Nginx needs a different setup. To use PHP with Nginx, you must install PHP-FPM (FastCGI Process Manager). PHP-FPM acts as a bridge between the Nginx web server and PHP. Nginx does not handle PHP files directly, so it passes those requests to PHP-FPM, which runs the code and sends the result back. The commands in this section install PHP and set it up to work with your Nginx server.

Unlike Apache, Nginx does not natively process PHP files. Run the commands below to install PHP-FPM, which handles these requests. Nginx itself must already be installed on the server.

Advertisement

Run the commands below to set that up. When they finish, note the PHP version that was installed. You need it for the Nginx configuration later, because the PHP-FPM socket name includes the version number.

🐧Bash / Shell
sudo apt update
sudo apt install php-fpm

PHP is not built into Nginx the way it is with Apache, so PHP and Nginx run as two separate services. Whenever you change PHP settings, restart or reload PHP-FPM. When you change the Nginx configuration, restart or reload Nginx. Changing one does not apply the change to the other.

You need to add lines to your Nginx server block configuration so Nginx sends PHP files to PHP-FPM. Make sure the line that points to the PHP socket shows the version you installed, for instance `fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;`. If the version in this line does not match the installed PHP-FPM version, Nginx cannot reach PHP and you will usually see a 502 Bad Gateway error.

After you save the file, test the Nginx configuration for syntax errors and then reload Nginx. Doing this in that order keeps a typo from taking your site down.

Advertisement

A basic PHP installation does not include everything most applications need. Optional extensions add features and can improve performance. Common ones include `php-mysql` for database access and `php-curl` for transferring data with URLs. You can install extensions with the `apt install` command.

Install only the extensions your application asks for. Applications like WordPress list the PHP modules they need in their documentation. After you install an extension, restart Apache, or PHP-FPM if you use Nginx, so PHP loads it.

Sometimes the newest PHP version is not available in Ubuntu's default software sources. In that case you can add an extra repository. First you add a tool that manages software sources. Then you add a PPA (Personal Package Archive) that contains newer PHP versions. The commands below show how to set up the extra repository.

A PPA is maintained by a third party, not by Ubuntu. Use one you trust, and test it on a staging server before you use it on a production system.

Advertisement

To install other PHP versions that are not in the Ubuntu repository, run the commands below. They add a third-party PPA that carries several PHP versions side by side. This is useful when one site needs PHP 8.3 and an older application still needs an earlier release.

🐧Bash / Shell
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

After you add the repository, run the update command again so Ubuntu reads the new package list. Then you can install another PHP version. Install the matching extensions for that version too, since extensions are packaged per version.

If you run Nginx, point the socket line in your server block to the new version. If you run Apache, check which PHP version the Apache module uses and switch it if needed. To undo the change, remove the PHP packages for that version and remove the PPA.

🐧Bash / Shell
sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

43 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *