Skip to content

Instantly share code, notes, and snippets.

@ankurk91
Last active June 4, 2025 04:57
Ngnix conf with php/node on Ubuntu

Install on Ubuntu 22/24

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 add-apt-repository ppa:ondrej/nginx -y
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.4-fpm
sudo systemctl start php8.4-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.4/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 example.com;
root /home/user-name/projects/laravel-app/public;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
access_log /var/log/nginx/example.com_access.log combined buffer=512k flush=1m;
error_log /var/log/nginx/example.com_error.log warn;
}
# /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
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;
##
# Gzip Settings
##
gzip on;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# /etc/nginx/sites-enabled/node-js.conf
server {
server_name example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
# Proxy headers
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Proxy timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
access_log /var/log/nginx/example.com_access.log combined buffer=512k flush=1m;
error_log /var/log/nginx/example.com_error.log warn;
location ~ /\.(?!well-known).* {
deny all;
}
}
@ankurk91
Copy link
Author

Reserved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment