20
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WordPressのパーミッション設定 決定版

Posted at

#はじめに
WordPressの構築時、SFTPでファイル転送ができないことや、プラグインがインストールできないなどのエラーに遭遇したため、wordPressのパーミッション設定についてまとめます。

#前提条件

  • wordpressのサーバーに対して、SFTPでファイル転送することがある。(ログインユーザはec2-user)
  • /var/www/html/配下にwordpressのディレクトリやファイルが置かれている
  • nginxを使用。apacheでも問題なし。

#ファイル転送やプラグインインストール時、パーミッションが原因のエラー

###プラグインやwordpressバージョンをアップデートするときに、FTP接続情報を聞かれる
原因は、サーバであるapacheやnginxに対して、ディレクトリやファイル所有者権限が不足しているためです。
スクリーンショット 2021-11-13 0.09.25.png

###プラグインを更新するときに、ディレクトリを作成できませんでした。とエラーになる
こちらも同様に原因は、サーバであるapacheやnginxに対して、ディレクトリやファイル所有者権限が不足しているためです。
スクリーンショット 2021-11-13 0.10.01.png

###SFTPでファイル転送できない
原因は、転送先のディレクトリや上書きするファイルに対して、書き込み権限が足りていないためです。
スクリーンショット 2021-11-13 0.21.36.png

#結論

サーバーがnginx、SFTPのログインユーザーをec2-userとして、ファイルを転送する場合、以下のように設定します。

ファイルやディレクトリ 権限 所有者:グループ
.htaccess 606 nginx:ec2-user
wp-config.php 600 nginx:ec2-user
ディレクトリ 775 nginx:ec2-user
ファイル 664 nginx:ec2-user

以下のコマンドで所有者とグループ、権限を一括で変更できます。

$ sudo chown -R nginx:ec2-user /var/www/html


$ sudo find /var/www/html -type f -exec chmod 664 {} \;
$ sudo find /var/www/html -type d -exec chmod 775 {} \;
↑できない場合は↓
$ sudo find /var/www/html -type f -print | xargs chmod 664
$ sudo find /var/www/html -type d -print | xargs chmod 775

-execオプションを入れることで、findで検索した結果を使って別のコマンドを実行できます。
その時に、{}は検索結果とエイリアスとして働きます。

###セキュリティー面に不安の場合
もし、セキュリティーに不安がある場合、ファイル転送作業後は、権限を狭めるとよいと思います。

ファイルやディレクトリ 権限 所有者:グループ
ディレクトリ 705 nginx:ec2-user
ファイル 604 nginx:ec2-user

##パーミッションを修正してもFTP接続情報が聞かれる場合
wp-config.phpに追記します。

wp-config.php
define('FS_METHOD', 'direct');

このとき、

if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
require_once(ABSPATH . 'wp-settings.php');

よりも上に追記する必要があります。

#wordpressのパーミッションに問題ないかの確認方法
wordpress→ツール→サイトヘルスに遷移し、サイトヘルスが「良好」だと問題ありません。

スクリーンショット 2021-11-12 13.33.26.png

#参考

20
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?