How to Install Nginx with PHP-FPM on Ubuntu 18.04 LTS 2

How to Install Nginx with PHP-FPM on Ubuntu 18.04 LTS

This tutorial will help you to install Nginx web server with PHP-FPM/FastCGI on Ubuntu 18.04 (Bionic) LTS system. In this tutorial, we are using PHP 7.3 and configure with Nginx using PHP-FPM and FastCGI.

Nginx Installation

Nginx packages are available under default repositories. SSH to your Ubuntu 18.04 LTS system with sudo privileges account and install Nginx web server from the official repository.

sudo apt update 
sudo apt install nginx

PHP Installation

For the PHP installation we recommend to use ppa:ondrej/php PPA, which provides latest PHP versions for Ubuntu systems. Use the below couple of commands to add the PPA to your system.

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

Then install PHP 7.3 the latest version available on the day of writing this tutorial. Simply execute follows commands for the installation of PHP and PHP-FPM packages.

apt update
sudo apt install php7.3 php7.3-fpm
Note:- When you are using PHP-FPM. All the PHP modules configurations are residing under /etc/php/7.3/fpm directory. You can read more about enable/disable PHP modules.

After installing above packages php7.3-fpm service will automatically be started. You can make sure by typing below command on terminal.

sudo systemctl status php7.3-fpm

● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-11-21 03:36:08 UTC; 36s ago
     Docs: man:php-fpm7.3(8)
 Main PID: 9054 (php-fpm7.3)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 4704)
   CGroup: /system.slice/php7.3-fpm.service
           ├─9054 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
           ├─9069 php-fpm: pool www
           └─9070 php-fpm: pool www

Nov 21 03:36:08 tecadmin systemd[1]: Starting The PHP 7.3 FastCGI Process Manager...
Nov 21 03:36:08 tecadmin systemd[1]: Started The PHP 7.3 FastCGI Process Manager.

Nginx Configuration

Let’s create a Nginx virtual host to run with FPM/FastCGI. For this tutorial, we use default VirtualHost. Edit VirtualHost host configuration file in text editor. You can create new VirtualHost as per your requirements, so make sure to enable any new VirtualHost.

sudo vim /etc/nginx/sites-available/example.com

Use the below basic Nginx Virtual host configuration with php fpm settings. Update the configuration as followings.

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm;
        server_name example.com;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ .php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }
}

Save your changes to the configuration file and create a link to site enabled directory.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com 

Then restart Nginx service to reload the changes.

sudo systemctl restart nginx.service

Test Setup

Create a PHP script with phpinfo() function and place it to your server document root. Use below command to do it.

echo "" > /var/www/html/info.php

Now access the info.php using server IP address (for default VirtualHost) for your configured domain in Nginx VirtualHost.

<imgsrc=”https://www.ubuntu-server.com/wp-content/uploads/2020/04/nginx-php-fpm-ubuntu1804.png” alt=”Nginx PHP-FPM Ubuntu 18.04″ width=”999″ height=”690″ class=”aligncenter size-full wp-image-19840″ srcset=”https://www.ubuntu-server.com/wp-content/uploads/2020/04/nginx-php-fpm-ubuntu1804.png 999w, https://tecadmin.net/wp-content/uploads/2019/11/nginx-php-fpm-ubuntu1804-300×207.png 300w, https://tecadmin.net/wp-content/uploads/2019/11/nginx-php-fpm-ubuntu1804-300×207@2x.png 600w” sizes=”(max-width: 999px) 100vw, 999px”>

Slide down the page and see the value of $_SERVER[‘SERVER_SOFTWARE’]. This will the show the web server details.

<imgsrc=”https://www.ubuntu-server.com/wp-content/uploads/2020/04/nginx-php-fpm-ubuntu1804-2.png” alt=”Nginx PHP Ubuntu 18.04″ width=”999″ height=”690″ class=”aligncenter size-full wp-image-19842″ srcset=”https://www.ubuntu-server.com/wp-content/uploads/2020/04/nginx-php-fpm-ubuntu1804-2.png 999w, https://tecadmin.net/wp-content/uploads/2019/11/nginx-php-fpm-ubuntu1804-2-300×207.png 300w, https://tecadmin.net/wp-content/uploads/2019/11/nginx-php-fpm-ubuntu1804-2-300×207@2x.png 600w” sizes=”(max-width: 999px) 100vw, 999px”>

Conclusion

You have successfully configured Nginx web server with PHP-FPM on your Ubuntu 18.04 (Bionic) LTS. You can now host a website from your server.

The post How to Install Nginx with PHP-FPM on Ubuntu 18.04 LTS appeared first on TecAdmin.

Go to Source
Author: Rahul

Leave a Comment

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