How to setup Nginx with PHP-FPM on Ubuntu 23.04

Step 1: Update and upgrade Ubuntu

First, we need to update and upgrade to the latest available packages

sudo apt update -y
sudo apt upgrade -y

Step 2: Install the required packages

After the system is updated, Install the required packages

sudo apt install -y vim screen git wget curl net-tools

Step 3: Install Nginx Web Server

Then we are going to install nginx web server

sudo apt install nginx -y

Now we can see, nginx service is installed and running properly

Step 4: Install php8.1-fpm

Now we are going to install PHP 8.1 with the following command

sudo apt install php8.1 php8.1-{intl,cli,imagick,mbstring,gd,xml,imap,zip,curl,ldap,mysqli,opcache,fpm} libapache2-mod-php8.1 -y

We can see php8.1-fpm working properly

Step 4: Configure php-fpm to your domain’s virtual host file

Now create a new virtual host file for your domain

vim /etc/nginx/conf.d/

Create virtual host credentials and configure php-fpm in your domain’s virtual host file

server {
  # Example PHP Nginx FPM config file
  listen 80;
  root /var/www/adminhint/html;

  # Add index.php to setup Nginx, PHP & PHP-FPM config
  index index.php index.html index.htm index.nginx-debian.html;

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

  # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # Nginx php-fpm sock config:
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;

  # deny access to Apache .htaccess on Nginx with PHP, 
  # if Apache and Nginx document roots concur
  location ~ /\.ht {
    deny all;
} # End of PHP FPM Nginx config example

Save the file and restart the services

systemctl restart nginx
systemctl restart php8.1-fpm

Step 5: Test with phpinfo in the browser

Create the domain’s root directory

mkdir -p /var/www/adminhint/html

Then create an info.php file for the domain’s document root directory

vim /var/www/adminhint/html/info.php
<?php phpinfo(); ?>

Save the file and run your domain’s URL in browser

Now you can see phpinfo page in the browser same as the following screenshot