From 5a20bfaed24023d335350405d8b083a8bc0c615e Mon Sep 17 00:00:00 2001 From: Goostaf Date: Wed, 14 Aug 2024 18:12:09 +0200 Subject: [PATCH] Use internal Apache rewrites (#5) * Rewrite internally in Apache * Try to fix infinite redirects * Use more generic file name * More documentation * Cleanup, less strict env variables * Depend on database * Make README match configuration options --- README.md | 20 ++++++++++++++------ docker-compose.yaml | 2 ++ mediawiki/Dockerfile | 5 ++++- mediawiki/LocalSettings.php | 13 ++++++------- mediawiki/apache-custom-directory.conf | 20 ++++++++++++++++++++ proxy/nginx.conf.template | 4 +--- 6 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 mediawiki/apache-custom-directory.conf diff --git a/README.md b/README.md index f705625..8b49103 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,16 @@ Includes a proxy that helps routing to sub-paths. # Usage with Gamma -In order to authorize with Gamma, create a client with a redirect URL that matches `https://[URL of wiki]/wiki/index.php/Special:PluggableAuthLogin`. +In order to authorize with Gamma, create a client with a redirect URL that matches `https://[URL of wiki]/[wiki path]/Special:PluggableAuthLogin`. Also make sure that the email scope is enabled. If you want to make any restrictions on who can access the wiki, you can set those directly on the client. The wiki will not do any additional checks other than login status. # Wiki Setup -After creating a blank software stack (e.g. through Docker compose), you should be able to access the wiki through the proxy at http://localhost:4000/wiki, or directly at http://localhost:4001. -The wiki can now be initialized by navigating to http://localhost:4000/wiki/mw-config and following the instructions. +After creating a blank software stack (e.g. through Docker compose), you should be able to access the wiki through the proxy at `http://localhost:4000/[wiki path]`, or directly at `http://localhost:4001/[wiki path]`. +Note that doing the latter requires setting `ROOT_URL` to match the non-proxied version, or else all further links will incorrectly lead to the proxied version. +The wiki can now be initialized by navigating to `http://localhost:4000/[wiki path]/mw-config` and following the instructions. Should you encounter a 404 error when clicking continue, navigate directly to the wiki setup page at http://localhost:4001/mw-config. If there are any database-related errors after initialization, run the upgrade script: `php maintenance/update.php`. @@ -23,12 +24,14 @@ More information about this update script can be found in MediaWiki's [documenta > [!WARNING] > Make sure that there exists a backup of the database before running the upgrade script. -## Upgrating from previous versions +## Upgrading from Previous Versions If you are upgrading from version < 1.39, you will need to run the upgrade script to create new database tables used by new extensions: `php maintenance/update.php`. ## Configuration +### Environment Variables + The following environment variables can be set to configure the wiki: | Variable | Description | Default | Example | @@ -37,13 +40,13 @@ The following environment variables can be set to configure the wiki: | `CLIENT_ID` | The client ID of the provider | (Required) | `123456` | | `CLIENT_SECRET` | The client secret of the provider | (Required) | `abcdef` | | `WIKI_NAME` | The name of the wiki | (Required) | `My Wiki` | -| `WIKI_PATH` | The root path of the wiki | (Required) | `/wiki` | +| `WIKI_PATH` | The root path of the wiki | `""` | `/wiki` | | `ROOT_URL` | The root URL of the wiki | (Required) | `https://wiki.chalmers.it` | | `DB_SERVER` | URL to the database | (Required) | `db` | | `DB_NAME` | The database name to use | (Required) | `wiki` | | `DB_USER` | Username to access database | (Required) | `wiki` | | `DB_PASSWORD` | Password to access database | (Required) | `password` | -| `DB_PREFIX` | Prefix to be used when addressing database. | (Required) | `mw_` | +| `DB_PREFIX` | Prefix to be used when addressing database. | `mw_` | `wiki_` | | `SECRET_KEY` | The secret key for the wiki | (Required) | `secret` | | `UPGRADE_KEY` | The upgrade key for the wiki. Used for installs. | (Required) | `123456` | | `ENABLE_LOCAL_LOGIN` | Enable local login for the wiki. | `false` | `false` | @@ -52,3 +55,8 @@ The following environment variables can be set to configure the wiki: | `WORDMARK_WIDTH` | Width of the wordmark (max 124) | `124` | `64` | | `WORDMARK_HEIGHT` | Height of the wordmark (max 32) | `32` | `32` | | `DEFAULT_SKIN` | Default skin to use. Is also displayed for non-logged in users | `citizen` | `vector-2022` | + +### Choosing a Path + +If you want to host the wiki under a subdirectory, setting the `WIKI_PATH` environment variable to the desired path will make the wiki accessible at `http://localhost:4000/[wiki path]`. +If you want to host the wiki at the root, set `WIKI_PATH` to an empty string. diff --git a/docker-compose.yaml b/docker-compose.yaml index 0e31c13..48b1a57 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -18,6 +18,8 @@ services: SECRET_KEY: dasdasdasd UPGRADE_KEY: dasd ROOT_URL: http://localhost:4000 + depends_on: + - wiki-db ports: - 4001:80 volumes: diff --git a/mediawiki/Dockerfile b/mediawiki/Dockerfile index 1fe1509..d864db1 100644 --- a/mediawiki/Dockerfile +++ b/mediawiki/Dockerfile @@ -31,7 +31,7 @@ RUN git checkout 4ff9d24 # Install ParserFunctions for MediaWiki 1.39 WORKDIR /tmp -RUN curl -L https://extdist.wmflabs.org/dist/extensions/ParserFunctions-REL1_39-7fa883d.tar.gz -o ParserFunctions.tar.gz +RUN curl -L https://extdist.wmflabs.org/dist/extensions/ParserFunctions-REL1_39-1ce7bc0.tar.gz -o ParserFunctions.tar.gz RUN tar -xzf ParserFunctions.tar.gz -C /var/www/html/extensions WORKDIR /var/www/html @@ -41,5 +41,8 @@ RUN git clone https://github.com/StarCitizenTools/mediawiki-skins-Citizen Citize WORKDIR /var/www/html/skins/Citizen RUN git checkout 9caf91a +# See https://www.mediawiki.org/wiki/Topic:Weqepsq3e8po62y1 +COPY ./apache-custom-directory.conf /etc/apache2/sites-available/000-default.conf + COPY ./LocalSettings.php /var/www/html/ WORKDIR /var/www/html diff --git a/mediawiki/LocalSettings.php b/mediawiki/LocalSettings.php index cfd43f9..b9b7a68 100644 --- a/mediawiki/LocalSettings.php +++ b/mediawiki/LocalSettings.php @@ -27,17 +27,16 @@ ## For more information on customizing the URLs ## (like /w/index.php/Page_title to /wiki/Page_title) please see: ## https://www.mediawiki.org/wiki/Manual:Short_URL -$wgScriptPath = $_ENV["WIKI_PATH"]; +$wgScriptPath = $_ENV["WIKI_PATH"] ?? ""; ## The protocol and server name to use in fully-qualified URLs $wgServer = $_ENV["ROOT_URL"]; -## The URL path to static resources (images, scripts, etc.) -$wgResourceBasePath = $wgScriptPath; +# The base URL that is used to construct all internal links +# Normally points to the main index.php script. Can be relative or absolute +$wgArticlePath = "$wgScriptPath/$1"; -$wgArticlePath = "/wiki/$1"; - -## The URL paths to the logo. Make sure you change this from the default, +## The URL paths to the logo. Make sure you change this from the default, ## or else you'll overwrite your logo when you upgrade! $wgLogos = [ 'icon' => "$wgScriptPath/images/logo.png", @@ -72,7 +71,7 @@ $wgDBpassword = $_ENV["DB_PASSWORD"]; # MySQL specific settings -$wgDBprefix = $_ENV["DB_PREFIX"]; +$wgDBprefix = $_ENV["DB_PREFIX"] ?? "mw_"; # MySQL table options to use during installation or update $wgDBTableOptions = "ENGINE=InnoDB, DEFAULT CHARSET=binary"; diff --git a/mediawiki/apache-custom-directory.conf b/mediawiki/apache-custom-directory.conf new file mode 100644 index 0000000..b891bd8 --- /dev/null +++ b/mediawiki/apache-custom-directory.conf @@ -0,0 +1,20 @@ +# Allows all resources to be accessed on its given subpath +# Resolves 404 errors when files are served at the root +LoadModule env_module modules/mod_env.so +LoadModule alias_module modules/mod_alias.so + + + + Define WIKI_ALIAS_PATH "${WIKI_PATH}" + + + Define WIKI_ALIAS_PATH "" + + + Alias ${WIKI_ALIAS_PATH} /var/www/html + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + + AllowEncodedSlashes NoDecode + diff --git a/proxy/nginx.conf.template b/proxy/nginx.conf.template index 6dc34ba..deb60b4 100644 --- a/proxy/nginx.conf.template +++ b/proxy/nginx.conf.template @@ -29,9 +29,7 @@ http { } location /${WIKI_PATH} { - # Trailing slash is important, strips the location before forwarding, - # if location is /wiki, a request to /wiki/a would be forwarded to /a - proxy_pass http://wiki/; + proxy_pass http://wiki; } } }