How to install PHP on Ubuntu with Apache or Nginx

PHP is a server-side scripting language used to build dynamic websites and web applications. It’s at the core of platforms like WordPress and Laravel, powering millions of sites worldwide. PHP is an industry standard because it’s fast, open-source, well-documented, and integrates easily with popular web servers such as Apache and Nginx.

In this guide, you’ll see how to install PHP on an Ubuntu system and get it working with either Apache or Nginx. We’ll cover everything from installing PHP and setting up your web server to testing your setup and adding useful extensions.

By the end, you’ll have a working PHP environment ready to run your website or application.

Prerequisites

Before you start, make sure you have the following:

  • An Ubuntu server – LTS version 20.04 or later. Hostinger offers Ubuntu VPS hosting that is quick and easy to set up.
  • Basic command line knowledge – you don’t need to be an expert, but you should be comfortable using the terminal.
  • A user with sudo privileges – required for installing packages and managing a web server.
  • SSH access – to connect to your remote server.

How to install PHP on Ubuntu

1. Update your package list

Before installing PHP, update your system’s package list. This ensures Ubuntu knows where to get any necessary installation files.

Use this command:

sudo apt update

It’s like checking with your suppliers before placing an order – if the catalog is outdated, your system might not find what you’re asking for.

2. Install PHP

To install PHP on Ubuntu, start with the default version available in the package manager. This is usually stable and well-supported.

sudo apt install php

If you need a specific version, like PHP 8.0, you’ll need to add a third-party Personal Package Archive (PPA) repository first. The following commands will add the most commonly used PPA for older versions of PHP maintained by Ondřej Surý, and will install your desired version of PHP:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.0

Make sure you replace php8.0 with the version you want.

Once that’s done, check if the PHP is running:

php -v

If you see version info without any errors, the installation was successful.

Make a note of the version, as you’ll need it later when setting up Apache or Nginx. In the screenshot above, the PHP version is 8.3.

3. Test PHP on the command line

Before setting up a web server, it’s best to verify if PHP is working as expected. This helps catch any problems early, before adding more moving parts.

php -r 'echo "PHP is workingn";'

You should see PHP is working in the output.

If you get that message, PHP is running fine and you’re ready to proceed.

4. Option 1: Install PHP with Apache

If you want to use PHP with the Apache web server, you’ll need to install Apache along with the PHP module that lets it handle .php files.

Start by installing Apache:

sudo apt install apache2

Then, install the PHP module for Apache that matches your installed PHP version. For example, if you’re using PHP 8.3:

sudo apt install libapache2-mod-php8.3

Restart Apache to load the new module:

sudo systemctl restart apache2

Now it’s time to check if everything’s working. Create a test file in the Apache root directory:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Open your browser and go to http://<your_server_ip>/info.php. Make sure you replace <your_server_ip> with the actual IP of your server.

You should see a page with PHP version details and configuration info. If this page loads as expected, you’re all set.

Once you’re done, you can delete the test file:

sudo rm /var/www/html/info.php

5. Option 2: Install and configure PHP with Nginx

Unlike Apache, Nginx doesn’t handle PHP files on its own – it passes them to the PHP-FPM processor. So, to use PHP with Nginx, you’ll need to install both Nginx and the matching version of PHP-FPM.

Start by installing Nginx:

sudo apt install nginx

Then, install the PHP-FPM module that matches your installed PHP version. For example, if you’re using PHP 8.3:

sudo apt install php8.3-fpm

Next, configure Nginx to work with PHP. Open the default site configuration file:

sudo nano /etc/nginx/sites-available/default

Look for the block that starts with location ~ .php$. Update it so it looks like the following, adjusting the version number in the socket path to match your PHP version:

location ~ .php$ {

    include snippets/fastcgi-php.conf;

    fastcgi_pass unix:/run/php/php8.3-fpm.sock;

}

Once updated, restart both services:

sudo systemctl restart php8.3-fpm

sudo systemctl restart nginx

To check if PHP is working, create a test file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Open your browser and go to http://<your_server_ip>/info.php. Make sure you replace <your_server_ip> with the actual IP of your server.

You should see a page with PHP version details and configuration info. If the page loads as expected, it means everything works as expected.

When you’re done testing, remove the file:

sudo rm /var/www/html/info.php

6. Install PHP extensions

At some point, you’ll likely need additional PHP extensions. The ones to install will depend on your project’s requirements – they add support for things like databases, XML, or HTTP requests.

A complete list of PHP extensions can be found in the official PHP documentation. Here are some commonly used ones:

  • php-mysql – for connecting to MySQL or MariaDB.
  • php-mbstring – for working with multibyte strings.
  • php-curl – for sending HTTP requests.
  • php-xml – for processing XML data.
  • php-zip – for working with ZIP files.

To install one or more, run the following, listing each extension like so:

sudo apt install php8.3-mysql php8.3-mbstring php8.3-curl

Make sure to match the version number with the PHP version installed on your system.

After installing extensions, always restart your web server and PHP service.

For Apache:

sudo systemctl restart apache2

For Nginx with PHP-FPM:

sudo systemctl restart php8.3-fpm

sudo systemctl restart nginx

To check currently installed PHP modules, run:

php -m

Conclusion

You’ve now installed PHP on Ubuntu and set it up to run with either Apache or Nginx web server. Whether you’re building a personal project or setting up a production site, having PHP properly configured is a solid first step.

If your application relies on a database, you can install MySQL on your Ubuntu server, which can be managed through a browser using phpMyAdmin.

How to install PHP on Ubuntu FAQ

What version of PHP should I install on Ubuntu?

It depends on the tools or frameworks your project uses. If you’re starting fresh, it’s recommended to go with the latest stable release available, which includes recent security updates and is usually the safest choice. Keep in mind that some applications may require an older version for compatibility, so check the requirements first. 

Do I need to install any additional PHP extensions?

Yes, most likely. A base PHP install usually isn’t enough for web applications, but this varies greatly by project. If you’re using any external tools or frameworks, check their documentation to see if any additional extensions are needed, then install them using your package manager.

Is there a graphical interface to install PHP on Ubuntu?

Not really. Most Ubuntu servers don’t have a graphical interface at all and are managed entirely through the command line. While Ubuntu Desktop offers tools like Synaptic, they’re rarely used for server setups. Installing PHP and managing packages is faster and more consistent using terminal commands.


All of the tutorial content on this website is subject to
Hostinger’s rigorous editorial standards and values.

Author
The author

Dovydas

Dovydas has worked in cloud engineering and IT operations, focusing on maintaining and improving complex systems. He’s especially interested in software development and automation and is always looking for ways to make things more efficient and reliable. You can find him on LinkedIn.

Similar Posts