本页内容

  • 前置条件
  • 安装 Nginx、MariaDB 与 PHP
  • 为 WordPress 创建数据库
  • 在 Ubuntu 22.04 上安装 WordPress
  • 为 WordPress 创建 Nginx 虚拟主机
  • 完成 WordPress 网站安装
  • 在 WordPress 上启用 HTTPS
  • 结语

WordPress CMS 是一款免费、开源的内容管理系统,可让用户轻松创建和管理网站。它以灵活性、友好的用户界面,以及丰富的插件和主题库而著称,无论是初学者还是专业人士都非常适用。

WordPress 让网站的创建与维护变得简单。由于其受欢迎程度,如今超过三分之一的网站由 WordPress 驱动。它使用 PHP 编写,并以 MariaDB 和 MySQL 作为数据库后端。

在本教程中,我们将向您展示如何在 Ubuntu 22.04 上安装 WordPress,并搭配 Nginx 以及免费的 Let's Encrypt SSL 证书。

前置条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 一个已指向您服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

安装 Nginx、MariaDB 与 PHP

在开始之前,必须在您的服务器上安装 LEMP 服务器。如果尚未安装,可以运行以下命令进行安装:

apt install nginx mariadb-server php php-fpm php-curl php-mysql php-gd php-mbstring php-xml php-imagick php-zip php-xmlrpc -y

LEMP 服务器安装完成后,使用以下命令验证 PHP 版本:

php -v

您将在以下输出中看到 PHP 版本:

PHP 8.1.2 (cli) (built: Apr  7 2022 17:46:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

接下来,编辑 PHP 配置文件,并调整一些默认设置:

nano /etc/php/8.1/fpm/php.ini

修改以下几行:

cgi.fix_pathinfo=0
upload_max_filesize = 128M
post_max_size = 128M
memory_limit = 512M
max_execution_time = 120

完成后保存并关闭文件。

为 WordPress 创建数据库

WordPress 使用数据库来存储其内容。因此您需要为 WordPress 创建一个数据库和用户。

首先,使用以下命令登录 MariaDB 命令行:

mysql

登录后,使用以下命令创建数据库和用户:

MariaDB [(none)]> CREATE DATABASE wpdb;
MariaDB [(none)]> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'securepasssword';

接下来,使用以下命令授予 WordPress 数据库所有权限:

MariaDB [(none)]> GRANT ALL ON wpdb.* TO 'wpuser'@'localhost';

接下来,使用以下命令刷新权限并退出 MariaDB:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

完成后,您可以继续进行下一步。

在 Ubuntu 22.04 上安装 WordPress

首先,进入 Nginx 网站根目录,并使用以下命令下载最新版本的 WordPress:

cd /var/www/html
wget https://wordpress.org/latest.tar.gz

WordPress 下载完成后,使用以下命令解压下载的文件:

tar -zxvf latest.tar.gz

接下来,重命名 WordPress 示例配置文件。

mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

接下来,编辑 WordPress 配置文件,并设置您的数据库信息:

nano /var/www/html/wordpress/wp-config.php

按如下方式定义您的数据库设置:

define( 'DB_NAME', 'wpdb' );

/** Database username */
define( 'DB_USER', 'wpuser' );

/** Database password */
define( 'DB_PASSWORD', 'securepasssword' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

出于安全考虑,您还需要更新 wp-config 文件中的安全密钥。首先,前往此处生成密钥。然后按如下方式添加:

define('AUTH_KEY',         'Y$I,-gafVeR>Z-8qy&jQ62L}{R)e|lK/#RBh.Y#f+p-P*.8,,hP-iX[q3*tVP-fu');
define('SECURE_AUTH_KEY',  'D)k6o`D G%<()-zXP5{T{v2)Zgo-c+8T-Un=+R3%n/X2=MLDb5$O]UHA%gK| .WR');
define('LOGGED_IN_KEY',    'eL|5#`ul|;MrKm#q$KVl/ky(i}Jc;xrH(Eb|Hwzb/?-.RLSUSX2X[4HD:U:UOP:Y');
define('NONCE_KEY',        ']azQ+9f^#~l*r>uPMH5H>ck:?az4o[)*Txo:+MGjE5f&0kag3O9m85g3~VJ6YVWE');
define('AUTH_SALT',        'fAM5&`m4X+{+wSsF.!}-/8@Ce~~u%>}la1bCC,@#+R*t]uYf?[hph/>!Bw>v#oaQ');
define('SECURE_AUTH_SALT', '}|Z&dj_tFV2T$7y(O#O|bwwQ$sH6t!-zdE.MlOHLZ>4WDqG:_Qzn#Allm-UO1#7P');
define('LOGGED_IN_SALT',   'b9Uf~**E_xt@{KWknsAL^9D7Ix3CO.+PpFF~btd)-pG~pXPQ,[c&WRE-NgLG9~)|');
define('NONCE_SALT',       '}mTUi&.#i+YJT-TSrbIwqWO<]ut3K%CS~7g.} *NztVlgZDr`?>wxJ+_VW-D_zif');

完成后保存并关闭文件。接下来,为 WordPress 目录设置正确的权限和所有权:

chown -R www-data:www-data /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress

为 WordPress 创建 Nginx 虚拟主机

接下来,您需要创建一个 Nginx 虚拟主机配置文件,以便通过 Internet 提供 WordPress 服务。

nano /etc/nginx/conf.d/wordpress.conf

添加以下配置:

server {
    listen 80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  wordpress.example.com;

    client_max_body_size 500M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
	
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }	

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }	

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

保存并关闭文件,然后使用以下命令验证 Nginx 配置:

nginx -t

您将看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重启 Nginx 和 PHP-FPM 服务以应用更改。

systemctl restart nginx
systemctl restart php8.1-fpm

您也可以使用以下命令检查 Nginx 的状态:

systemctl status nginx

您将看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-05-05 11:36:28 UTC; 10s ago
       Docs: man:nginx(8)
    Process: 16880 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 16882 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 16883 (nginx)
      Tasks: 3 (limit: 4630)
     Memory: 3.4M
        CPU: 49ms
     CGroup: /system.slice/nginx.service
             ??16883 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??16884 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ??16885 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

May 05 11:36:28 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 05 11:36:28 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

完成 WordPress 网站安装

现在,打开您的网页浏览器,使用网址 http://wordpress.example.com 访问 WordPress 安装向导。您将被重定向到以下页面:

如何在 Ubuntu 22.04 上使用 Nginx 与 Let's Encrypt SSL 安装 WordPress

选择您的语言并点击“继续”按钮。您应该会看到 WordPress 站点配置页面:

如何在 Ubuntu 22.04 上使用 Nginx 与 Let's Encrypt SSL 安装 WordPress

填写您的网站名称、管理员用户名、密码和电子邮件,然后点击“安装 WordPress”按钮。WordPress 安装完成后,您应该会看到以下页面:

如何在 Ubuntu 22.04 上使用 Nginx 与 Let's Encrypt SSL 安装 WordPress

点击“登录”按钮。您应该会看到 WordPress 登录页面:

如何在 Ubuntu 22.04 上使用 Nginx 与 Let's Encrypt SSL 安装 WordPress

填写您的管理员用户名和密码,然后点击“登录”按钮。您应该会在以下页面看到 WordPress 仪表盘:

如何在 Ubuntu 22.04 上使用 Nginx 与 Let's Encrypt SSL 安装 WordPress

在 WordPress 上启用 HTTPS

要在您的站点上启用 HTTPS,您需要将 Certbot Let's Encrypt 客户端安装到系统中。可以运行以下命令进行安装:

apt-get install python3-certbot-nginx -y

Certbot 客户端安装完成后,运行以下命令为您的网站启用 HTTPS:

certbot --nginx -d wordpress.example.com

系统会要求您提供有效的电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 
[email protected]


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for wordpress.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/wordpress.conf

接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入 2 并按回车完成安装。您应该会看到以下输出:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/wordpress.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://wordpress.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=wordpress.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/wordpress.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/wordpress.example.com/privkey.pem
   Your cert will expire on 2023-02-08. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

结语

恭喜!您已成功在 Ubuntu 22.04 上安装了 WordPress,并搭配 Nginx 与 Let's Encrypt SSL。现在您可以安装自己喜欢的主题和插件,开始搭建您自己的网站了。