Mappen, bestanden & rechten - 2020 (WordPress, 2)
Configureren van rechten op bestanden en mappen rondom een WordPress-installatie, kan best complex zijn. In mei 2020 dacht dat ik er uit was: WordPress mag basically geen bestanden overschrijven, en updates gaan volledig via eigen scripts. Dat bracht echter een probleem met zich mee: Zonder schrijfrechten worden een aantal dingen toch wel erg ingewikkeld.
Daarom deze tweede versie van dit script (aug. 2020), met een parameter mode waardoor het script op twee manieren kan werken: Closed met beperkte rechten zoals passend voor operationeel werk, en Open met verruimde rechten, zoals nodig tijdens installatie van plugins en dergelijke niet-operationele gebeurtenissen.
Problemen
De eerdere oplossing en bijbehorende script, waren te weinig flexibel:
Automatische theme-updates
We hebben een project waar een externe partij het theme ontwikkeld. Nieuwe theme-updates kunnen automatisch worden toegepast. Het is onhandig dat dat nu niet werkt
Cache legen
Het is onhandig dat cache-plugins niet zelf de cache kunnen lezen wegens gebrek aan schrijftoegang
W3 Total Cache-configuratie
Plugin W3 Total Cache Error wil tijdens installatie bepaalde mapen & bestanden schrijven of aanpassen. Dat kan nu niet. Handmatig wil dat ook niet zo gemakkelijk lukken, ook al geeft de plugin aan, wat er precies moet gebeuren
Altijd gedoe met WordFence
Ik heb het nog steeds niet goed geanalyseerd, maar WordFence doet nog altijd moeitlijk.
Verschillende rechten gedurende installatie & in bedrijf
Oplossing: Verruimde schrijfrechten & twee sets van rechten
De nieuw oplossing is een tweetrapsraket:
- In eerste aanleg hetzelfde script als hiervoor, maar met meer plekken binnen een WordPress-installatie waar computeraccount
www-data
(gebruiker; Apache) mag schrijven - Een aparte modus van dit script, waarin rechten ruimer gedefineerd worden, zoals nodig tijdens installatie en configuratie van plugins en andere onderdelen.
Script
#!/bin/bash # echo " " echo " " echo "############################################################" echo "wp_sr - Set WordPress file and directory rights" echo "############################################################" echo " " echo "Examples:" echo " wp_sr # Set 'closed' settings on site in cwd" echo " wp_sr /var/www/example.com open # Set 'open' settings on given site" echo " " # # * Mappen, bestanden & rechten - 2020 (2) # * Jeroen Strompf - Aug. 2020 # * Also works with Wordfence # # ################################################################################### # Process input arguments ################################################################################### # # path=$1 mode=$2 ################################################################################### # Process path variable ################################################################################### # if [ -n "$path" ]; then # Path provided # echo " Path provided" # # Check if path starts with /var/www/ ######################################## # if [ "${path:0:9}" = "/var/www/" ]; then # echo " Path starts with /var/www/ - Good!" # # If not, add /var/www/ ######################################## # else # echo " Path doesn't start with /var/www/ - Added" path="/var/www/$path" # fi else # No argument provided # # Use PWD ######################################## # echo " No argument provided - Use PWD" path=$PWD # # # Check if path starts with /var/www/ ######################################## # if [ "${path:0:9}" = "/var/www/" ]; then # echo " PWD starts with /var/www/ - Good!" # else # echo " PWD doesn't start with /var/www/ - Exiting" exit fi fi # Check if this is the root of a WordPress site ############################################### # if [ -f "$path/wp-config.php" ]; then # echo " Path points to the root of a WordPress-site - Good" # else # echo " Path doesn't point to the root of a WordPress-site - Exiting" exit fi # echo " Path: $path" echo " " ################################################################################### # Process mode variable ################################################################################### # # Mode is only "open" when explicitly stated. Otherwise it's closed # if [ -n "$mode" ]; then # mode provided # if [ "$mode" = "open" ]; then mode="open" else mode="closed" fi else mode="closed" fi echo " Mode: $mode" echo " " ################################################################################### # Set owner & group ################################################################################### # echo " Apply universal settings..." echo " Set owner & group..." sudo chown -R www-data $path sudo chgrp -R www-admin $path echo " Set owner & group... Done" ################################################################################### # Set basic rights ################################################################################### # # * Thanks to this command, only one find-chmod-command is needed. Otherwise, an # additional find-chmod-command would have been needed to disable execution for # files, and that takes quite some time # * Drawback of this approach: You can't ls before the whole procedure is finished. # You can however, use "sudo ls" or "sudo ls -alF" if really needed: In order to # do an "ls" command, you need execution right on the directory and read-rights # on something ;) # echo " Set basic rights..." # # Settings ############################################# # # * User: Read (4) - no write, no execute → 4 # * Group: Read (4) + write(2), no execute → 6 # * Others: Nothing → 0 # sudo chmod -R 460 $path # # sudo ls -alF $path # # echo " Set basic rights... Done!" ################################################################################### # Set directory rights for user & group ################################################################################### # # * This command is quite slow. Hence do it for user and group at once # * Grant execute rights for user & group on directories - Not on files # * "sudo" is needed for "find", as otherwise, you can't yet read this direcotry # echo " Set directory rights for user & group..." sudo find $path -type d -exec sudo chmod ug+x {} \; # # sudo ls -alF $path echo " Set directory rights for user & group... Done" ################################################################################### # Allow user always to write at specific locations ################################################################################### # # These are locations where User needs to write during *operationals* # echo " Allow User to always write at specific locations..." # Uploads #################################### # # Obvious that User needs write-access to the uploads folder. In the Apache # virtual file definition, execution of php is disabled here # sudo chmod -R u+w $path"/wp-content/uploads" # Custom theme #################################### # # No need to apply this to the whole themes folder - That is still done through # the regular update process # sudo chmod -R u+w $path"/wp-content/themes/kbo2019" # W3 Total Cache - Cache directory #################################### # # Was: sudo chmod -R u+w $path"/wp-content/cache/page_enhanced" but this seems # too limited: Cache needs to be flushed during operationals # sudo chmod -R u+w $path"/wp-content/cache/" # wp-content/wflogs #################################### # # * Wordfence seems to need write rights to wp-content/wflogs when the site is operational # * http://wiki.devliegendebrigade.nl/Wordfence#Oorzaak # sudo chmod -R u+w $path"/wp-content/wflogs/" echo " Allow User to always write at specific locations... Done" echo " Apply universal settings... Done" ################################################################################### # If mode="open" - Specific settings ################################################################################### # # if [ "$mode" = "open" ]; then # echo " Mode is 'open' - Allow User to write at specific locations..." # # wp-config #################################### # # Needed by W3 Total Cache # sudo chmod u+w $path"/wp-config.php" # Allow writing to root to create .htaccess file ################################################ # # * Needed by W3 Total Cache # * Not recursively # sudo chmod u+w $path # Allow writing to .htaccess ################################################ # # Needed by W3 Total Cache # sudo chmod u+w $path"/.htaccess" # wp-content ################################################ # # * Needed by Wordfence, to create folder wp-content/wflogs # * Not recursively # sudo chmod u+w $path"/wp-content" echo " Mode is 'open' - Allow User to write at specific locations...Done" # else # # Nothing needs to be done: This is covered by the universal settings # fi