在 .bash_profile 中创建别名以获得 Wordpress 的正确权限

Creating an alias in .bash_profile for getting the correct permissions on Wordpress

所以我一直在阅读有关 Wordpress 正确权限的各种帖子,最值得注意的是以下内容:

[1][1]: Correct file permissions for WordPress

[1][1]: https://codex.wordpress.org/Hardening_WordPress

有人可以解释一下将我的用户添加到 www-data 组中是否是个好主意。以及为什么我需要这样做,因为我不完全理解这一点,我是否需要这样做...

我刚刚花了很多时间更新我所有的 wordpress 网站,并开始在更高级别保护它们。我想创建单个命令或脚本,以便我可以在

之间快速设置正确的权限
  1. 设置 wordpress
  2. 更新插件和 wordpress
  3. 正常强化的 Wordpress,用户仍然可以编辑和上传媒体,但它已安全锁定。

那么最好创建一个.sh 文件或在.bash_profile 中创建一个别名。我有大约 14 个 wordpress 网站,我刚刚花时间更新和保护它们,现在我需要整理权限。显然,我希望以最有效的方式做到这一点。

目前的.bash_profile选项是:

设置 Wordpress 并更新

alias suwpchn='sudo chown -R www-data:www-data *; find . -type d -exec chmod 755 {} \; find . -type f -exec chmod 644 {} \; ls -la;'

但我得到的错误是

find: paths must precede expression: find

但我也在考虑有一个 bash 脚本,我可以在其中调用它,它会立即设置所有 wordpress 站点权限,这是我从 Michael Conigliaro 那里找到的:

#!/bin/bash
#
# This script configures WordPress file permissions based on recommendations
# from http://codex.wordpress.org/Hardening_WordPress#File_permissions
#
# Author: Michael Conigliaro (https://gist.github.com/macbleser/9136424)
#
WP_ROOT=${1:-.} # <-- wordpress root directory, current directory by default
[ -e "$WP_ROOT/wp-config.php" ] || { echo "Usage: [=12=] /path/to/wordpress"; exit; } # <-- detect that the directory is a wordpress root
WP_OWNER=$(id -u $(logname)) # <-- wordpress owner (This assumes the wordpress owner is the logged in user)
WP_GROUP=$(id -g $(logname)) # <-- wordpress group (This assumes the wordpress owner is the logged in user)
WS_GROUP=$(
     source /etc/apache2/envvars 2>/dev/null && # This works on debian-based systems at least
     echo "$APACHE_RUN_GROUP" ||
     echo nobody  
) # <-- webserver group
echo "Fixing permissions on $WP_ROOT"
echo "Wordpress owner.group: $WP_OWNER.$WP_GROUP"
echo "Web Server group: $WS_GROUP"

echo 'reset to safe defaults'
find ${WP_ROOT} -exec chown ${WP_OWNER}:${WP_GROUP} {} \;
find ${WP_ROOT} -type d -exec chmod 755 {} \;
find ${WP_ROOT} -type f -exec chmod 644 {} \;

echo 'allow wordpress to manage wp-config.php (but prevent world access)'
chgrp ${WS_GROUP} ${WP_ROOT}/wp-config.php
chmod 660 ${WP_ROOT}/wp-config.php

echo 'allow wordpress to manage .htaccess'
touch ${WP_ROOT}/.htaccess
chgrp ${WS_GROUP} ${WP_ROOT}/.htaccess
chmod 664 ${WP_ROOT}/.htaccess

echo 'allow wordpress to manage wp-content'
find ${WP_ROOT}/wp-content -exec chgrp ${WS_GROUP} {} \;
find ${WP_ROOT}/wp-content -type d -exec chmod 775 {} \;
find ${WP_ROOT}/wp-content -type f -exec chmod 664 {} \;

当我 运行 使用此脚本时,我的 wordpress 网站出现白屏。我在 Debian GNU/Linux 8 \n \l

我看到的 WS_GROUP 是错误的,因为它在我的服务器上将 nobody 打印为 apache 运行s 作为 www-data。但我不确定如何解决这个问题 source /etc/apache2/envvars 它说 export APACHE_RUN_GROUP=www-data 所以不确定为什么那部分不工作。

谁能帮我看看如何修复脚本以及如何添加一组 wordpress 网站,这样我就可以 运行 脚本,它会为所有网站设置权限,这样我就可以自动更新网站...

谢谢 J

它看起来像';'由于它被转义而未被识别,但 -exec 仍然需要它,因此您可以同时使用:

alias suwpchn='find . -type d -exec chmod 755 {} \;; find . -type f -exec chmod 644 {} \;; ls -la;

我不是bash大师所以我不能在这里详细说明。