diff --git a/README.md b/README.md index ff357be..93dc0be 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ blank. |----------|---------|--------------| | Wordpress (`WORDPRESS`) | `0` | Install WordPress into an empty `webroot/`. Finish setup at `/wp-admin` | | Composer modules (`COMPOSER_MODULES`) | | Space separated packages, e.g. `phpmailer/phpmailer vlucas/phpdotenv`. Pin a version with `vendor/package:^6.0` | +| Root Folder (`ROOT_FOLDER`) | | Folder in `webroot/` to serve at your domain's root, see [below](#serving-a-folder-at-the-root). Blank serves `webroot/` as is | | User Uploaded Files (`USER_UPLOAD`) | `0` | Skip the Git setup, you upload the files yourself | | Update Check (`UPDATE_CHECK`) | `1` | Say in the console when the egg or the Docker image has an update | | Startup Command (`STARTUP_CMD`) | `./start.sh` | Leave as is unless you know you need to change it | @@ -56,6 +57,37 @@ blank. Each Composer package is added once and then kept at that version. On every start `composer install` keeps `vendor/` in line with `composer.lock`. +### Serving a folder at the root + +Keep your main site in its own folder (any name) and other sites next to it: + +``` +webroot/ +├── / -> example.com/ +├── blog/ -> example.com/blog/ +└── shop/ -> example.com/shop/ +``` + +Set **Root Folder** to `` (the folder's name, e.g. `site`, or a path to a nested +folder, e.g. `dev/main` for `webroot/dev/main`) in the server's **Startup** tab and restart +the server. `webroot/` is then served at `/`, every other +folder in `webroot/` is still served at its own path (`webroot/blog` at `/blog/`), and old +links to `//...` redirect to the same page at `/...`. PHP and the dotfile blocking +work the same in all of them. + +- Each part of the path can use letters, numbers, `.`, `_` and `-`, and starts with a letter + or number (no spaces, no `..`). +- With a nested path like `dev/main`, the other folders in `webroot/dev` are still served at + `/dev//`. +- If the main site and `webroot/` both have a folder with the same name (e.g. + `/shop` and `shop/`), `webroot/shop` wins. +- Files at the top of `webroot/` (not in a folder) aren't served. +- Composer still installs into `webroot/`, so from the main site load it with + `require __DIR__ . '/../vendor/autoload.php';`. +- If the folder doesn't exist the server serves `webroot/` as is and says so in the console. +- Servers installed before the egg had this option need a **Reinstall** first (see + [Updates](#updates)) to get the new `nginx/conf.d/default.conf`. + ### Git | Variable | Default | What it does | diff --git a/egg-nginx-webhost.json b/egg-nginx-webhost.json index b4a02e2..83f1c5f 100644 --- a/egg-nginx-webhost.json +++ b/egg-nginx-webhost.json @@ -24,7 +24,7 @@ }, "scripts": { "installation": { - "script": "#!\/bin\/ash\n# Nginx Webhost installer: installs the nginx\/PHP config and sets up the website files.\n\n# Bump this whenever the egg or the repo's config files change. Servers compare it with\n# the egg on GitHub at startup and show an update notice in the console.\nEGG_VERSION=\"1.0.3\"\n\nREPO=\"https:\/\/github.com\/Rustix-Development\/pterodactyl-nginx-egg\"\n\nis_true() { [ \"$1\" = \"1\" ] || [ \"$1\" = \"true\" ]; }\n\n# Drop user:token@ from a git URL so a changed token still counts as the same repo\nstrip_auth() { printf '%s' \"$1\" | sed -E 's#^([a-z]+:\/\/)[^\/@]*@#\\1#'; }\n\ntrap 'rm -rf \/tmp\/egg' EXIT\n\n# The install container has a terminal nobody types into: never wait for a git password\nexport GIT_TERMINAL_PROMPT=0\n\ncd \/mnt\/server || exit 1\napk add --no-cache git curl || { echo \"Failed to install git and curl\"; exit 1; }\n# Server files belong to the server user, not root; let git work on them anyway\ngit config --global --add safe.directory '*'\n\necho \"Downloading config files from ${REPO}\"\nrm -rf \/tmp\/egg\ngit clone --depth 1 \"${REPO}\" \/tmp\/egg || { echo \"Failed to download config files\"; exit 1; }\nif [ ! -f \/tmp\/egg\/start.sh ] || [ ! -d \/tmp\/egg\/nginx ] || [ ! -d \/tmp\/egg\/php-fpm ]; then\n echo \"Config files are missing from ${REPO} (empty repo or wrong default branch?)\"\n exit 1\nfi\n\n# Config files are replaced on every (re)install\ncp -r \/tmp\/egg\/nginx \/tmp\/egg\/php-fpm \/mnt\/server\/ \\\n && cp \/tmp\/egg\/start.sh \/mnt\/server\/start.sh \\\n && chmod +x \/mnt\/server\/start.sh \\\n || { echo \"Failed to copy config files\"; exit 1; }\n# Left behind by older versions of this egg, it stops nginx from starting\nrm -f \/mnt\/server\/nginx\/conf.d\/nextcloud.conf\nmkdir -p \/mnt\/server\/webroot \/mnt\/server\/tmp \/mnt\/server\/logs\necho \"${EGG_VERSION}\" > \/mnt\/server\/.egg-version\necho \"Installed Nginx Webhost egg v${EGG_VERSION} config files\"\n\ncd \/mnt\/server\/webroot || exit 1\n\n# A default landing page (this egg's or an older egg's) doesn't count as a website\nif [ \"$(ls -A)\" = \"index.php\" ] && grep -qE 'Web server is running<\/h1>|Webserver Correctly Installed' index.php; then\n rm -f index.php\nfi\n\nseed_default_page() {\n if [ -z \"$(ls -A)\" ]; then\n cp \/tmp\/egg\/webroot\/index.php .\/\n fi\n}\n\n# WordPress\nif is_true \"${WORDPRESS}\"; then\n if [ -f wp-load.php ]; then\n echo \"WordPress is already installed, leaving it alone.\"\n elif [ -n \"$(ls -A)\" ]; then\n echo \"webroot already has files, not installing WordPress over them.\"\n else\n echo \"Installing WordPress...\"\n # Unpack outside webroot so a broken download never leaves files behind in it\n WP_TMP=\/mnt\/server\/.wordpress-download\n rm -rf \"${WP_TMP}\" && mkdir -p \"${WP_TMP}\"\n if ! curl -fsSL https:\/\/wordpress.org\/latest.tar.gz | tar xz -C \"${WP_TMP}\" || [ ! -f \"${WP_TMP}\/wordpress\/wp-load.php\" ]; then\n rm -rf \"${WP_TMP}\"\n echo \"Failed to download WordPress\"\n exit 1\n fi\n cp -a \"${WP_TMP}\/wordpress\/.\" . && rm -rf \"${WP_TMP}\"\n echo \"WordPress installed. Finish setup at http:\/\/ip:port\/wp-admin\"\n fi\n exit 0\nfi\n\nif is_true \"${USER_UPLOAD}\"; then\n seed_default_page\n echo \"User Uploaded Files is on, skipping website setup. Upload your files into webroot.\"\n exit 0\nfi\n\nif [ -z \"${GIT_ADDRESS}\" ]; then\n seed_default_page\n echo \"Install complete. No Git Repo Address set, upload your website into webroot.\"\n exit 0\nfi\n\n# Git\ncase \"${GIT_ADDRESS}\" in\n *.git) ;;\n *) GIT_ADDRESS=\"${GIT_ADDRESS}.git\" ;;\nesac\n\nif [ -n \"${USERNAME}\" ] && [ -n \"${ACCESS_TOKEN}\" ]; then\n GIT_ADDRESS=\"https:\/\/${USERNAME}:${ACCESS_TOKEN}@${GIT_ADDRESS#*:\/\/}\"\nfi\n\nif [ -n \"$(ls -A)\" ]; then\n if [ -d .git ]; then\n if [ \"$(strip_auth \"$(git config --get remote.origin.url)\")\" = \"$(strip_auth \"${GIT_ADDRESS}\")\" ]; then\n echo \"Pulling latest website files from git\"\n # Picks up a changed Git Username\/Access Token\n git remote set-url origin \"${GIT_ADDRESS}\"\n git pull --ff-only || echo \"git pull failed, keeping the current files.\"\n else\n echo \"webroot is a clone of a different git repo, leaving it alone.\"\n fi\n else\n echo \"webroot already has files that aren't from git, leaving them alone.\"\n fi\nelse\n echo \"Cloning website files from git\"\n if [ -n \"${BRANCH}\" ]; then\n git clone --single-branch --branch \"${BRANCH}\" \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address, Git Branch and credentials\"; exit 1; }\n else\n git clone \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address and credentials\"; exit 1; }\n fi\nfi\n\necho \"Install complete. Visit http:\/\/ip:port\/\"\n", + "script": "#!\/bin\/ash\n# Nginx Webhost installer: installs the nginx\/PHP config and sets up the website files.\n\n# Bump this whenever the egg or the repo's config files change. Servers compare it with\n# the egg on GitHub at startup and show an update notice in the console.\nEGG_VERSION=\"1.0.4\"\n\nREPO=\"https:\/\/github.com\/Rustix-Development\/pterodactyl-nginx-egg\"\n\nis_true() { [ \"$1\" = \"1\" ] || [ \"$1\" = \"true\" ]; }\n\n# Drop user:token@ from a git URL so a changed token still counts as the same repo\nstrip_auth() { printf '%s' \"$1\" | sed -E 's#^([a-z]+:\/\/)[^\/@]*@#\\1#'; }\n\ntrap 'rm -rf \/tmp\/egg' EXIT\n\n# The install container has a terminal nobody types into: never wait for a git password\nexport GIT_TERMINAL_PROMPT=0\n\ncd \/mnt\/server || exit 1\napk add --no-cache git curl || { echo \"Failed to install git and curl\"; exit 1; }\n# Server files belong to the server user, not root; let git work on them anyway\ngit config --global --add safe.directory '*'\n\necho \"Downloading config files from ${REPO}\"\nrm -rf \/tmp\/egg\ngit clone --depth 1 \"${REPO}\" \/tmp\/egg || { echo \"Failed to download config files\"; exit 1; }\nif [ ! -f \/tmp\/egg\/start.sh ] || [ ! -d \/tmp\/egg\/nginx ] || [ ! -d \/tmp\/egg\/php-fpm ]; then\n echo \"Config files are missing from ${REPO} (empty repo or wrong default branch?)\"\n exit 1\nfi\n\n# Config files are replaced on every (re)install\ncp -r \/tmp\/egg\/nginx \/tmp\/egg\/php-fpm \/mnt\/server\/ \\\n && cp \/tmp\/egg\/start.sh \/mnt\/server\/start.sh \\\n && chmod +x \/mnt\/server\/start.sh \\\n || { echo \"Failed to copy config files\"; exit 1; }\n# Left behind by older versions of this egg, it stops nginx from starting\nrm -f \/mnt\/server\/nginx\/conf.d\/nextcloud.conf\nmkdir -p \/mnt\/server\/webroot \/mnt\/server\/tmp \/mnt\/server\/logs\necho \"${EGG_VERSION}\" > \/mnt\/server\/.egg-version\necho \"Installed Nginx Webhost egg v${EGG_VERSION} config files\"\n\ncd \/mnt\/server\/webroot || exit 1\n\n# A default landing page (this egg's or an older egg's) doesn't count as a website\nif [ \"$(ls -A)\" = \"index.php\" ] && grep -qE 'Web server is running<\/h1>|Webserver Correctly Installed' index.php; then\n rm -f index.php\nfi\n\nseed_default_page() {\n if [ -z \"$(ls -A)\" ]; then\n cp \/tmp\/egg\/webroot\/index.php .\/\n fi\n}\n\n# WordPress\nif is_true \"${WORDPRESS}\"; then\n if [ -f wp-load.php ]; then\n echo \"WordPress is already installed, leaving it alone.\"\n elif [ -n \"$(ls -A)\" ]; then\n echo \"webroot already has files, not installing WordPress over them.\"\n else\n echo \"Installing WordPress...\"\n # Unpack outside webroot so a broken download never leaves files behind in it\n WP_TMP=\/mnt\/server\/.wordpress-download\n rm -rf \"${WP_TMP}\" && mkdir -p \"${WP_TMP}\"\n if ! curl -fsSL https:\/\/wordpress.org\/latest.tar.gz | tar xz -C \"${WP_TMP}\" || [ ! -f \"${WP_TMP}\/wordpress\/wp-load.php\" ]; then\n rm -rf \"${WP_TMP}\"\n echo \"Failed to download WordPress\"\n exit 1\n fi\n cp -a \"${WP_TMP}\/wordpress\/.\" . && rm -rf \"${WP_TMP}\"\n echo \"WordPress installed. Finish setup at http:\/\/ip:port\/wp-admin\"\n fi\n exit 0\nfi\n\nif is_true \"${USER_UPLOAD}\"; then\n seed_default_page\n echo \"User Uploaded Files is on, skipping website setup. Upload your files into webroot.\"\n exit 0\nfi\n\nif [ -z \"${GIT_ADDRESS}\" ]; then\n seed_default_page\n echo \"Install complete. No Git Repo Address set, upload your website into webroot.\"\n exit 0\nfi\n\n# Git\ncase \"${GIT_ADDRESS}\" in\n *.git) ;;\n *) GIT_ADDRESS=\"${GIT_ADDRESS}.git\" ;;\nesac\n\nif [ -n \"${USERNAME}\" ] && [ -n \"${ACCESS_TOKEN}\" ]; then\n GIT_ADDRESS=\"https:\/\/${USERNAME}:${ACCESS_TOKEN}@${GIT_ADDRESS#*:\/\/}\"\nfi\n\nif [ -n \"$(ls -A)\" ]; then\n if [ -d .git ]; then\n if [ \"$(strip_auth \"$(git config --get remote.origin.url)\")\" = \"$(strip_auth \"${GIT_ADDRESS}\")\" ]; then\n echo \"Pulling latest website files from git\"\n # Picks up a changed Git Username\/Access Token\n git remote set-url origin \"${GIT_ADDRESS}\"\n git pull --ff-only || echo \"git pull failed, keeping the current files.\"\n else\n echo \"webroot is a clone of a different git repo, leaving it alone.\"\n fi\n else\n echo \"webroot already has files that aren't from git, leaving them alone.\"\n fi\nelse\n echo \"Cloning website files from git\"\n if [ -n \"${BRANCH}\" ]; then\n git clone --single-branch --branch \"${BRANCH}\" \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address, Git Branch and credentials\"; exit 1; }\n else\n git clone \"${GIT_ADDRESS}\" . || { echo \"git clone failed, check Git Repo Address and credentials\"; exit 1; }\n fi\nfi\n\necho \"Install complete. Visit http:\/\/ip:port\/\"\n", "container": "alpine:3.24", "entrypoint": "ash" } @@ -100,6 +100,16 @@ "rules": "required|boolean", "field_type": "text" }, + { + "name": "Root Folder", + "description": "Folder inside webroot to serve at your domain's root, a name or a path: serves webroot\/ at \/, dev\/main serves webroot\/dev\/main. The other folders in webroot are still served at their own path (webroot\/blog at \/blog\/), and links to \/\/... redirect to \/...\n\nLeave blank to serve webroot as is (default).", + "env_variable": "ROOT_FOLDER", + "default_value": "", + "user_viewable": true, + "user_editable": true, + "rules": "nullable|string|max:255|regex:\/^\\\/?[A-Za-z0-9][A-Za-z0-9._-]*(\\\/[A-Za-z0-9][A-Za-z0-9._-]*)*\\\/?$\/", + "field_type": "text" + }, { "name": "Update Check", "description": "Check GitHub on startup and say in the console when the egg or the Docker image has an update.\n\n0 = false\n1 = true (default)", diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf index efbe685..1f4bf19 100644 --- a/nginx/conf.d/default.conf +++ b/nginx/conf.d/default.conf @@ -6,7 +6,9 @@ server { #access_log /home/container/naccess.log; #error_log /home/container/nerror.log error; - root /home/container/webroot; + # Written by start.sh on every start: webroot, or the Root Folder variable's folder in it + # (the other folders in webroot are still served at //) + include /home/container/tmp/nginx-root.conf; index index.html index.htm index.php; charset utf-8; diff --git a/start.sh b/start.sh index 4408c04..2c32bfa 100755 --- a/start.sh +++ b/start.sh @@ -190,6 +190,58 @@ else exit 1 fi +# nginx's root, from the Root Folder variable (default.conf includes the file written here). +# Blank: webroot. A folder in webroot (a name or a path like dev/main): that folder is the +# site at /, the other folders in webroot are still served at //, and +# //... redirects to /... +write_root_conf() { + conf=/home/container/tmp/nginx-root.conf + folder=$(printf '%s' "${ROOT_FOLDER}" | sed 's#//*#/#g; s#^/##; s#/$##') + + # Every part of the path starts with a letter or digit, so no "..", dotfiles or spaces + if [ -n "${folder}" ] && ! printf '%s' "${folder}" | grep -qE '^[A-Za-z0-9][A-Za-z0-9._-]*(/[A-Za-z0-9][A-Za-z0-9._-]*)*$'; then + log_warning "Root Folder \"${ROOT_FOLDER}\" isn't a folder path in webroot, serving webroot as is." + folder="" + elif [ -n "${folder}" ] && [ ! -d "webroot/${folder}" ]; then + log_warning "Root Folder: webroot/${folder} doesn't exist, serving webroot as is." + folder="" + fi + + if [ -z "${folder}" ]; then + echo "root /home/container/webroot;" > "${conf}" + return + fi + + escaped=$(printf '%s' "${folder}" | sed 's/\./\\./g') + cat > "${conf}" </, everything else comes from webroot/${folder} +set \$site_folder "/${folder}"; +set \$first_folder ""; +if (\$uri ~ "^/([^/]+)") { + set \$first_folder "/home/container/webroot/\$1"; +} +if (-d \$first_folder) { + set \$site_folder ""; +} +root /home/container/webroot\$site_folder; +EOF + log_info "Serving webroot/${folder} at /, the other folders in webroot at //." + if ! grep -qs 'tmp/nginx-root.conf' nginx/conf.d/default.conf; then + log_warning "Root Folder is set, but nginx/conf.d/default.conf is from an older egg and ignores it. Reinstall the server (Settings -> Reinstall Server) to get the new config." + fi +} +write_root_conf + log_info "Starting nginx..." set -- -e stderr -c /home/container/nginx/nginx.conf -p /home/container/ if ! grep -qE '^[[:space:]]*worker_processes' /home/container/nginx/nginx.conf; then