Step 1: Updating and Upgrading Ubuntu
To ensure our system has the latest available packages, we begin by updating and upgrading it:
sudo apt update -y
sudo apt upgrade -y
Step 2: Installing Required Packages
After updating the system, we proceed to install the necessary packages:
sudo apt install -y vim screen git wget curl net-tools
Step 3: Installing Nginx Web Server
Next, we install the Nginx web server:
sudo apt install nginx -y
Upon successful installation, Nginx service is up and running.
Step 4: Installing php8.1-fpm
Now, we install PHP 8.1 along with required extensions:
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
After installation, php8.1-fpm is ready for use.
Step 5: Configuring php-fpm for Your Domain’s Virtual Host File
Create a new virtual host file for your domain:
vim /etc/nginx/conf.d/servertutorials.net.conf
Configure php-fpm in the virtual host file with your domain credentials:
server {
# Example PHP Nginx FPM config file
listen 80;
root /var/www/servertutorials/html;
# Add index.php to setup Nginx, PHP & PHP-FPM config
index index.php index.html index.htm index.nginx-debian.html;
server_name server-tutorials.net;
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 6: Testing with phpinfo in the Browser
Create the domain’s root directory:
mkdir -p /var/www/servertutorials/html
Create an info.php file for the domain’s document root directory:
vim /var/www/servertutorials/html/info.php
<?php phpinfo(); ?>
Save the file and navigate to your domain’s URL in the browser:
http://server-tutorials.net