Install PHP for Nginx with PHP-FPM:
sudo apt update
sudo apt install --no-install-recommends php8.1 [Apache HTTP server and its httpd process]
sudo apt-get install php8.1-fpm -y [For PHP-FPM works with nginx]
php -v
sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-imap
php -m
Check if PHP-FPM is running:
sudo systemctl status php8.1-fpm
Add PHP support to Nginx:
sudo nano /etc/nginx/sites-available/default
Enable PHP in Nginx’s config file:
server {
# Example PHP Nginx FPM config file
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to setup Nginx, PHP & PHP-FPM config
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
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;
# Nginx php-cgi config :
# Nginx PHP fastcgi_pass 127.0.0.1:9000;
}
# 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
sudo nginx -t
sudo systemctl restart nginx