Last active
August 9, 2025 21:35
-
-
Save SurajDadral/22e66533d006f11859b49f8d6445093c to your computer and use it in GitHub Desktop.
Enable PHP and CGI in user directory (public_html) in nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Default server configuration | |
# | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
root /var/www/html; | |
# Add index.php to the list if you are using PHP | |
index index.php index.html index.htm index.nginx-debian.html; | |
server_name example.com; | |
# Don't allow access to dotfiles (such as .htaccess) | |
location ~ /\. { | |
deny all; | |
} | |
location / { | |
# First attempt to serve request as file, then | |
# as directory, then fall back to displaying a 404. | |
try_files $uri $uri/ =404; | |
} | |
# Serve user directories php files | |
# This MUST appear above both the normal userdir and php location blocks | |
# else it will either attempt to load from your main (non-user) site | |
# or ask vistors to download PHP files (which can be bad for security) | |
location ~ ^/~(.+?)/(.*\.php)$ { | |
alias /home/$1/public_html; | |
autoindex on; | |
# default fastcgi settings | |
include fastcgi_params; | |
# A bit of added security -- not full proof, but can help | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini | |
# Check that the PHP script exists before passing it | |
if (!-f $document_root/$2) {return 404;} | |
fastcgi_param SCRIPT_FILENAME $document_root/$2; | |
fastcgi_index index.php; | |
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; | |
} | |
# Enable CGI | |
include userdir-fcgiwrap.conf; | |
# userdir block | |
# Enable files listing in public_html of user home dir | |
location ~ ^/~(.+?)(/.*)?$ { | |
alias /home/$1/public_html$2; | |
index index.php index.html index.htm; | |
autoindex on; | |
} | |
# default php block | |
# Enable php in server root | |
location ~* \.php$ { | |
include snippets/fastcgi-php.conf; | |
# default fastcgi settings | |
include fastcgi_params; | |
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Include this file on your nginx.conf to support debian cgi-bin scripts using | |
# fcgiwrap in user directory: /home/<user>/public_html/cgi-bin/ | |
# Place this file in /etc/nginx/ | |
# And add "include userdir-fcgiwrap.conf;" to site default configuration | |
# Use this location to enable execution of all script files present in /home/<user>/public_html/cgi-bin/ | |
# location ~ ^/~(.+?)(/cgi-bin/)(.*\..*)$ { | |
# Enable execution of files with specific extension through cgi | |
# location ~ ^/~(.+?)(/cgi-bin/)(.*)?\.(py|cgi)$ { | |
location ~ ^/~(.+?)(/cgi-bin/)(.*)?\.(cgi)$ { | |
alias /home/$1/public_html$2$3.$4; | |
# Disable gzip (it makes scripts feel slower since they have to complete | |
# before getting gzipped) | |
gzip off; | |
# Check that the PHP script exists before passing it | |
if (!-f /home/$1/public_html$2$3.$4) {return 404;} | |
# Fastcgi parameters, include the standard ones | |
include /etc/nginx/fastcgi_params; | |
# Adjust non standard parameters (SCRIPT_FILENAME) | |
# fastcgi_param DOCUMENT_ROOT /home/$1/public_html/cgi-bin; | |
fastcgi_param SCRIPT_FILENAME $document_root; | |
# Fastcgi socket | |
fastcgi_pass unix:/var/run/fcgiwrap.socket; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment