nginx/nginx.sh

117 lines
2.7 KiB
Bash
Raw Normal View History

2024-06-14 07:14:42 +00:00
#!/usr/bin/env bash
2024-12-02 08:41:11 +00:00
unset backup_port addurl adddomains
2025-01-09 22:45:52 +00:00
set -e
2024-12-02 08:41:11 +00:00
clear
2024-06-14 07:14:42 +00:00
# Fill in the following variables
2025-01-09 22:45:52 +00:00
eff_email_address= # eg, eff@web.com
appname= # eg, nextcloud
subdomain= # eg, cloud
domain= # eg, web.com
adddomains=() # eg, web2.com web3.comf
host= # eg, 127.0.0.1
port= # eg, 65000
backup_port= # eg, 65000 (defaults to localhost 127.0.0.1 as host)
# Grab URL
2024-12-02 08:41:11 +00:00
if [ ${subdomain} ]
2024-06-14 07:14:42 +00:00
then
2024-12-02 08:41:11 +00:00
url=${subdomain}.${domain}
else
url=${domain}
fi
2025-01-09 22:45:52 +00:00
# Grab URLs for extra domains
for add in ${adddomains[@]}
2024-12-02 08:41:11 +00:00
do
2025-01-09 22:45:52 +00:00
addurl+=(${subdomain}.${add})
2024-12-02 08:41:11 +00:00
done
2025-01-09 22:45:52 +00:00
# Figure out nginx conf directory
if grep -q 'include.*conf.d' /etc/nginx/nginx.conf
then
2025-03-13 08:50:08 +00:00
nginxdir=conf.d
elif grep -q 'include.*sites-enabled' /etc/nginx/nginx.conf
2025-01-09 22:45:52 +00:00
then
2025-03-13 08:50:08 +00:00
nginxdir=sites-available
2025-01-09 22:45:52 +00:00
sudo ln -s -f /etc/nginx/sites-available/${appname}.conf /etc/nginx/sites-enabled/
else
echo "Missing nginx directory, exiting..."
exit 1
fi
2024-12-02 08:41:11 +00:00
2025-01-09 22:45:52 +00:00
# Add backup directive to nginx.conf if supplied
2024-12-02 08:41:11 +00:00
if [ ${backup_port} ]
then
2025-01-09 22:45:52 +00:00
sudo tee /etc/nginx/${nginxdir}/${appname}.conf >/dev/null <<- conf
upstream ${appname} {
server ${host}:${port};
2024-12-02 08:41:11 +00:00
2025-01-09 22:45:52 +00:00
server 127.0.0.1:${backup_port} backup;
}
2024-12-02 08:41:11 +00:00
2025-01-09 22:45:52 +00:00
conf
2024-12-02 08:41:11 +00:00
else
2025-01-09 22:45:52 +00:00
sudo tee /etc/nginx/${nginxdir}/${appname}.conf >/dev/null <<- conf
upstream ${appname} {
2025-03-13 09:06:15 +00:00
server ${host}:${port};
2025-01-09 22:45:52 +00:00
}
2024-12-02 08:41:11 +00:00
2025-01-09 22:45:52 +00:00
conf
2024-06-14 07:14:42 +00:00
fi
2025-01-09 22:45:52 +00:00
# Nginx
sudo tee -a /etc/nginx/${nginxdir}/${appname}.conf >/dev/null <<- conf
2024-06-14 07:14:42 +00:00
server {
server_name ${url};
2025-01-09 22:45:52 +00:00
include http_upgrade;
2024-06-14 07:14:42 +00:00
location / {
2024-12-02 08:41:11 +00:00
proxy_pass http://${appname};
2025-01-09 22:45:52 +00:00
include proxy_params;
2024-06-14 07:14:42 +00:00
error_log /var/log/nginx/${appname}_error.log;
access_log /var/log/nginx/${appname}_access.log;
}
}
2024-12-02 08:41:11 +00:00
2024-06-14 07:14:42 +00:00
conf
2025-01-09 22:45:52 +00:00
# Certbot
sudo certbot --nginx --non-interactive --agree-tos --no-eff-email --staple-ocsp --hsts --no-redirect \
--email ${eff_email_address} \
--domain ${url}
# Add extra domains
2024-12-02 08:41:11 +00:00
for url in ${addurl[@]}
do
2025-01-09 22:45:52 +00:00
sudo tee -a /etc/nginx/${nginxdir}/${appname}.conf >/dev/null <<- conf
server {
server_name ${url};
include http_upgrade;
location / {
proxy_pass http://${appname};
include proxy_params;
error_log /var/log/nginx/${appname}_error.log;
access_log /var/log/nginx/${appname}_access.log;
}
}
conf
# Certbot
sudo certbot --nginx --non-interactive --agree-tos --no-eff-email --staple-ocsp --hsts --no-redirect \
--email ${eff_email_address} \
--domain ${url}
2024-12-02 08:41:11 +00:00
done
2024-06-14 07:14:42 +00:00
2025-01-09 22:45:52 +00:00
# Add http2 and http3 directives
2024-12-02 08:41:11 +00:00
sudo sed -e '/listen 80/d' \
-e '/listen 443/a\
listen 443 quic;\
listen [::]:443 ssl;\
listen [::]:443 quic;\
2025-01-09 22:45:52 +00:00
' -i /etc/nginx/${nginxdir}/${appname}.conf
sudo systemctl reload nginx.service