Skip to content
Follow
Ubuntu Linux

How to Install PHP on Ubuntu Linux

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

You install PHP on Ubuntu Linux to run server-side web development scripts.

PHP is a popular open-source scripting language vital for dynamic websites and web applications.

Installing PHP on an Ubuntu computer requires specific settings for Apache or Nginx web servers. Apache and Nginx servers frequently run PHP programs, such as WordPress. This guide explains how to set up PHP for both server types.

We’ll help you install a specific version, such as the stable PHP 8.0 release, ensuring it integrates smoothly with your web server setup.

⚡ 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.

How to install PHP on Ubuntu with Apache support

Installing PHP on Ubuntu to work with Apache is straightforward using your terminal. You’ll run two simple commands: one to update your software list and another to install PHP along with the necessary Apache module. After installation, just restart Apache to make sure PHP is ready to go.

🐧Bash / Shell
sudo apt update
sudo apt install php libapache2-mod-php
⚠️Warning
After installing PHP, restart the Apache web server so the PHP modules apply. PHP is tightly integrated with Apache. If you change PHP and want the changes to apply, restart or reload Apache.

To do that, run the commands below.

To install PHP on Ubuntu for Nginx, you must set up PHP-FPM. PHP-FPM acts as a bridge between the Nginx web server and PHP. Nginx does not handle PHP files directly, so PHP-FPM processes those requests. The commands install PHP and configure it to work with your Nginx server.

Nginx doesn’t natively handle PHP files like Apache does. To get PHP working with Nginx, you’ll need to install and use PHP-FPM (FastCGI process manager), which is a tool that helps manage PHP requests.

Run the commands below to set that up.

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

Since PHP isn’t as integrated with Nginx, you’ll need to restart or reload both PHP and Nginx separately whenever you make PHP changes.

⚠️Warning
The Nginx server block configuration requires adding lines to process PHP files. Ensure the specific PHP version appears in the highlighted line, for example, `fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;`. This action allows Nginx to communicate with the PHP processor.

PHP installation includes optional extensions that improve performance and add features. Common extensions include `php-mysql` for database access and `php-curl` for transferring data with URLs. You can install these extensions by running the `apt install` command.

To install a newer PHP version on Ubuntu Linux when the newest PHP version is not available in Ubuntu's default software sources, you can add an extra repository. This process involves adding a tool to manage software sources and then a specific PPA (Personal Package Archive) that contains newer PHP versions. The commands below demonstrate how to set up the extra repository.

To install the latest of other versions of PHP that are not available in the Ubuntu repository, run the commands below to install a third-party PPA repository that includes multiple versions of PHP.

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

After adding the repository above, you can then install another PHP version.

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

That should do it!

Conclusion:

That’s how you install PHP on Ubuntu Linux with support for either Apache or Nginx. If you notice any errors or have anything to add, please let us know in the comments below.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Mount Windows 11 Shares on Ubuntu Linux
Ubuntu Linux How to Mount Windows 11 Shares on Ubuntu Linux
How to Install Apache Solr on Ubuntu Linux
Ubuntu Linux How to Install Apache Solr on Ubuntu Linux
How to Install Mastodon CMS on Ubuntu Linux
CMS How to Install Mastodon CMS on Ubuntu Linux
How to Change Default Distro in Windows Subsystem for Linux
Windows How to Change Default Distro in Windows Subsystem for Linux

43 Comments

Leave a Comment

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