Set up a NGINX server for Debian 12
After installing the NGINX server, NGINX is directed to the folder
/usr/share/nginx/html
Let's change this path.
I want my site example.com to be in a folder var/www/
Сreate a example.com folder
mkdir /var/www/example.com
Set folder permissions 755
chmod 755 /var/www/example.com
755 ( drwxr-xr-x ) everyone can read this directory, but only the owner can change its contents
Create a new file index.php in the folder example.com
touch /var/www/example.com/index.php
Set file index.php permissions 644
chmod 644 /var/www/example.com/index.php
644 (-rw-r--r--) only the owner has read and write permission; group and others have read-only access
Set the owner www-data and group www-data for the example.com folder and also for all folders and files inside the example.com folder
chown -R www-data:www-data /var/www/example.com
Notice: User and group settings are in the file
/etc/php/8.3/fpm/pool.d/www.conf
Open the file index.php and write in it
<?php
echo "Hello world!";
?>
Now open the file default.conf
nano /etc/nginx/conf.d/default.conf
And write to it
server {
listen 80 default_server;
server_name localhost;
root /var/www/;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /example.com/ {
try_files $uri /example.com/index.php?$args;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $uri =404;
include fastcgi_params;
}
}
Now open the file nginx.conf
nano /etc/nginx/nginx.conf
And in the first line replace
user nginx;
replace with
user www-data;
Restart the server
service nginx restart
Opening the site in a browser
http://localhost/example.com/
If you saw the text
Hello world
NGINX server setup done correctly
If an error occurs, you can find the cause of the error in the file error.log
/var/log/nginx/error.log
My simple config file nginx.conf looks like this
user www-data;
worker_processes auto;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log notice;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}