2015年9月14日 星期一

WordPress Tuning 及效能改進的常用小技巧

WordPress Tuning 及效能改進的常用小技巧

Disable Revision, 取消文章版本記錄功能:

1. 修改 WordPress 網站根目錄下 wp-config.php 檔
例如檔案 /var/www/html/wp-config.php 找到 //define('WP_POST_REVISIONS', true ); 這行進行修改或是直接在底下添加:
define('AUTOSAVE_INTERVAL', 300 );   // 每5分鐘自動存檔一次
define('WP_POST_REVISIONS', false ); // 存檔時不備存前一版內容
2. 進入 database 清除舊有的 revision 記錄
$ mysql -uroot -p
mysql> use myWordPressDBName;
mysql> DELETE FROM wp_posts WHERE post_type = "revision";
mysql> exit;
每個 WordPress 的子網誌有自己的資料表群組, 例如第2個子網誌的貼文資料表有可能名為 wp_2_posts, 要修改前要先確認正確的資料表名稱。

參考資料:

How to Disable Post Revisions in WordPress and Reduce Database Size

WordPress ERR_TOO_MANY_REDIRECTS

WordPress ERR_TOO_MANY_REDIRECTS


Got an WordPress ERR_TOO_MANY_REDIRECTS error when the first time visiting your new fresh installed WordPress website?
WordPress_ERR_TOO_MANY_REDIRECTS
Try following approaches, it maybe helps.

Case 1: WordPress was installed in an LAMP server

When you installed WordPress in an LAMP server, It means the Apache Server is your only httpd server, and it is responsible for the directory rewrite job for your WordPress website.

Step 1: Check Apache rewrite module is enabled!
Simply enable it in Ubuntu using following command:
$ sudo a2enmod rewrite
and you'll find a "rewrite.load" file in directory "/etc/apache2/mods-enabled" with an standard Ubuntu installation.
Step 2: Check .htaccess authorization
If your WordPress is installed in directory "/var/www/html", find the ".htaccess" file is in it with correct authorization. You could use following command to set the authorization:
$ sudo chown www-data:www-data /var/www/html/.htaccess
$ sudo chmod 644 /var/www/html/.htaccess
Step 3: Check rewrite authorization already granted to WordPress directory
If your WordPress is installed in directory "/var/www/html", edit the Apache configuration file:
$ sudo vi /etc/apache2/apache2.conf
and make sure you've granted the rewrite authorizaiton to the WordPress directory something like:
<Directory /var/www/html>
        AllowOverride All
</Directory>
Step 4: Verify .htaccess file again if you installed WordPress as an WordPress MU (WordPress Nework)
Go to the control panel of WordPress where you clicked to install WordPress Network previously, and make sure you've followed it's instruction to modify the .htaccess file.

Case 2: WordPress was installed in an LNAMP server

When you installed WordPress in an LNAMP server, it usually means you use Nginx server as an reverse proxy httpd server and proxy requests to Apache server. It will be more easy and flexible when you leave the rewrite job to Apache server.
Step 1: Verify every steps in case 1 are set!
Apache server still responsible for WordPress directory's rewrite job.

Step 2: Pass the WordPress directory request to Apache server from Nginx
By the Nginx's default configuration, your WordPress request will be trapped in loop of index.php page and root directory. Just pass the WordPress requests to Apache server and everything will be fine.
Try to add following codes as a final location search rule to your "/etc/nginx/sites-available/default" file if your Nginx is an standard installation in Ubuntu:
        location ~ ^/.* {
                proxy_pass http://localhost:8080;
                include proxy_params;
        }
The http://localhost:8080 in the example should be changed to http://YourServerDomainName:YourApachePortNumber.

Hope this helps!
Relative article

2015年9月12日 星期六

WordPress sitemap Apache .htaccess Rewrite 設定

WordPress sitemap Apache .htaccess Rewrite 設定

簡述


LNAMP LAMP 架構下 WordPress MU WordPress Nework 網誌網路使用 Yoast SEO sitemap XML網站地圖 時 Apache 後台的 .htaccess rewrite 規則定義檔的設定方式, Nginx 不使用此檔
檔案位置: /網站根目錄/.htaccess
包含 WordPress rewrite 規則、Yoast SEO 外掛中 sitemap 的 rewrite 規則, 並已考慮 WordPress MU 子目錄架構的狀況。內容定義如下:
# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# Yoast SEO sitemap rewrite rule for WordPress - start (by rex)
RewriteRule ^([^/]+/)*sitemap_index.xml$ /$1index.php?sitemap=1 [L]
RewriteRule ^([^/]+/)*locations.kml$ /$1index.php?sitemap=wpseo_local_kml [L]
RewriteRule ^([^/]+/)*geo_sitemap.xml$ /$1index.php?sitemap=geo [L]
RewriteRule ^([^/]+/)*([^/]+?)-sitemap([0-9]+)?.xml$ /$1index.php?sitemap=$2&sitemap_n=$3 [L]
RewriteRule ^([^/]+/)*([a-z]+)?-?sitemap.xsl$ /$1index.php?xsl=$2 [L]
# Yoast SEO sitemap rewrite rule for WordPress - end

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

</IfModule>

# END WordPress

2015年9月11日 星期五

[專案] LNAMP 網站重建, 根目錄移轉, 架設 WordPress 4.3 網誌網路

[專案] LNAMP 網站重建, 根目錄移轉, 架設 WordPress 4.3 網誌網路

專案日期: 2015.09.11 (五)


目的:

1. 想讓 local 端和免費空間上所建的 WordPress 網誌網路目錄結構一致, 方便在 local 編撰後再 update 到免費空間上發佈!

2. 原先網站根目錄 /var/www/html 之下的內容想先保留可切換回來的彈性。

3. 原 WordPress 網誌網路的文章資料要倒進新的網誌


專案資訊:

原網站根目錄: /var/www/html
新網站根目錄: /var/www/wordpress



執行步驟:

1. 透過 WordPress 控制台工具匯出原 wordpress 上的文章
2. 備份 Apache 與 Nginx 需修改到的設定檔:
$ sudo cp /etc/apache2/apache2.conf /ect/apache2/apache2.conf.path.html
$ sudo cp /etc/apache2/sites-available/000-default.conf /ect/apache2/sites-available/000-default.conf.path.html
$ sudo cp /et/nginx/sites-available/default /etc/nginx/sites-available/default.path.html
3. 修改 Apache 與 Nginx 的設定檔, 將錄徑 /var/www/html 改為 /var/www/wordpress
$ sudo vi /etc/apache2/apache2.conf
<Directory /var/www/wordpress>
        AllowOverride All
</Directory>

$ sudo vi /etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/wordpress

$ sudo vi /etc/nginx/sites-available/default
root /var/www/wordpress;
4. 下載 WordPress 4.3, 解壓縮, 複製程式主目錄到 /var/www/wordpress 並修改權限
$ sudo cp -r wordpresss /var/www/wordpress
$ sudo chown -R www-data:www-data /var/www/wordpress
$ sudo chmod -R 644 /var/www/wordpress
$ sudo chmod -R a+X /var/www/wordpress
5. 備份並修改 wp-config-sample.php 啟用 WordPress MU 的安裝工具
$ sudo cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config-sample.php.backup

$ sudo vi /var/www/wordpress/wp-config-sample.php
define('WP_ALLOW_MULTISITE', true);
6. 重啟 Apache 及 Nginx 服務
$ sudo service apache2 restart
$ sudo service nginx  restart
7. 建立新的 MySQL 資料庫, 和原站台的資料分開管理
$ mysql -uroot -p
mysql> create database newpress;
mysql> exit
8. 瀏覽器連上 VM 網址, 開始安裝 WordPress
使用新的 database 名稱 newpress,
安裝的第一個網誌將做為網誌網路的簡單入口, 先輸入與網誌網路相關標題, 之後可修改!
安裝完會要求以設定的帳密登入, 登入後會直接進到控制台

9. 安裝網誌網路:
控制台 --> 工具 --> 網誌網路安裝
設定 "網誌網路標題"
如果連線時是使用 IP 而非 domain name, WordPress 會直接認為初始安裝是用子目錄, 所以網誌網路的架構只能用子目錄而不能使用子網域;因為本來就想用子目錄方式安裝, 所以不影響!
點選 "安裝" 按鈕後會提示要備份和修改 .htaccess 和 wp-config.php 檔, 照做!
$ sudo cp /var/www/wordpress/.htaccess /var/www/wordpress/.htaccess.backup
$ sudo cp /var/www/wordpress/wp-config.php /var/www/wordpress/wp-config.php
修改完後重新登入 WordPress
10. 修改 nginx 的設定檔, 解決已知 WordPress 架在 LNAMP 下會出現的重複導向問題
$ sudo vi /etc/nginx/sites-available/default  # 注意 location 設定的順序會影響結果
// location 原 /
// location 自訂 .php
// location 自訂 cache
// location wp-admin
location ~ (^(?:/[^/]+)?/wp-admin$) {
        rewrite (.*) $1/;
}
// location 自訂 / 
location ~ ^/.* {
        proxy_pass http://localhost:8080;
        include proxy_params;
}
有些設定看似多餘, 是為了讓設定項目看起來和原先版本的架構較一致方便比對, 兩個版本的設定檔都有適合使用的地方, 都要保存起來參考。

11. 使用和免費空間上一致的子目錄名稱建立一個新的子網誌並匯入備存的既有文章
日後重新匯入時要先刪除文章、頁面、標簽、分類並清除相關回收桶資料才不會重複!

安裝後出了一個後來覺得很糗的問題:
架好後出現以下無法存取的情況, 一直想不通是啥原因!

Not Found

The requested URL /lnamp/wp-admin/ was not found on this server.
Apache/2.4.7 (Ubuntu) Server at 192.168.227.134 Port 8080
看錯誤訊息是來自 Apache 無法讀取 /lnamp/wp-admin/ 這個偽靜態網址。
其實一開始就猜可能是 Apache 那邊設定的問題, 因為根據之前使用的經驗 Nginx 這邊最多只需處理一個因 LNAMP 架構多出來的一個問題, 要負責把 /lnamp/wp-admin rewrite 成 /lnamp/wp-admin/ 丟給 Apache 就算完成任務了,之後其他 wp-admin 的工作或是偽靜態網址的處理都是 Apache 那邊要負責的! 但一直看不出原因,所以又來回好幾次呆呆地在測 Nginx 那邊的 proxy 設定是否改錯了? /var/www/wordpress 和底下檔案及 .htaccess 權限是否設錯了? Apache 的 rewrite module 是否沒重新啟用而無效? 後來發現連主網誌的偽靜態網址也失效,確信是 Apache 那邊 rewrite 出了問題,把相關設定仔仔細細看了好幾遍最後發現是開 rewrite 權限的 <Directory> 不小心把 wordpress 打成 wordpresss 多了一個 s, 差點沒昏倒! XD


WordPress 資料庫中資料表項目

WordPress 資料庫中資料表項目


WordPress 資料庫中資料表項目簡單分成三組:
網誌網路子網誌獨立網誌網誌網路主網誌
commentmetacommentmetacommentmeta
commentscommentscomments
linkslinkslinks
optionsoptionsoptions
postmetapostmetapostmeta
postspostsposts
term_relationshipsterm_relationshipsterm_relationships
term_taxonomyterm_taxonomyterm_taxonomy
termstermsterms
usermetausermeta
usersusers
blog_versions
blogs
registration_log
signups
site
sitemeta

2015年9月10日 星期四

WordPress 內嵌 Google 相簿圖片的方法

WordPress 內嵌 Google 相簿圖片的方法


方法:

1. 將 Google 相簿中要使用在 WordPress 的圖片集中在一個相簿, 將相簿分享並取得分享的網址。

2. 登出所有 Google 帳戶, 在未連接 Google 帳戶的狀態下開啟分享相簿的網址, 點開要取得連結網址的圖片, right-click 取得網址。

3. 貼圖應加上自己可搜索的圖片資訊, 例如圖檔名稱, 以防 Google 又改變策略使得連結失效時不易知道原來是連哪張圖。

注意事項:

1. 在連接 Google 帳號狀態下所取得的圖片網址是後來改版極長的網址, 這個分享網址只有已登入 Google 的用戶端才能存取得到, 未登入者會跳出 Google 帳戶登入畫面要求先登入! 所以要取得圖片網址前一定要先登出 Google 帳戶。

2. 透過 Google 相簿分享連結取得的圖片僅為縮小版的圖片, 無法連結原圖。要看原圖必需開啟分享的相簿網址, 使用其中附帶的縮放工具來放大。

另有取得原圖連結的方法是從 picasaweb.com 取得分享網址或將圖分享在 blogspot.com 的 blog 上從中取得分享網址, 然後將網址中一段標示縮放尺寸的 sxxxx (xxxx 為尺寸大小的數字) 改為 s0 即為原尺寸圖片分享位址!

測試日期: 2015.09.10

在 WordPress 文章中部份使用自訂的 CSS

在 WordPress 文章中部份使用自訂的 CSS


1. 編輯自訂的 CSS 檔, 例如 /var/www/html/mycss/rexcss01.css, 使用 rexcss01 來當 class id, 範例如下:
table.rexcss01, table.rexcss01 th, table.rexcss01 td {
border: 1px solid black;
border-collapse: collapse;
border-spacing: 0;
padding: 5px;
vertical-align: middle;
width: auto;
}
table.rexcss01 th {
text-align: center;
}
pre.rexcss01 {
background-color: black;
color: #bbbbbb;
}
檔案權限要打開讓網站服務帳號 www-data 可以存取。

2. 在 WordPress 的文章第一行載入 CSS 檔:
<link rel="stylesheet" href="/mycss/rexcss01.css">
文章使用文字模式編輯且千萬不要切換成預覽方式編輯, 不然 link 或 style 此類的 tag 和內容會被自動移除!

3. 在想要使用自訂 CSS 的 tag 上加上 class id, 例如:
<table class="rexcss01">
<thead><tr><td>左</td><td>右</td></tr></thead>
<tbody>
<tr><td>1</td><td>甲</td></tr>
<tr><td>2</td><td>乙</td></tr>
</tbody>
</table>

4. 範例所得的結果如下:
1
2

後來發現使用非正規方法在 html body 中 import CSS 檔不太適合, 因為 WordPress 的匯入工具會自動將這些語法清除。目前比較可行的辦法還是直接把自訂的 style sheet 內容加在佈景主題的 style.css 檔尾! 只是像 WordPress.com 的免費空間無法直接去修改檔案, 只能選用可自訂 CSS 內容的佈景主題!