Installing and Configuring PHP-FPM on Ubuntu Linux

30 minutes
  • 6 Learning Objectives

About this Hands-on Lab

Before we can start building our world-changing website or application on LEMP, we have to lay the foundation for the stack. In this hands-on lab, we will walk through installing and configuring PHP-FPM on Ubuntu Linux.

When the lab is complete, we will have a configured PHP-FPM installation on Ubuntu Linux.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Validate That the NGINX Server Is Installed, Enabled and Running

Become root:

sudo su -

Check the status of the nginx service:

systemctl status nginx

The service should be enabled and running.

Verify that we can load the default NGINX web page using curl:

By Public IP:

curl http://`cat /tmp/public_ip.txt`

By Public DNS:

curl http://`cat /tmp/public_dns.txt`
Install PHP-FPM

Install PHP-FPM using apt-get:

apt-get -y install php-fpm

Validate that the php7.2-fpm service is running, using systemctl:

systemctl status php7.2-fpm.service

The service should be enabled and running.

Test Access PHP Pages Using NGINX

We have two pre-configured PHP pages in /usr/share/nginx/html/. Let’s take a look at them:

more /usr/share/nginx/html/*.php

Now, try accessing the PHP files via NGINX:

curl http://`cat /tmp/public_dns.txt`/info.php
curl http://`cat /tmp/public_dns.txt`/hello.php

We can see the contents, but the PHP code is not being executed. Try accessing the PHP files at http://PUBLIC_DNS/info.php and http://PUBLIC_DNS/hello.php with a web browser.

Configure NGINX to use php-fpm to Process PHP Pages

We need to add a configuration block to the /etc/nginx/conf.d/default.conf file, so that NGINX uses php-fpm to process PHP pages:

vi /etc/nginx/conf.d/default.conf

After the location / block, add the following:

 location ~ .php$ {
     fastcgi_pass unix:/run/php/php7.2-fpm.sock;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
     include snippets/fastcgi-php.conf;
 }
Validate and Activate PHP Configuration

Before we reload NGINX, we need to validate the configuration:

nginx -t

Once everything checks out, reload the configuration:

systemctl reload nginx

Make sure NGINX is running:

systemctl status nginx
Test Access PHP Pages Using NGINX with PHP-FPM

Now, let’s try accessing the PHP files via NGINX:

curl http://`cat /tmp/public_dns.txt`/info.php
curl http://`cat /tmp/public_dns.txt`/hello.php

Now the PHP code is not being executed. You can try accessing the PHP files at http://PUBLIC_DNS/info.php and http://PUBLIC_DNS/hello.php with your web browser.

Additional Resources

Big State College (BSC) is a Large Ten Conference school in a Midwestern state. BSC is looking to deploy a centralized web hosting service using the LEMP stack.

As the engineers tasked with executing this project, we will be deploying and configuring PHP-FPM on an Ubuntu Linux server for developing and testing PHP code before deploying the new environment. Let's begin!

Connecting to the Lab Server

Use the credentials provided on the hands-on lab overview page, and log in using an SSH connection to the Public IP or DNS of the server as cloud_user.

Making a SSH connection using the ssh command. Replace <<public_IP_or_DNS>> with the Public IP or DNS provided on the overview page.

ssh cloud_user@<<public_IP_or_DNS>>

If you can't use ssh or prefer a GUI client, use the credentials provided on the hands-on lab overview page to configure your SSH connection to the lab server.

Lab Git Repository

Feel free to explore the configurations and code from the lab at: GitHub - linuxacademy/content-lemp-deep-dive

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?