How to configure PHP-FPM with Nginx in Debian
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP intro whatis.
Install Nginx and PHP-FPM:
sudo apt update && sudo apt install nginx php-fpm -y
Find PHP socket file:
user@localhost:~$ file /var/run/php/php8.2-fpm.sock
/var/run/php/php8.2-fpm.sock: socket
Create directory for project:
sudo mkdir -p /var/www/your-project
Create "index.php" file in /var/www/your-project directory:
sudo nano /var/www/your-project/index.html
Now add PHP code in "index.php":
<?php
$command = "echo \"CPU `LC_ALL=C top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\([0-9.]*\)%* id.*/\1/\" | awk>
//echo shell_exec($command);
$output = shell_exec($command);
echo $output;
?>
This code get information about CPU, RAM and disk storage.
Change owner and group of "your-project" directory to "www-data":
sudo chown -R www-data:www-data /var/www/your-project/
Configuring Nginx. Create file /etc/nginx/sites-available/your-project file and insert this configs:
server {
server_name your-project.com;
listen 80;
root /var/www/your-project;
index index.php;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
}
Close file and save changes.
Create symlink for configuration file:
sudo ln -s /etc/nginx/sites-available/your-project /etc/nginx/sites-enabled
Test new configurations:
sudo nginx -t
Output must be like this:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx for apply new configurations:
sudo systemctl restart nginx
Allow UFW to accep HTTP requests:
sudo ufw allow 'Nginx Full'
Testing. Enter in browser address of your project: http://your-project.com/