How To

Install PHP on Ubuntu 18.04: Apache, Nginx FPM, and Ondrej PPA

PHP is the most widely used server-side scripting language for web development. Ubuntu 18.04 ships with PHP 7.2 in its default repositories, which is fully compatible with WordPress, Laravel, Drupal, and Nextcloud.

The installation method differs based on which web server you are using. Apache handles PHP through a module loaded directly into the server process. Nginx takes a different approach and delegates PHP processing to a separate service called PHP-FPM.

This guide covers how to install PHP on Ubuntu 18.04 with either Apache or Nginx, how to install PHP extensions, how to test your setup, and how to install PHP 7.3 or 7.1 using the Ondrej PPA.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Install PHP on Ubuntu 18.04 with Apache or Nginx

Apache. The Apache web server processes PHP through a loadable module. Install PHP and the module together:

bashsudo apt install php libapache2-mod-php

Restart Apache to load the module:

bashsudo systemctl restart apache2

Nginx. Nginx has no built-in PHP support. It passes PHP requests to PHP-FPM (FastCGI Process Manager), which runs as a separate service and communicates with Nginx through a Unix socket. This separation means PHP processes do not share memory with the web server process itself.

Install PHP-FPM:

bashsudo apt install php-fpm

Verify the service is running:

bashsystemctl status php7.2-fpm

Add a location block to your Nginx server block so that .php requests are forwarded to the FPM socket:

nginxlocation ~ \.php$ {    include snippets/fastcgi-php.conf;    fastcgi_pass unix:/run/php/php7.2-fpm.sock;}

Restart Nginx to apply the change:

bashsudo systemctl restart nginx

Install PHP Extensions and Test PHP Processing

PHP extensions expand the language’s capabilities, adding support for databases, image processing, caching, and more. Install extensions with:

bashsudo apt install php-[extname]

For example, to add MySQL and GD image support:

bashsudo apt install php-mysql php-gd

After installing any extension, restart the relevant service, Apache for the mod-php setup or PHP-FPM for the Nginx setup. Without a restart, the extension does not load into the running process.

Test PHP processing. Create a test file in your web root:

php&lt;?phpphpinfo();

Save it as /var/www/html/info.php and visit http://your_server_ip/info.php. A detailed PHP configuration page should load. Delete this file immediately after testing. Leaving phpinfo visible on a production server exposes your PHP version, loaded modules, and server paths to anyone who visits the URL.

Install PHP 7.3 or PHP 7.1 via the Ondrej PPA

The Ubuntu 18.04 repositories only provide PHP 7.2. For other versions, add the Ondrej PPA, maintained by Ondrej Surý, a Debian PHP package maintainer who provides up-to-date builds for Ubuntu. This PPA supports multiple PHP versions in parallel and is the standard method for running non-default PHP versions on Ubuntu 18.04.

Enable the PPA:

bashsudo apt install software-properties-commonsudo add-apt-repository ppa:ondrej/php

Install PHP 7.3 (latest stable at the time of writing):

bashsudo apt install php7.3 php7.3-common php7.3-opcache php7.3-cli php7.3-gd php7.3-curl php7.3-mysql

Install PHP 7.1 (legacy — use only if an application is not compatible with 7.2 or 7.3):

bashsudo apt install php7.1 php7.1-common php7.1-opcache php7.1-mcrypt php7.1-cli php7.1-gd php7.1-curl php7.1-mysql

Verify the active version:

bashphp -v

If multiple PHP versions are installed side by side, use sudo update-alternatives --config php to select the default CLI version without removing the other installations.

PHP is now installed on your Ubuntu 18.04 server. Whether you are running WordPress on Apache or a Laravel API behind Nginx FPM, the stack is ready. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

Install Nginx on Ubuntu 16.04: UFW, PPA, and Config Structure

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server and reverse proxy. It handles…

44 minutes ago

Install Nginx on Ubuntu 18.04: UFW, Service Control, and Config

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server and reverse proxy. It handles…

48 minutes ago

Secure Nginx with Let’s Encrypt on Ubuntu 18.04: SSL Setup Guide

Let's Encrypt is a free, automated, and open certificate authority run by the Internet Security Research…

52 minutes ago

Install Skype on Ubuntu 18.04: .deb Package and Auto-Updates

Skype is one of the most widely used communication platforms in the world. It lets you…

1 hour ago

Install Samba on Ubuntu 18.04: Configure Shares and User Access

Samba is a free, open-source implementation of the SMB/CIFS network protocol that lets Linux servers share…

22 hours ago

Set Up an OpenVPN Server on Ubuntu 18.04 with EasyRSA and UFW

Running your own VPN gives you full control over your traffic, privacy, and connection security. It encrypts…

22 hours ago