Installing WordPress on Ubuntu 24.04

Installing WordPress on Ubuntu 24.04

在 Ubuntu 24.04 上安装 WordPress

WordPress is a free, open-source content management system (CMS) built with PHP and backed by MySQL or MariaDB. It began as a blogging tool and has since grown into one of the most widely used platforms for building websites, powering over 22% of the top one million websites as of December 2024.

WordPress 是一个免费的开源内容管理系统 (CMS),基于 PHP 构建,并由 MySQL 或 MariaDB 提供支持。它最初是一个博客工具,现已发展成为全球最广泛使用的网站构建平台之一。截至 2024 年 12 月,全球前一百万个网站中,超过 22% 的网站都在使用 WordPress。

This guide walks through installing and configuring WordPress on a server running Ubuntu 24.04 with the LEMP stack (Linux, Nginx, MySQL, PHP). By the end, you’ll have a hardened LEMP environment with WordPress deployed and ready to serve dynamic websites from your own server. Before you begin, you need access to an Ubuntu 24.04 instance as a non-root sudo user, and, if using a custom domain, a domain A record configured with your DNS provider pointing to your server IP (e.g., www.example.com).

本指南将引导您在运行 Ubuntu 24.04 的服务器上安装和配置 WordPress,并使用 LEMP 技术栈(Linux, Nginx, MySQL, PHP)。完成本指南后,您将拥有一个经过加固的 LEMP 环境,并成功部署 WordPress,准备好从您自己的服务器托管动态网站。在开始之前,您需要以非 root 的 sudo 用户身份访问 Ubuntu 24.04 实例;如果您使用自定义域名,则需要在 DNS 提供商处配置指向服务器 IP 的域名 A 记录(例如 www.example.com)。

1. Install Nginx Web Server

1. 安装 Nginx Web 服务器

  1. Install Nginx: $ sudo apt install nginx -y

  2. 安装 Nginx:$ sudo apt install nginx -y

  3. Start the Nginx service: $ sudo systemctl start nginx

  4. 启动 Nginx 服务:$ sudo systemctl start nginx

  5. Enable Nginx to start automatically on boot: $ sudo systemctl enable nginx

  6. 设置 Nginx 开机自启:$ sudo systemctl enable nginx

  7. Check the service status to verify Nginx is running: $ sudo systemctl status nginx

  8. 检查服务状态以验证 Nginx 是否正在运行:$ sudo systemctl status nginx


2. Configure the Firewall

2. 配置防火墙

Use UFW (Uncomplicated Firewall) to allow HTTP and HTTPS traffic. 使用 UFW (Uncomplicated Firewall) 来允许 HTTP 和 HTTPS 流量。

  1. Check the current firewall status: $ sudo ufw status

  2. 检查当前防火墙状态:$ sudo ufw status

  3. Allow both HTTP and HTTPS through the firewall using the Nginx Full profile: $ sudo ufw allow "Nginx Full"

  4. 使用 Nginx Full 配置文件允许 HTTP 和 HTTPS 通过防火墙:$ sudo ufw allow "Nginx Full"

  5. Reload UFW to apply the rule: $ sudo ufw reload

  6. 重载 UFW 以应用规则:$ sudo ufw reload

  7. Verify that the rule has been applied: $ sudo ufw status

  8. 验证规则是否已应用:$ sudo ufw status


3. Install MySQL Database

3. 安装 MySQL 数据库

  1. Install the MySQL server package: $ sudo apt install mysql-server -y

  2. 安装 MySQL 服务器包:$ sudo apt install mysql-server -y

  3. Verify that MySQL was installed successfully: $ mysql --version

  4. 验证 MySQL 是否安装成功:$ mysql --version

  5. Start the MySQL service to activate the database server: $ sudo systemctl start mysql

  6. 启动 MySQL 服务以激活数据库服务器:$ sudo systemctl start mysql

  7. Enable MySQL to start on boot: $ sudo systemctl enable mysql

  8. 设置 MySQL 开机自启:$ sudo systemctl enable mysql

  9. Verify that MySQL is running correctly by checking its service status: $ sudo systemctl status mysql

  10. 通过检查服务状态验证 MySQL 是否正常运行:$ sudo systemctl status mysql

  11. Harden your MySQL setup by running the built-in security script: $ sudo mysql_secure_installation

  12. 运行内置的安全脚本以加固 MySQL 设置:$ sudo mysql_secure_installation


4. Create a WordPress Database and User

4. 创建 WordPress 数据库和用户

WordPress requires a dedicated MySQL database to store its content, settings, and user information. WordPress 需要一个专用的 MySQL 数据库来存储其内容、设置和用户信息。

  1. Log in to the MySQL shell as the root user: $ sudo mysql

  2. 以 root 用户身份登录 MySQL shell:$ sudo mysql

  3. Create the WordPress database: mysql> CREATE DATABASE wordpressdb;

  4. 创建 WordPress 数据库:mysql> CREATE DATABASE wordpressdb;

  5. Create a dedicated user for WordPress with a strong password: mysql> CREATE USER 'wordpressdbuser'@'localhost' IDENTIFIED BY 'strongpassword';

  6. 为 WordPress 创建一个带有强密码的专用用户:mysql> CREATE USER 'wordpressdbuser'@'localhost' IDENTIFIED BY 'strongpassword';

  7. Grant full privileges to the new user on the WordPress database: mysql> GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wordpressdbuser'@'localhost';

  8. 授予新用户对 WordPress 数据库的完全权限:mysql> GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wordpressdbuser'@'localhost';

  9. Apply the changes by reloading the privileges: mysql> FLUSH PRIVILEGES;

  10. 通过重载权限来应用更改:mysql> FLUSH PRIVILEGES;

  11. Exit the MySQL shell: mysql> EXIT;

  12. 退出 MySQL shell:mysql> EXIT;


5. Install PHP and Extensions

5. 安装 PHP 及扩展

WordPress uses PHP to process dynamic content and connect to the MySQL database. WordPress 使用 PHP 来处理动态内容并连接到 MySQL 数据库。

  1. Install PHP and all required modules: $ sudo apt install php php-cli php-common php-imap php-fpm php-snmp php-xml php-zip php-mbstring php-curl php-mysqli php-gd php-intl -y

  2. 安装 PHP 及所有必需的模块:$ sudo apt install php php-cli php-common php-imap php-fpm php-snmp php-xml php-zip php-mbstring php-curl php-mysqli php-gd php-intl -y

  3. Verify the installed PHP version: $ php -v

  4. 验证已安装的 PHP 版本:$ php -v

  5. Confirm that php-fpm is running: $ sudo systemctl status php8.3-fpm

  6. 确认 php-fpm 正在运行:$ sudo systemctl status php8.3-fpm


6. Download and Install WordPress

6. 下载并安装 WordPress

  1. Download the latest WordPress release: $ wget http://wordpress.org/latest.tar.gz

  2. 下载最新的 WordPress 发行版:$ wget http://wordpress.org/latest.tar.gz

  3. Extract the archive: $ sudo tar -xvzf latest.tar.gz

  4. 解压归档文件:$ sudo tar -xvzf latest.tar.gz

  5. Move the contents to the Nginx web root: $ sudo mv wordpress/* /var/www/html/

  6. 将内容移动到 Nginx Web 根目录:$ sudo mv wordpress/* /var/www/html/

  7. Set ownership to the Nginx user: $ sudo chown -R www-data:www-data /var/www/html

  8. 将所有权设置为 Nginx 用户:$ sudo chown -R www-data:www-data /var/www/html

  9. Change to the web root: $ cd /var/www/html/

  10. 进入 Web 根目录:$ cd /var/www/html/

  11. Remove default Nginx placeholders: $ sudo rm index.html index.nginx-debian.html

  12. 删除默认的 Nginx 占位文件:$ sudo rm index.html index.nginx-debian.html

  13. Rename the sample config: $ sudo mv wp-config-sample.php wp-config.php

  14. 重命名示例配置文件:$ sudo mv wp-config-sample.php wp-config.php


Configure the WordPress wp-config.php File

配置 WordPress wp-config.php 文件

To allow WordPress to connect to your MySQL database, update the wp-config.php file with the database credentials created earlier. 为了允许 WordPress 连接到您的 MySQL 数据库,请使用之前创建的数据库凭据更新 wp-config.php 文件。

  1. Open the configuration file in a text editor: $ sudo nano wp-config.php

  2. 在文本编辑器中打开配置文件:$ sudo nano wp-config.php

  3. Find and update the following lines with your database information:

  4. 找到并使用您的数据库信息更新以下行:

// The name of the database for WordPress
define( 'DB_NAME', 'wordpressdb' );
// MySQL database username
define( 'DB_USER', 'wordpressdbuser' );
// MySQL database password
define( 'DB_PASSWORD', 'strongpassword' );