Skip to content

Home

修復WordPress插件安裝權限問題

問題

當我嘗試在WordPress中安裝插件時,遇到了以下錯誤:

安裝失敗:下載失敗。檔案串流的目標目錄不存在,或者無法寫入。

這個問題是由於內容文件夾的權限問題導致的。我已經以超級用戶(sudo su)的身份編輯了一些文件,但安裝需要ec2-user的寫入權限。

解決方案

假設你在AWS EC2實例上進行設定,並且已登入為ec2-user,並假設WordPress位於/var/www路徑中,執行以下命令以更改所有權:

sudo chown -R ec2-user:apache /var/www

改變所有權後,你現在應該可以成功地安裝插件。

Fix WordPress with All Pages Returning 404 Not Found

Problem

I encountered a strange issue with WordPress: while the homepage loaded properly, all the other pages failed to do so. Instead, an error page displayed the message:

Not Found

The requested URL was not found on this server.

However, since I had migrated the files from another server, all the pages should already exist. I suspected that the .htaccess file might be the culprit, but after hours of troubleshooting, I still had no clue.

Solution

As it turns out, in my case, the .htaccess file was correctly configured. The issue lay elsewhere. To resolve it, edit the httpd.conf file:

    sudo vi /etc/httpd/conf/httpd.conf

Locate the section that starts with:

    <Directory "/var/www/html">

Change the configuration from AllowOverride None to:

    AllowOverride All

Finally, restart the server:

    sudo systemctl restart httpd

After doing this, all the pages should render properly.

修復WordPress,所有頁面都返回404未找到

問題

我遇到了一個關於WordPress的奇怪問題:首頁可以正常加載,但所有其他頁面都無法做到。相反,錯誤頁面顯示了這樣的信息:

未找到

找不到請求的URL在此服務器上。

然而,由於我已經將檔案從另一台服務器遷移過來,所以所有頁面都應該已經存在。我懷疑.htaccess檔案可能是問題所在,但經過幾個小時的故障排除,我仍然沒有線索。

解決方案

事實證明,在我的情況下,.htaccess檔案配置正確。問題在於其他地方。要解決它,编辑httpd.conf檔案:

    sudo vi /etc/httpd/conf/httpd.conf

找到以以下開頭的部分:

    <Directory "/var/www/html">

將配置從AllowOverride None改為:

    AllowOverride All

最後,重啟服務器:

    sudo systemctl restart httpd

做完這些,所有頁面都應該可以正常顯示。

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.