Nginx Configuration Guide


1. Introduction
ShaunSocial is a social networking platform built on Laravel 12, running on PHP-FPM 8.1 or higher.

Nginx is recommended due to its high performance and efficient handling of static assets, especially in containerized or microk8s environments.



2. Recommended Directory Structure
/var/www/shaunsocial/
├── public/ # Document root
├── storage/
├── bootstrap/
├── app/
├── config/
└── …

Notes:

  • The public/ directory is the web root.
  • The storage/ and bootstrap/cache/ directories must be writable by the PHP-FPM user (e.g., www-data).



3. Basic Nginx Configuration
Create a file: /etc/nginx/sites-available/shaunsocial.conf



Example Configuration:
server {
listen 80;
server_name shaunsocial.local; # Replace with your actual domain

root /var/www/shaunsocial/public;
index index.php index.html;

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

# Handle normal requests
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# PHP-FPM handling
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;  # Adjust PHP version if needed
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
}

# Basic security
location ~ /\. {
    deny all;
}

}


Enable and reload the site:

sudo ln -s /etc/nginx/sites-available/shaunsocial.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx



4. HTTPS Configuration
Example Configuration:

server {
listen 443 ssl http2;
server_name shaunsocial.com;

ssl_certificate     /etc/letsencrypt/live/shaunsocial.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/shaunsocial.com/privkey.pem;
include snippets/ssl-params.conf;

root /var/www/shaunsocial/public;
index index.php index.html;

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

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~ /\. {
    deny all;
}

}



5. Permissions and Ownership
sudo chown -R www-data:www-data /var/www/shaunsocial
sudo chmod -R 755 /var/www/shaunsocial
sudo chmod -R 775 /var/www/shaunsocial/storage /var/www/shaunsocial/bootstrap/cache

Powered by BetterDocs