Find Us on Socials

Subscribe Now

How to setup Nginx with PHP-FPM on Ubuntu 23.04

It’s easy to set up Nginx with PHP-FPM on Ubuntu 23.04, which improves the speed of your web server. Using the package manager, install Nginx and PHP-FPM first: sudo apt update && sudo apt install nginx php-fpm. After installation, update the server block in /etc/nginx/sites-available/default and redirect the fastcgi_pass directive to the PHP-FPM socket, usually /run/php/php8.1-fpm.sock, to configure Nginx to use PHP. Once the changes have been saved, the settings should be confirmed by using sudo nginx -t. Afterward, Nginx should be reloaded by using sudo systemctl reload nginx.


Your server can now efficiently serve PHP apps.

Step 1: Update and upgrade Ubuntu: PHP-FPM on 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 updates, you should 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, the system has installed and is running the nginx service 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/adminhint.com.conf

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;

  server_name adminhint.com;
  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

http://aadminhint.com

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

Setting up Nginx with PHP-FPM on Ubuntu 23.04 is a straightforward process that enhances your web server’s performance and efficiency. By following the outlined steps, it is ensured that your server is optimized for handling PHP requests, resulting in faster load times and improved scalability. This setup is ideal for hosting dynamic websites and applications, providing a robust and secure environment. Regular maintenance and updates will keep your server running smoothly. With Nginx and PHP-FPM configured, the web content can be effectively managed and served on Ubuntu 23.04.