Skip to content

Instantly share code, notes, and snippets.

@ankurk91
Last active March 15, 2024 05:20
Show Gist options
  • Save ankurk91/d6bb80692bb72c169f15c08985df85f6 to your computer and use it in GitHub Desktop.
Save ankurk91/d6bb80692bb72c169f15c08985df85f6 to your computer and use it in GitHub Desktop.
Ngnix conf with php on Ubuntu 20/22

Install on Ubuntu 20/22

Cleanups

⚠️ If you have already installed apache, you must uninstall it

sudo systemctl stop apache2.service
sudo apt remove apache2
sudo apt remove "libapache2-mod-php*"

Install Nginx

sudo apt update
sudo apt install nginx -y
sudo systemctl enable nginx

Configure Nginx for PHP

Hope you have already installed the php-cli, lets install the glue now

sudo apt install php8.3-fpm
sudo systemctl start php8.3-fpm

Ngnix service commands

  • Check for syntax errors
sudo nginx -t
  • Reload after updating the configurations
sudo systemctl reload nginx

Tweak php.ini

You can tweak php.ini at this location

/etc/php/8.3/fpm/php.ini

Links

# /etc/nginx/sites-enabled/laravel.conf
server {
listen 80;
# listen 443 ssl;
# ssl_certificate /etc/nginx/certs/laravel.test.pem;
# ssl_certificate_key /etc/nginx/certs/laravel.test-key.pem;
server_name laravel.test *.laravel.test;
root /home/ankur/projects/laravel-app/public;
add_header X-Frame-Options "SAMEORIGIN";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 100M;
# 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 TLSv1.3; # 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_static on;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment