Skip to content

Home

使用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 )

Installing PHP 7.2 Instead of PHP 5.4 on Amazon Linux 2

Hello everyone, and welcome to another episode of Continuous Improvement, the podcast where we explore different solutions to common problems in the tech world. I'm your host, Victor. In today's episode, we'll be talking about a common issue that many of us face when setting up an Amazon Linux 2 AMI server and trying to install PHP.

And we're back. So, imagine this: you've just launched a brand new Amazon Linux 2 AMI server, and you're excited to get started. But as you try to install PHP using the usual command yum install php, there seems to be a problem. The version it installs is PHP 5.4.16, and you realize that the latest version is PHP 7.2. What do you do?

Well, fear not! There is a solution to this dilemma. You can enable PHP 7.2 via Amazon Linux Extras. Let me walk you through it.

First, open up your terminal and run the following command with sudo privileges:

sudo amazon-linux-extras enable php7.2

This command will enable PHP 7.2 through the Amazon Linux Extras. Once it's enabled, we can proceed with the installation.

But hold on, there's one more step before we install PHP 7.2. We need to clean the metadata. Run the following command:

yum clean metadata

This will ensure that we have the most up-to-date information for the installation process. Now, we can finally install PHP 7.2 along with some additional packages that we'll need. Run this command:

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

And that's it! You've successfully installed PHP 7.2 on your Amazon Linux 2 AMI server. To double-check, run php -v in your terminal, and you should see something like this:

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

And there you have it, a step-by-step solution to upgrade your PHP version on an Amazon Linux 2 AMI server. Remember, keeping your software up to date is essential for security and performance reasons.

Thank you for tuning in to this episode of Continuous Improvement. If you found this information helpful, please consider subscribing to our podcast and leaving us a review. If you have any suggestions for future topics or questions, feel free to reach out to us on our website or social media channels.

Until next time, I'm Victor, your host, signing off.

在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.

Reading Large Files Using Node.js

Hello everyone and welcome back to another episode of Continuous Improvement. I'm your host, Victor, and today we're going to tackle a common problem that many of us face when dealing with large datasets: reading massive log files.

Recently, I had to analyze a massive dataset consisting of log files and when I tried opening it in Excel, my trusty laptop simply froze. Frustrating, right? Luckily, I found a solution using Node.js that I want to share with you today.

So, let's dive into the problem. Imagine you have a small file and you want to read its content using a script. You might use something like this:

[Script excerpt: fs.readFile]

This script works perfectly fine for small files. However, when it comes to large files, you might encounter an error like the following:

[Script error excerpt]

Ouch, that's definitely not what we want. But fear not, there's a solution! Instead of using fs.readFile, we can leverage Node.js's native readline library to tackle larger files.

Here's how it works:

[Script excerpt: readline.createInterface]

With this approach, we create a readline interface for our large file, setting the input as the file stream and the output as the standard output. Then, we can use the line event to process each line of the file, and the pause event to indicate when we're done reading the file.

By processing the file line by line, you can perform various operations like parsing the lines into JSON or incrementing a counter. And once the file has been completely read, you can display the final results.

And there you have it! Using this Node.js readline approach, you can now process massive datasets without running into buffer errors. If you want to dive deeper, I highly recommend checking out the official documentation for the Node.js Readline API.

I hope you found this solution helpful. Remember, continuous improvement is all about finding better ways to solve problems and streamline our processes.

Thank you for tuning in to Continuous Improvement. I'm Victor, your host, and I'll see you in the next episode. Keep improving!

使用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.

Migrating WordPress MySQL Database to AWS RDS

Hello everyone, and welcome to another episode of Continuous Improvement. I'm your host, Victor, and today we have an exciting topic to discuss - migrating your local WordPress MySQL database to Amazon Web Services Relational Database Service, also known as RDS. If you've been considering this migration for improved performance, database scalability, and easier maintenance, then you've come to the right place. So let's dive right into it.

The first step is to navigate to the AWS console and choose RDS. From there, you'll need to create a MySQL database. Simply fill in the form as shown, including the DB Instance Identifier, Master Username, and Master Password. Most of the settings can be left at their default values, including the default VPC. If you're unsure, don't worry, we'll guide you through the process.

Next, it's time to back up your existing WordPress database. SSH into your WordPress instance and use the command mysqldump -u root -p [YourDatabaseName] > backup.sql to create a backup of your database. Remember to replace [YourDatabaseName] with the actual name of your database, such as bitnami_wordpress. This backup file will be crucial in the migration process.

Now that you've backed up your database, it's time to import it into your newly created AWS RDS instance. Use the command mysql -u admin -p -h [RDS_ENDPOINT] -D wordpress < backup.sql to import the backup. Just remember to replace admin and [RDS_ENDPOINT] with your own values. If you encounter any issues during this step, we've got your back.

In case you come across an error stating "ERROR 1049 (42000): Unknown database 'wordpress'," it means your wordpress database hasn't been created yet. Don't worry, it's an easy fix. Start by connecting to the database using mysql -h [RDS_ENDPOINT] --user=admin --password=[YourPassword]. Once connected, create a new database with the command mysql> CREATE DATABASE wordpress;. Make sure you exit MySQL after creating the database.

Lastly, you'll need to edit the wp-config.php file in your WordPress EC2 instance. This file is typically located in your WordPress directory, such as /home/bitnami/apps/wordpress/htdocs if you're using Bitnami. Update the values for DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST to match your newly created RDS instance. Once you've made the changes, save the file, and you're all set!

And that's it! You've successfully migrated your local WordPress MySQL database to AWS RDS. Now you can enjoy improved performance, scalability, and easier maintenance of your website. If you have any questions or need further assistance, feel free to reach out. We're here to help you every step of the way.

Thank you all for tuning in to this episode of Continuous Improvement. I hope you found this guide helpful and that it inspires you to take advantage of the benefits AWS RDS can offer for your WordPress database. Stay tuned for more episodes focused on helping you improve your workflows, technology, and more. I'm Victor, your host, signing off. Until next time!

將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]' );

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

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