Install Nginx

Single Line
sudo apt update ; sudo apt install nginx ; sudo ufw enable ; sudo ufw allow OpenSSH ; sudo ufw allow 'Nginx HTTP' ; sudo ufw allow 'Nginx HTTPS'

sudo ufw app list
sudo ufw status

Nginx Optimized Config file ( /etc/nginx/nginx.conf )

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
#include /etc/nginx/conf.d/*;
events {
	worker_connections 768;
	# multi_accept on;
}

http {
#	access_by_lua_file anti_ddos_challenge.lua;
	#client_max_body_size = 250M;
	##
	# Basic Settings
	##
	client_max_body_size 250M;
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	 gzip_vary on;
	 gzip_proxied any;
	 gzip_comp_level 6;
	 gzip_min_length    256;
	 gzip_buffers 16 8k;
	 gzip_http_version 1.1;
	 gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/svg image/jpg image/png;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

#mail {
#	# See sample authentication script at:
#	# <http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript>
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

Default Nginx File

server {
    listen 80;
    listen [::]:80;

    root /var/www/html/first-project/public;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }

    location ~ /\\.ht {
            deny all;
    }
}

Nginx File After SSL

server {
    listen 80;
    access_log off;
    root /var/www/html/example.com/public;
    index index.php index.html index.htm index.nginx-debian.html;
    client_max_body_size 1000M;
    fastcgi_read_timeout 8600;
    proxy_cache_valid 200 365d;
    location ~ \\.(env|log|htaccess)$ {
        deny all;
    }
    location ~*\\.(?:js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg|mp3|jpe?g,eot|ttf|svg)$ {
        access_log off;
        expires 360d;
        add_header Access-Control-Allow-Origin *;
	add_header Pragma public;
        add_header Cache-Control "public";
        add_header Vary Accept-Encoding; 
        try_files $uri $uri/ /index.php?$query_string;
    }
    location / {
	add_header Access-Control-Allow-Origin *;
        if ($host ~* ^(www)) {
          rewrite ^/(.*)$ <https://example.com/$1> permanent;
        }
				if ($scheme = http) {
				        return 301 https://example.com$request_uri;
				}
        try_files $uri $uri/ /index.php?$query_string;
	access_log off;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
   listen 443 ssl; # managed by Certbot
   server_name example.com www.example.com;
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Install New Site NGINX one line


Install Cerbot Single Line
sudo snap install core; sudo snap refresh core ; sudo snap install --classic certbot ; sudo ln -s /snap/bin/certbot /usr/bin/certbot
#sudo certbot --nginx
#sudo certbot certonly --nginx

mkdir /var/www/html/example.com ; mkdir /var/www/html/example.com/public ; echo "<?php php_info() ?>" ; rm -rf /etc/nginx/sites-available/example.com ; rm -rf  /etc/nginx/sites-enabled/example.com ; echo "server {
    listen 80;

    root /var/www/html/example.com/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name www.example.com example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }

    location ~ /\\.ht {
            deny all;
    }
}" > /etc/nginx/sites-available/example.com ; ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/  ; certbot certonly --noninteractive --agree-tos --cert-name example.com -d example.com -d www.example.com --register-unsafely-without-email --webroot -w /var/www/html/example.com  ; rm -rf /etc/nginx/nginx.conf ;  echo "user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
#include /etc/nginx/conf.d/*;
events {
    worker_connections 768;
    # multi_accept on;
}
http {
#   access_by_lua_file anti_ddos_challenge.lua;
    #client_max_body_size = 250M;
    ##
    # Basic Settings
    ##
    client_max_body_size 250M;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

     gzip_vary on;
     gzip_proxied any;
     gzip_comp_level 6;
     gzip_min_length    256;
     gzip_buffers 16 8k;
     gzip_http_version 1.1;
     gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/svg image/jpg image/png;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # <http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript>
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities 'TOP' 'USER';
#   # imap_capabilities 'IMAP4rev1' 'UIDPLUS';
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}" > /etc/nginx/nginx.conf  ; rm -rf /etc/nginx/sites-available/example.com ;
rm -rf /etc/nginx/sites-enabled/example.com  ; echo 'server {
    listen 80;
    listen 443 ssl; # managed by Certbot
    server_name example.com www.example.com;
    access_log off;
    root /var/www/html/example.com/public;
    index index.php index.html index.htm index.nginx-debian.html;
    client_max_body_size 1000M;
    fastcgi_read_timeout 8600;
    proxy_cache_valid 200 365d;
    location ~ \\.(env|log|htaccess)$ {
        deny all;
    }
    location ~*\\.(?:js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg|mp3|jpe?g,eot|ttf|svg)$ {
        access_log off;
        expires 360d;
        add_header Access-Control-Allow-Origin *;
    add_header Pragma public;
        add_header Cache-Control "public";
        add_header Vary Accept-Encoding; 
        try_files $uri $uri/ /index.php?$query_string;
    }
    location / {
    add_header Access-Control-Allow-Origin *;
        if ($host ~* ^(www)) {
          rewrite ^/(.*)$ <https://example.com/$1> permanent;
        }
        if ($scheme != "https") {
            rewrite ^ <https://$host$uri> permanent;
        }
        try_files $uri $uri/ /index.php?$query_string;
    access_log off;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    } 
   ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}' > /etc/nginx/sites-available/example.com ; rm -rf /etc/nginx/sites-enabled/example.com ; ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ ; service nginx restart