Skip to content

Instantly share code, notes, and snippets.

@amanjuman
Last active March 26, 2024 21:41
Show Gist options
  • Save amanjuman/7144a14a2600d8c436b1773ca2df36d5 to your computer and use it in GitHub Desktop.
Save amanjuman/7144a14a2600d8c436b1773ca2df36d5 to your computer and use it in GitHub Desktop.
Pihole Nginx Config
curl -sSL https://install.pi-hole.net | bash
sudo apt-get -y install software-properties-common python3-certbot-nginx nginx php7.2-fpm php7.2-cgi php7.2-xml php7.2-sqlite3 php7.2-intl apache2-utils
sudo certbot --nginx --agree-tos --register-unsafely-without-email --no-redirect -d subdomain.example.com
htpasswd -c /etc/nginx/.htpasswd user
chown -R www-data:www-data /var/www/html
usermod -aG pihole www-data
chmod -R 755 /var/www/html
server
{
# Listen Ports
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
# Site Location
root /var/www/html/admin;
server_name subdomain.example.com;
autoindex off;
# Index Files
index pihole/index.php index.php index.html index.htm;
# SSL
ssl_certificate /etc/letsencrypt/live/subdomain.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/subdomain.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/subdomain.example.com/fullchain.pem;
# Logging
access_log /var/log/nginx/subdomain.example.com-access.log;
error_log /var/log/nginx/subdomain.example.com-error.log;
error_page 404 /pihole/index.php;
# HTTP to HTTPS redirection
if ($scheme != "https")
{
return 301 https://$host$request_uri;
}
# Permalink
location /
{
expires max;
try_files $uri $uri/ =404;
}
# PHP Upstream
location ~ \.php$
{
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param FQDN true;
auth_basic "Restricted"; # For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd; # For Basic Auth
}
# Static File Location
location /*.js
{
index pihole/index.js;
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd; #For Basic Auth
}
# Administrator Access
location /admin
{
root /var/www/html;
index index.php index.html index.htm;
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd; #For Basic Auth
}
# Hidden File Protection
location ~ /\.ht
{
deny all;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment