#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: http://mirrors.slickstack.io/bash/ss-perms.txt ######################################## #### path: /var/www/ss-perms ####################################################################### #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Resets all file and user permissions across the entire SlickStack server ############# #### module version: Ubuntu 20.04 LTS ############################################################## #### sourced by: ss-install, ss-update, ss core cron jobs ########################################## #### bash aliases: ss perms ######################################################################## #################################################################################################### ## include SlickStack configuration ## source /var/www/ss-config ## include SlickStack functions ## source /var/www/ss-functions #################################################################################################### #### SS-Perms: Message (Begin Script) ############################################################## #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be noticed by sudo users who manually call ss core bash scripts ## ## echo message ## echo -e "${PURPLE}Running ss-perms: Resets all file and user permissions across the entire SlickStack server... ${NOCOLOR}" sleep "$SLEEP_MESSAGE_BEGIN" #################################################################################################### #### SS-Perms: Reset Ubuntu (System Files + Users/Groups) Permissions ############################## #################################################################################################### ## here we ensure permissions are safe for general system (Linux) files and otherwise ## ## since this is a single-user stack we can lock these down without issues ## #################################################################################################### #### SS-Perms: Reset SlickStack Core Files Permissions ############################################# #################################################################################################### ## SlickStack Core bash scripts and cron jobs are still executable by root regardless ## ## but these permissions help ensure consistency and better overall security ## ## ensure core directories exist (except html) ## mkdir /var/www mkdir /var/www/cache mkdir /var/www/cache/fastcgi mkdir /var/www/cache/opcache mkdir /var/www/logs mkdir /var/www/meta ## chroot (SFTP) user directory ## chown root:root /var ## must be root:root chown root:root /var/www ## must be root:root chmod 0755 /var/www ## must be 0755 ## SlickStack core scripts ## chown root:root /var/www/*cron* ## must be root:root chown root:root /var/www/ss* ## must be root:root chmod 0740 /var/www/*cron* ## 0740 is enough chmod 0740 /var/www/ss* ## 0740 is enough ## no worries if the SFTP user accesses this stuff (but www-data access is the priority) ## chown www-data:www-data /var/www/cache ## must be www-data:www-data chown www-data:www-data /var/www/cache/fastcgi ## must be www-data:www-data chown www-data:www-data /var/www/cache/opcache ## must be www-data:www-data chown www-data:www-data /var/www/logs ## must be www-data:www-data chown www-data:www-data /var/www/meta ## must be www-data:www-data chmod 0755 /var/www/cache ## 775 should also work chmod 0755 /var/www/cache/fastcgi ## 775 should also work chmod 0755 /var/www/cache/opcache ## 755 should also work chmod 0755 /var/www/logs ## 755 should also work chmod 0755 /var/www/meta ## 755 should also work ## ensure critical files and directories exist (will not overwrite) ## mkdir /var/www/html mkdir /var/www/html/.well-known mkdir /var/www/html/.well-known/acme-challenge ## accessed by server for e.g. Cerbot but also by SFTP user for things like Stripe ## chown -R $SFTP_USER:www-data /var/www/html/.well-known chmod 0755 /var/www/html/.well-known #################################################################################################### #### SS-Perms: Reset Permissions (Adminer) ######################################################### #################################################################################################### ## run ss-perms-adminer ## source /var/www/ss-perms-adminer #################################################################################################### #### SS-Perms: Reset Permissions (ClamAV) ########################################################## #################################################################################################### ## run ss-perms-clamav ## source /var/www/ss-perms-clamav #################################################################################################### #### SS-Perms: Reset Permissions (Craft CMS) ####################################################### #################################################################################################### if [[ "$SS_APP" == "craft" ]]; then ## run ss-perms-craft-core ## source /var/www/ss-perms-craft-core ## run ss-perms-craft-config ## source /var/www/ss-perms-craft-config fi #################################################################################################### #### SS-Perms: Reset Permissions (Magento) ######################################################### #################################################################################################### if [[ "$SS_APP" == "magento" ]]; then ## run ss-perms-magento-core ## source /var/www/ss-perms-magento-core ## run ss-perms-magento-config ## source /var/www/ss-perms-magento-config fi #################################################################################################### #### SS-Perms: Reset Permissions (MediaWiki) ####################################################### #################################################################################################### if [[ "$SS_APP" == "mediawiki" ]]; then ## run ss-perms-mediawiki-core ## source /var/www/ss-perms-mediawiki-core ## run ss-perms-mediawiki-config ## source /var/www/ss-perms-mediawiki-config fi #################################################################################################### #### SS-Perms: Reset Permissions (Moodle) ########################################################## #################################################################################################### if [[ "$SS_APP" == "moodle" ]]; then ## run ss-perms-moodle-core ## source /var/www/ss-perms-moodle-core ## run ss-perms-moodle-config ## source /var/www/ss-perms-moodle-config fi #################################################################################################### #### SS-Perms: Reset Permissions (MySQL) ########################################################### #################################################################################################### ## run ss-perms-mysql ## source /var/www/ss-perms-mysql #################################################################################################### #### SS-Perms: Reset Permissions (Nginx) ########################################################### #################################################################################################### ## run ss-perms-nginx ## source /var/www/ss-perms-nginx #################################################################################################### #### SS-Perms: Reset Permissions (OpenCart) ######################################################## #################################################################################################### if [[ "$SS_APP" == "opencart" ]]; then ## run ss-perms-opencart-core ## source /var/www/ss-perms-opencart-core ## run ss-perms-opencart-config ## source /var/www/ss-perms-opencart-config fi #################################################################################################### #### SS-Perms: Reset Permissions (PHP-FPM) ######################################################### #################################################################################################### ## run ss-perms-php ## source /var/www/ss-perms-php #################################################################################################### #### SS-Perms: Reset Permissions (Postfix) ######################################################### #################################################################################################### ## run ss-perms-postfix ## source /var/www/ss-perms-postfix #################################################################################################### #### SS-Perms: Reset Permissions (PrestaShop) ###################################################### #################################################################################################### if [[ "$SS_APP" == "prestashop" ]]; then ## run ss-perms-prestashop-core ## source /var/www/ss-perms-prestashop-core ## run ss-perms-prestashop-config ## source /var/www/ss-perms-prestashop-config fi #################################################################################################### #### SS-Perms: Reset Permissions (Redis) ########################################################### #################################################################################################### ## run ss-perms-redis ## source /var/www/ss-perms-redis #################################################################################################### #### SS-Perms: Reset Permissions (Ubuntu) ############################################################# #################################################################################################### ## run ss-perms-ubuntu-bash ## source /var/www/ss-perms-ubuntu-bash ## run ss-perms-ubuntu-crontab ## source /var/www/ss-perms-ubuntu-crontab ## run ss-perms-ubuntu-kernel ## source /var/www/ss-perms-ubuntu-kernel ## run ss-perms-ubuntu-ssh ## source /var/www/ss-perms-ubuntu-ssh ## run ss-perms-ubuntu-users ## source /var/www/ss-perms-ubuntu-users ## run ss-perms-ubuntu-utils ## source /var/www/ss-perms-ubuntu-utils #################################################################################################### #### SS-Perms: Reset Permissions (UFW) ############################################################# #################################################################################################### ## run ss-perms-ufw ## source /var/www/ss-perms-ufw #################################################################################################### #### SS-Perms: Reset Permissions (WordPress) ####################################################### #################################################################################################### if [[ "$SS_APP" == "wordpress" || -z "$SS_APP" ]]; then ## run ss-perms-wordpress-core ## source /var/www/ss-perms-wordpress-core ## run ss-perms-wordpress-config ## source /var/www/ss-perms-wordpress-config ## run ss-perms-wordpress-mu-plugins ## source /var/www/ss-perms-wordpress-mu-plugins ## run ss-perms-wordpress-cli ## source /var/www/ss-perms-wordpress-cli fi #################################################################################################### #### SS-Perms: Purge Cache (Redis) ################################################################# #################################################################################################### ## run ss-purge-redis ## source /var/www/ss-purge-redis #################################################################################################### #### SS-Perms: Touch Timestamp File (End Script) ################################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ## script timestamp ## touch /var/www/meta/.timestamp-ss-perms #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: http://www.unixmantra.com/2013/04/unix-linux-file-permissions.html ## Ref: https://unix.stackexchange.com/questions/203371/run-script-sh-vs-bash-script-sh-permission-denied/203372#203372 ## Ref: https://stackoverflow.com/questions/14372645/convert-dos2unix-line-endings-for-all-files-in-a-directory/19284034#19284034 ## Ref: https://stackoverflow.com/questions/11929461/how-can-i-run-dos2unix-on-an-entire-directory/11929475#11929475 ## Ref: https://unix.stackexchange.com/questions/310432/bash-script-to-convert-all-files-of-a-given-type-from-unix-to-dos-format/310437#310437 ## Ref: https://kb.iu.edu/d/acux ## Ref: https://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-lf-in-a-bash-script ## Ref: https://github.com/wp-cli/wp-cli/issues/3181 ## Ref: https://github.com/wp-cli/wp-cli/issues/1241 ## Ref: https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/ ## Ref: https://www.darklaunch.com/php-normalize-newlines-line-endings-crlf-cr-lf-unix-windows-mac ## Ref: https://serverfault.com/questions/344544/what-is-the-first-digit-for-in-4-digit-octal-unix-file-permission-notation ## Ref: https://unix.stackexchange.com/questions/79395/how-does-the-sticky-bit-work ## Ref: https://superuser.com/questions/684213/why-are-execute-permissions-along-with-write-permissions-needed-for-creating-a-f ## Ref: https://ubuntuforums.org/showthread.php?t=2383638 ## Ref: https://serverfault.com/questions/584986/bad-ownership-or-modes-for-chroot-directory-component ## Ref: https://stackoverflow.com/questions/6851909/how-do-i-delete-everything-in-redis ## Ref: https://serverfault.com/questions/888653/correct-web-user-for-php-fpm-under-nginx ## Ref: https://askubuntu.com/questions/25681/can-scripts-run-even-when-they-are-not-set-as-executable ## Ref: https://serverfault.com/questions/616788/nginx-403-forbidden-on-some-files-extensions ## Ref: https://serverfault.com/questions/686357/unable-to-open-jpeg-files-with-nginx ## Ref: https://askubuntu.com/questions/831414/what-is-the-difference-between-touch-file-and-file ## Ref: https://bash.cyberciti.biz/guide/Setting_up_permissions_on_a_script ## Ref: http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/raring/cron/raring/view/head:/database.c#L380 ## Ref: https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working ## Ref: https://unix.stackexchange.com/questions/358112/permissions-error-with-wordpress ## Ref: https://webinoly.com/support/975/chown-problem?show=975 ## SS_EOF