Skip to content

2020

Fixing Endless Redirection with HTTPS Settings in WordPress When Using AWS Load Balancer

Problem

I set up a WordPress blog on two Amazon Web Services (AWS) EC2 instances located in different availability zones. In front of these instances, an Elastic Load Balancer (ELB) is configured to redirect all HTTP requests on port 80 to HTTPS on port 443. Since the wp-config.php file was not updated to reflect this change, the requests were still using HTTP. To correct this, I updated the following values to use HTTPS:

    define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
    define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');

However, this led to an endless redirection loop, eventually resulting in an error stating "too many redirections."

Solution

To resolve the issue, add the following line to the wp-config.php file:

    $_SERVER['HTTPS'] = 'on';

This will fix the issue. Configurations like this can be challenging to troubleshoot, and you may spend many hours looking for a simple solution. Hopefully, this post will save you some time if you encounter this particular issue.

使用AWS負載平衡器時,透過HTTPS設定修復WordPress的無盡重定向

問題

我在兩個位於不同可用性區域的Amazon Web Services (AWS) EC2實例上設置了一個WordPress博客。在這些實例前,配置了一個Elastic負載平衡器 (ELB),以將所有在80端口的HTTP請求重定向到443端口的HTTPS。由於wp-config.php文件未更新以反映此變更,請求仍在使用HTTP。為了糾正這個,我更新了以下值以使用HTTPS:

    define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
    define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');

然而,這導致了一個無窮的重定向循環,最終導致一個錯誤,說明"重定向太多次"。

解決方案

要解決這個問題,需要在wp-config.php文件中添加以下行:

    $_SERVER['HTTPS'] = 'on';

這將修復此問題。這樣的配置可能難以排除故障,您可能會花費很多時間尋找一個簡單的解決方案。希望這篇文章能為您節省一些時間,如果您遇到這個特殊問題。

Installing PHP 7.2 Instead of PHP 5.4 on Amazon Linux 2

Problem

When you launch a new Amazon Linux 2 AMI server and try to install PHP using the following command:

yum install php

After a successful installation, if you check the version using php -v, you'll see:

PHP 5.4.16 (cli) (built: Oct 31 2019 18:34:05)

However, the latest PHP version is 7.2, and you may want to use this newer version instead.

Solution

You can enable PHP 7.2 via Amazon Linux Extras with the following command:

sudo amazon-linux-extras enable php7.2

Once it is enabled, follow the subsequent instructions to complete the installation:

yum clean metadata
yum install php-cli php-pdo php-fpm php-json php-mysqlnd

That's it. Check the PHP version again using php -v, and it should now display:

PHP 7.2.28 (cli) (built: Mar 2 2020 19:38:11) ( NTS )

在Amazon Linux 2上安裝PHP 7.2而非PHP 5.4

問題

當你啟動一個新的 Amazon Linux 2 AMI 服務器並嘗試使用以下命令安裝 PHP:

yum install php

成功安裝後,如果你使用 php -v檢查版本,你會看到:

PHP 5.4.16 (cli) (建立日期: 2019年10月31號 18:34:05 )

不過,最新的PHP版本是7.2,你可能希望使用這個新版本。

解決方案

你可以透過 Amazon Linux Extras 啟動 PHP 7.2,使用以下命令:

sudo amazon-linux-extras enable php7.2

一旦已經啟動,按照接下來的指示完成安裝:

yum clean metadata
yum install php-cli php-pdo php-fpm php-json php-mysqlnd

就這樣。再次使用 php -v檢查 PHP 版本,現在應該會顯示:

PHP 7.2.28 (cli) (建立日期: 2020年3月2日 19:38:11 ) ( NTS )

Reading Large Files Using Node.js

I recently faced the task of analyzing a massive dataset consisting of log files. When I attempted to open the file in Excel, my laptop simply froze. Given the limitations of the tools available, I decided to parse the file using a Node.js script.

Problem

To read a small file, you might use the following script:

var fs = require("fs")

fs.readFile("path/mySmallFile.txt", "utf-8", (err, data) => {
  if (err) {
    throw err
  }
  console.log(data)
})

Using this script, you should be able to read the content of a small file. However, for large files, you might encounter a buffer error like RangeError: Attempt to allocate Buffer larger than maximum size. The script would terminate, producing an error similar to the following:

Error: "toString" failed
  at stringSlice (buffer.js)
  at Buffer.toString (buffer.js)
  at FSReqWrap.readFileAfterClose [as oncomplete]

Solution

To read a large file, you can use Node.js's native readline library like so:

var fs = require("fs")
var readline = require("readline")

const rl = readline.createInterface({
  input: fs.createReadStream("path/largeFile.csv"),
  output: process.stdout,
  terminal: false,
})

rl.on("line", line => {
  console.log(line)
})

rl.on("pause", () => {
  console.log("Done!")
})

Replace the file path with the path to your large file. Inside the on('line') function, you can process the file line by line—such as parsing it into JSON and incrementing a counter. The final sum can be displayed using the on('pause') function after the file has been completely read.

With this approach, you should now be able to process massive datasets using Node.js. For more information, please refer to the official documentation: Node.js Readline API.

使用Node.js讀取大型檔案

我最近面臨了分析由日誌文件組成的大數據集的任務。當我試圖在Excel中打開這個文件時,我的筆記本電腦簡直凍結了。鑑於可用工具的限制,我決定使用Node.js腳本解析該文件。

問題

要讀取一個小文件,你可能會使用以下腳本:

var fs = require("fs")

fs.readFile("path/mySmallFile.txt", "utf-8", (err, data) => {
  if (err) {
    throw err
  }
  console.log(data)
})

使用此腳本,你應該能夠讀取小文件的內容。然而,對於大文件,你可能會遇到緩存錯誤,例如 RangeError: 嘗試分配的緩衝區大於最大大小。該腳本將終止,產生類似於以下的錯誤:

Error: "toString" failed
  at stringSlice (buffer.js)
  at Buffer.toString (buffer.js)
  at FSReqWrap.readFileAfterClose [as oncomplete]

解決方案

要讀取一個大文件,你可以像這樣使用Node.js的本地 readline庫:

var fs = require("fs")
var readline = require("readline")

const rl = readline.createInterface({
  input: fs.createReadStream("path/largeFile.csv"),
  output: process.stdout,
  terminal: false,
})

rl.on("line", line => {
  console.log(line)
})

rl.on("pause", () => {
  console.log("Done!")
})

將文件路徑替換為你的大文件的路徑。在 on('line')函數內部,你可以逐行處理文件,例如將其解析為JSON並增加計數器。完成閱讀文件後,可以使用 on('pause')函數顯示最終總和。

使用這種方法,你現在應該能夠使用Node.js處理大量數據集。有關更多信息,請參閱官方文檔:Node.js 讀取API

Migrating WordPress MySQL Database to AWS RDS

In this article, I'm going to explain how to migrate your local WordPress MySQL database to Amazon Web Services (AWS) Relational Database Service (RDS). You might want to do this for the following benefits:

  1. Improved performance, as your database will be separate from the resources running on your EC2 instance.
  2. The ability to horizontally scale your website, allowing multiple EC2 instances to connect to the same database.
  3. Easier database maintenance and upgrade tasks.

Convinced? Here are the steps to accomplish this migration:

First, navigate to the AWS console and choose RDS. Create a MySQL database, filling in the form as shown below:

DB Instance Identifier: wordpress Master Username: admin Master Password: [YourChoice]

You can leave most settings at their default values, including the default VPC.

Second, edit the security group of this newly created database. Select the database, navigate to the Connectivity and Security tab, and choose the security group. In the inbound rule tab, edit the inbound rule to remove the default settings and choose the type: MySQL, Protocol: TCP, and Port range: 3306.

In the source section, you may either choose the security group associated with your WordPress EC2 instance or enter your own IP address. If you haven't created your WordPress EC2 instance yet, you can do it now, or simply use Bitnami WordPress from the marketplace.

Third, SSH into your WordPress instance and back up your existing WordPress database with this command:

    mysqldump -u root -p [YourDatabaseName] > backup.sql

Replace [YourDatabaseName] with the name of your database, such as bitnami_wordpress. The backup.sql file should be created in your current directory. Import this file to your newly created AWS RDS instance by running the command:

    mysql -u admin -p -h [RDS_ENDPOINT] -D wordpress < backup.sql

Replace admin and [RDS_ENDPOINT] with your own values. If you encounter an error like:

    ERROR 1049 (42000): Unknown database 'wordpress'

This means that your wordpress database has not been created yet. First, connect to the database with the command:

    mysql -h [RDS_ENDPOINT] --user=admin --password=[YourPassword]

Once connected to MySQL, create a new database with the command:

    mysql> CREATE DATABASE wordpress;
    Query OK, 1 row affected (0.00 sec)
    mysql> exit;
    Bye

Finally, edit the wp-config.php file in your WordPress EC2 instance. This file is usually located in your WordPress directory, such as /home/bitnami/apps/wordpress/htdocs if you're using Bitnami. Update the following values:

    /** The name of the database for WordPress */
    define( 'DB_NAME', 'wordpress' );

    /** MySQL database username */
    define( 'DB_USER', 'admin' );

    /** MySQL database password */
    define( 'DB_PASSWORD', '[YourPassword]' );

    /** MySQL hostname */
    define( 'DB_HOST', '[RDS_ENDPOINT]' );

Replace the placeholders with your own values and save the file. The changes should take effect immediately.

Done. Feel free to reach out if you have any questions.

將WordPress MySQL資料庫遷移到AWS RDS

在這篇文章中,我將說明如何將您本地的WordPress MySQL資料庫遷移到Amazon Web Services(AWS)關聯性資料庫服務(RDS)。你可能會想要這樣做以獲取以下好處:

  1. 改善性能,因為您的資料庫將與在EC2實例上運行的資源分開。
  2. 能夠橫向伸縮您的網站,允許多個EC2實例連接到同一個資料庫。
  3. 更容易進行資料庫維護和升級任務。

信服了嗎?下面是達成此次遷移的步驟:

首先,導航到AWS控制台並選擇RDS。建立一個MySQL資料庫,如下圖所示填寫表單:

DB實例識別碼: wordpress 主用戶名: admin 主密碼: [YourChoice]

您可以將大多數設置保留為其預設值,包括預設的VPC。

其次,編輯這個新建的資料庫的安全組。選擇資料庫,導航至連接和安全標籤,並選擇安全組。在入站規則標籤中,編輯入站規則以移除預設設定並選擇類型:MySQL,協議:TCP,以及端口範圍:3306。

在來源部分,您可以選擇與您的WordPress EC2實例相關聯的安全組,或者輸入您自己的IP地址。如果您還未創建您的WordPress EC2實例,現在你可以做,或者只需從市場使用Bitnami WordPress。

第三,SSH進入您的WordPress實例,並用此命令備份您現有的WordPress資料庫:

    mysqldump -u root -p [YourDatabaseName] > backup.sql

[YourDatabaseName]替換為您的資料庫名稱,例如bitnami_wordpress。應在您的現有目錄中創建backup.sql文件。通過運行命令將此文件導入到您新建的AWS RDS實例:

    mysql -u admin -p -h [RDS_ENDPOINT] -D wordpress < backup.sql

admin[RDS_ENDPOINT]替換為您自己的值。如果您遇到如下的錯誤:

    ERROR 1049 (42000): Unknown database 'wordpress'

這表示您的wordpress資料庫尚未被創建。首先,用以下命令連接到資料庫:

    mysql -h [RDS_ENDPOINT] --user=admin --password=[YourPassword]

一旦連接到MySQL,就用以下命令創建一個新的資料庫:

    mysql> CREATE DATABASE wordpress;
    Query OK, 1 row affected (0.00 sec)
    mysql> exit;
    Bye

最後,編輯您的WordPress EC2實例中的wp-config.php文件。該文件通常位於您的WordPress目錄中,例如倘若您使用的是Bitnami,那麼它位於/home/bitnami/apps/wordpress/htdocs。更新下列值:

    /** The name of the database for WordPress */
    define( 'DB_NAME', 'wordpress' );

    /** MySQL database username */
    define( 'DB_USER', 'admin' );

    /** MySQL database password */
    define( 'DB_PASSWORD', '[YourPassword]' );

    /** MySQL hostname */
    define( 'DB_HOST', '[RDS_ENDPOINT]' );

使用您自己的值替換占位符並保存文件。更改應立即生效。

完成。如果您有任何問題,請隨時聯繫我。

Removing .DS_Store Files from Git Repositories

If you are a Mac user who also uses Git, you might accidentally commit a .DS_Store file. This could confuse your Windows colleagues, who may wonder what these files do and why you've committed them.

First, what is a .DS_Store file? DS stands for Desktop Services, and these files are used by Macs to determine how to display folders when you open them. For example, they store custom attributes like the positions of icons. These files are created and maintained by the Finder application on a Mac, and are normally hidden.

These .DS_Store files are not useful to Windows users and do not need to be committed to your GitHub repository. To remove existing .DS_Store files from your repository, run the following command:

    find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Afterwards, commit the changes to remove those files and push to the remote repository. To prevent these files from being added again, edit your .gitignore file and add the following line:

    .DS_Store

Doing so will address any concerns your colleagues may have.

從 Git 倉庫中移除 .DS_Store 文件

如果你是一個同時使用 Mac 和 Git 的用戶,你可能會無意間提交 .DS_Store 文件。這可能會讓你的 Windows 同事感到困惑,他們可能不清楚這些文件的用途,也不明白你為何要提交它們。

首先,什麼是 .DS_Store 文件? DS 代表桌面服務(Desktop Services),這些文件被 Mac 用來確定當你打開它們時如何顯示文件夾。例如,它們存儲自定義屬性,如圖標的位置。這些文件由 Mac 的 Finder 應用程序創建並維護,並且通常是隱藏的。

這些 .DS_Store 文件對 Windows 用戶無益,也不需要提交到你的 GitHub 倉庫。要從你的倉庫中移除現有的 .DS_Store 文件,執行以下命令:

    find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

然後,提交更改以移除這些文件,並推送到遠程倉庫。為了防止這些文件再次被添加,編輯你的 .gitignore 文件,並添加以下行:

    .DS_Store

這樣做將避免你的同事產生任何疑慮。