1. 程式人生 > >Linux無法寫入許可權問題 & 解決Wordpress不能自動安裝主題、外掛

Linux無法寫入許可權問題 & 解決Wordpress不能自動安裝主題、外掛

正 文:

 

    當你的wordpress遇到以下問題時:
1、不能上傳圖片 
2、不能自動安裝主題、外掛(需要FTP賬戶) 
3、不能自動更新 
4、其它任何需要wordpress寫檔案的問題
    這些問題基本都是一個原因,你的wordpress目錄不屬於當前的使用者和組,即web訪問的使用者沒有許可權操作wp的一切需要寫許可權的操作,其實就是linux下許可權不足,無法寫入造成的。

 

解決方法
首先需要你有root許可權,SSH登入,進入到wp的安裝目錄:
cd /var/www/html/my_wp_blog


給予所有的寫許可權:
chmod 777 wp-content
接下來給你的部落格的文章上傳一張圖片,WP會生成一個目錄,然後檢視是哪個使用者建立了資料夾。一般情況下,這個使用者名稱叫“apache”,也有不少人發現這個使用者是“nobody”,就飄易本身遇到的問題,我在VPS上安裝的是LNmp,這個使用者是“www”。
進入到wp的wp-content目錄,檢視該目錄下所有檔案/資料夾的許可權,所屬使用者、使用者組:
cd wp-content
ls -l

total 16
-rw-r–r– 1 root root   30 May  4  2007 index.php
drwxr-xr-x 3 root root 4096 Feb 10 19:31 plugins
drwxr-xr-x 5 root root 4096 Mar 23 03:04 themes
drwxrwxrwx 3 www www 4096 Mar 24 02:08 uploads
注意上傳目錄 uploads 是使用者 www 建立的。
接下來把wp-content許可權還原到 755::
cd ..
chmod 755 wp-content

下來就是實際修復的命令了,改變wp所在資料夾的擁有者為剛找到的這個使用者www:
cd ..
chown -R www:www my_wp_blog

 

備註:補充以另外一種查詢當前使用者和組的名稱的方法
開啟:

/usr/local/php/etc/php-fpm.conf


裡面有 user和group項,看他們的值是什麼,如下圖:

OK,問題解決了。下面是英文的原文:

 

There are a wide variety of problem reports appearing on support forums related to Wordpress that all have one root cause and solution. Here are some common issue descriptions:
* Problems uploading images
* Problems installing themes, plugins
* Problems auto-upgrading Wordpress
* Anything else where Wordpress needs to write files
And here is a typical error message:
“To perform the requested action, connection information is required.” Or…
“Unable to create directory [...]. Is its parent directory writable by the server?”

 

The problem is that Wordpress is executing in the context of your web server process, but the directories have write permissions based on the user context used to originally create the directories.

 

Many of the suggested solutions on the web simply won’t work, while other solutions work but create security problems with your Wordpress installation. Here is the full solution that should work on all Linux systems, regardless of the specific environment. What we are going to do is give your web server ownership of the directories and files of your Wordpress install. This requires you to be knowledgeable and comfortable in your bash shell environment, which is probably reasonably true if you installed Wordpress yourself.

 

First we will give everybody write access so that WP can write the content directories. Some solutions on the web stop at this step, but this leaves your files with no filesystem security. We are only doing this briefly in order to determine what user context is being used by the web server.

 

Go to your Wordpress root directory:
cd /var/www/html/my_wp_blog

 

Give the world write access to the content directory:
chmod 777 wp-content
Now log into Wordpress and upload a photo to a blog post, causing WP to create the new directories required. Then look on the server to see what user created the directories. This would commonly be apache, but also many people are reporting that this is the user “nobody” on their server.

 

# cd wp-content
# ls -l

total 16
-rw-r–r– 1 root root   30 May  4  2007 index.php
drwxr-xr-x 3 root root 4096 Feb 10 19:31 plugins
drwxr-xr-x 5 root root 4096 Mar 23 03:04 themes
drwxrwxrwx 3 apache apache 4096 Mar 24 02:08 uploads
Notice that the uploads directory was created by user apache:apache. This is the information you needed. Go back down one dir level and set the permissions back to a secure level.

 

# cd ..
# chmod 755 wp-content

Now for the actual fix. Recursively set the owner and group for your Wordpress installation to the user that created the uploads directory.

 

# cd ..
# chown -R apache:apache my_wp_blog

You’re done. Wordpress now has access to the file system for photo and attachment uploads, automatic upgrades, and anything else needed by your plugins.