NginxInstaller/app.js
2023-12-19 09:47:24 -06:00

83 lines
3.9 KiB
JavaScript

document.getElementById("submit").addEventListener("click", generateQuickCmd, false);
const service = "[Unit]\n" +
"Description=The NGINX HTTP and reverse proxy server\n" +
"After=syslog.target network-online.target remote-fs.target nss-lookup.target\n" +
"Wants=network-online.target\n" +
"\n" +
"[Service]\n" +
"Type=forking\n" +
"PIDFile=/var/run/nginx.pid\n" +
"ExecStartPre=/usr/sbin/nginx -t\n" +
"ExecStart=/usr/sbin/nginx\n" +
"ExecReload=/usr/sbin/nginx -s reload\n" +
"ExecStop=/bin/kill -s QUIT $MAINPID\n" +
"PrivateTmp=true\n" +
"\n" +
"[Install]\n" +
"WantedBy=multi-user.target";
function generateQuickCmd() {
fetch("https://paste.nevets.tech/api/", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: "{\"format\":\"url\",\"content\":\"" + encodeURIComponent(buildCmd()) + "\",\"expires\":3600,\"lexer\":\"bash\"}",
mode: "cors"
}).then(res => res.json().then(json => {
document.getElementById("script").innerText = "curl -s " + json.url + "/raw | bash";
}));
}
function buildCmd() {
let nginxStr = document.getElementById("link").value; //https://nginx.org/download/nginx-x.xx.x.tar.gz
let script = "#!/bin/bash\n" +
"if [ \"$EUID\" -ne 0 ]\n" +
" then echo \"Please run as root (or with sudo)\"\n" +
" exit\n" +
"fi\n\n" +
"apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev\n" +
"wget " + document.getElementById("link").value + "\n" +
"tar -xzvf " + extractNginxVersion(nginxStr) + "\n";
if (document.getElementById("extMod").checked === true) {
script += "git clone https://github.com/google/ngx_brotli.git\n" +
"git clone https://github.com/openresty/headers-more-nginx-module.git\n" +
"cd ngx_brotli/ && git submodule update --init && cd ../" + extractNginxVersion(nginxStr).replace(/\.tar\.gz$/, '') + "/\n";
} else {
script += "cd " + extractNginxVersion(nginxStr).replace(/\.tar\.gz$/, '') + "\n";
}
script += "cp -r conf /etc/nginx\n" +
"mkdir /var/cache/nginx && sudo chown www-data:www-data /var/cache/nginx" +
getConfigureStr() + "\n" +
"make\n" +
"make install\n" +
"echo \"" + service + "\" >> /lib/systemd/system/nginx.service\n" +
"systemctl enable nginx.service\n" +
"systemctl start nginx.service";
return script;
}
function getConfigureStr() {
let str = "./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/etc/nginx/logs/error.log --http-log-path=/etc/nginx/logs/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=www-data --group=www-data ";
if (document.getElementById("extMod").checked === true) {
str += "--add-module=../headers-more-nginx-module --add-module=../ngx_brotli ";
}
if (document.getElementById("http3").checked === true) {
str += "--with-http_v3_module ";
}
str += "--with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_gunzip_module --with-http_gzip_static_module ";
return str;
}
function extractNginxVersion(url) {
// This regex matches 'nginx' followed by a hyphen and a version number, followed by '.tar.gz'
const regex = /nginx-\d+\.\d+\.\d+\.tar\.gz/;
// Use the regex to search the string
const match = url.match(regex);
// If a match is found, return it. Otherwise, return null or an appropriate message
return match ? match[0] : null;
}