Skip to content

Home

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.

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

Hello, and welcome to another episode of Continuous Improvement. I'm your host, Victor. Today, we're going to discuss a common issue that many WordPress users face when setting up their websites on AWS EC2 instances. Specifically, we'll dive into the problem of endless redirection loops and how to fix them. So, let's get started!

Imagine this. You set up your WordPress blog on two AWS EC2 instances located in different availability zones. To manage the traffic, you wisely configure an Elastic Load Balancer (ELB) to redirect all HTTP requests to HTTPS. However, you encounter a roadblock when your requests keep looping endlessly, resulting in an error stating "too many redirections." Frustrating, right?

Luckily, there's a simple solution to this problem. Let me walk you through it step by step. Here's what you need to do.

First, open up your wp-config.php file. This file holds important configurations for your WordPress website. Look for the following lines of code:

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

You'll notice that these lines specify the WP_SITEURL and WP_HOME values using HTTPS. While this seems like the correct approach, it actually creates the endless redirection loop that we're trying to solve.

So, here's the fix. Add the following line to your wp-config.php file:

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

By adding this line, you're explicitly telling WordPress that HTTPS is enabled. This bypasses the endless redirection loop issue and ensures a smooth user experience.

And there you have it! A simple solution to a potentially frustrating problem. Configurations like these can be challenging to troubleshoot, and you might spend hours searching for a solution. But with this fix, you'll save valuable time and get your website up and running smoothly.

I hope you found this episode helpful. If you have any other WordPress-related issues or questions, feel free to reach out to me on our podcast's website or social media channels. Remember, continuous improvement is all about learning, growing, and overcoming challenges one step at a time.

Thank you for listening to Continuous Improvement. I'm your host, Victor, signing off. Stay curious, keep improving, and until next time!

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