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

xargs Command in Linux: Build and Execute Commands from Input

The xargs command in Linux reads items from standard input and passes them as arguments to another…

1 day ago

locate Command in Linux: Find Files Fast with a Database Search

The locate command in Linux searches for files and directories by name. It queries a pre-built database…

1 day ago

Listing Linux Services with systemctl: A Complete Guide

Most modern Linux distributions use systemd as the default service manager. Knowing how to list…

1 day ago

How to Truncate Files in Linux: Empty Files Without Deleting

Truncating a file in Linux means removing its contents while leaving the file itself in…

1 day ago

ss Command in Linux: Display Socket Statistics and Connections

The ss command in Linux lists open sockets and active network connections. It replaced the deprecated netstat command and…

2 days ago

ftp Command in Linux: Connect to Servers and Transfer Files

The ftp command in Linux connects to a remote FTP server and transfers files. FTP transmits everything…

2 days ago