在本页面上
- 前提条件
- 安装 LAMP 服务器
- 配置 MariaDB 数据库
- 下载 WordPress
- 为 WordPress 配置 Apache
- 使用 Let's Encrypt SSL 保护 WordPress
- 配置防火墙
- 访问 WordPress 后台
- 结论
WordPress 是一个免费、开源且完全用 PHP 构建的世界最流行的 CMS。全球有成千上万的人使用它来运行博客、商业网站和电子商务商店。它拥有一些很棒的功能,包括一个简单且易于使用的管理面板、数千个插件、庞大的社区、深入的主题设计、定制等等。
在本指南中,我们将向您展示如何在 AlmaLinux 8 上使用 Apache 和 Let's Encrypt SSL 安装 WordPress。
前提条件
- 一台运行 AlmaLinux 8 的服务器。
- 一个指向您服务器 IP 的有效域名。
- 在服务器上配置了 root 密码。
安装 LAMP 服务器
WordPress 需要在您的服务器上安装 LAMP 服务器。您可以使用以下命令安装它:
dnf install httpd mariadb-server php php-mysqlnd php-dom php-simplexml php-xml php-xmlreader php-curl php-exif php-ftp php-gd php-iconv php-json php-mbstring php-posix php-sockets php-tokenizer unzip -y
安装 LAMP 服务器后,启动 Apache 和 MariaDB 服务,并启用它们以在系统重启时启动:
systemctl start mariadb
systemctl start httpd
systemctl enable mariadb
systemctl enable httpd
完成后,您可以继续下一步。
配置 MariaDB 数据库
首先,您需要设置 MariaDB root 密码并保护 MariaDB 安装。您可以使用以下命令完成此操作:
mysql_secure_installation
如下所示回答所有问题:
Enter current password for root (enter for none): Set root password? [Y/n] Y New password: Re-enter new password: Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Y
完成后,使用以下命令登录 MariaDB:
mysql -u root -p
登录后,为 WordPress 创建一个数据库和用户:
MariaDB [(none)]> CREATE DATABASE wordpress;
MariaDB [(none)]> CREATE USER `wordpress`@`localhost` IDENTIFIED BY 'password';
接下来,使用下面的命令授予 WordPress 数据库所有权限:
MariaDB [(none)]> GRANT ALL ON wordpress.* TO `wordpress`@`localhost`;
接下来,使用以下命令刷新权限并退出 MariaDB shell:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
完成后,您可以继续下一步。
下载 WordPress
接下来,将目录更改为 Apache Web 根目录,并使用以下命令下载最新版本的 WordPress:
cd /var/www/html
wget https://wordpress.org/latest.tar.gz
下载完成后,使用以下命令解压下载的文件:
tar -xvzf latest.tar.gz
接下来,将目录更改为 wordpress 并重命名示例配置文件:
cd wordpress
mv wp-config-sample.php wp-config.php
接下来,使用以下命令编辑配置文件:
nano wp-config.php
如下所示定义您的数据库配置:
/** The name of the database for WordPress */ define( 'DB_NAME', 'wordpress' ); /** MySQL database username */ define( 'DB_USER', 'wordpress' ); /** MySQL database password */ define( 'DB_PASSWORD', 'password' ); /** MySQL hostname */ define( 'DB_HOST', 'localhost' );
完成后保存并关闭文件,然后为 WordPress 目录设置适当的权限:
chown -R apache:apache /var/www/html/wordpress
完成后,您可以继续下一步。
为 WordPress 配置 Apache
接下来,您需要为 WordPress 创建一个 Apache 虚拟主机配置文件。您可以使用以下命令创建它:
nano /etc/httpd/conf.d/wordpress.conf
添加以下行:
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/var/www/html/wordpress" ServerName wordpress.example.com ErrorLog "/var/log/httpd/example.com-error_log" CustomLog "/var/log/httpd/example.com-access_log" combined <Directory "/var/www/html/wordpress"> DirectoryIndex index.html index.php Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
保存并关闭文件,然后重新启动 Apache 服务以应用更改:
systemctl restart httpd
您可以使用以下命令检查 Apache 的状态:
systemctl status httpd
您应该得到以下输出:
? httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-07-09 03:30:47 EDT; 3s ago
Docs: man:httpd.service(8)
Main PID: 4153 (httpd)
Status: "Started, listening on: port 80"
Tasks: 213 (limit: 12524)
Memory: 24.5M
CGroup: /system.slice/httpd.service
??4153 /usr/sbin/httpd -DFOREGROUND
??4155 /usr/sbin/httpd -DFOREGROUND
??4156 /usr/sbin/httpd -DFOREGROUND
??4157 /usr/sbin/httpd -DFOREGROUND
??4158 /usr/sbin/httpd -DFOREGROUND
Jul 09 03:30:47 AlmaLinux systemd[1]: Stopped The Apache HTTP Server.
Jul 09 03:30:47 AlmaLinux systemd[1]: Starting The Apache HTTP Server...
完成后,您可以继续下一步。
使用 Let's Encrypt SSL 保护 WordPress
接下来,您需要安装 Certbot 客户端,为 WordPress 安装 Let's Encrypt SSL。您可以使用以下命令安装它:
dnf install epel-release -y
dnf install certbot python3-certbot-apache
接下来,使用以下命令为您的 lets 域名获取并安装 SSL 证书:
certbot --apache -d wordpress.example.com
您将被要求提供您的电子邮件地址并接受服务条款:
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator apache, Installer apache 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. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, 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 Account registered. Requesting a certificate for wordpress.example.com Performing the following challenges: http-01 challenge for wordpress.example.com Waiting for verification... Cleaning up challenges Deploying Certificate to VirtualHost /etc/httpd/conf.d/wordpress.conf Redirecting all traffic on port 80 to ssl in /etc/httpd/conf.d/wordpress.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://wordpress.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Subscribe to the EFF mailing list (email: [email protected] ). 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 certificate will expire on 2022-02-09. 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" - 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
配置防火墙
接下来,您需要通过 firewalld 允许端口 80 和 443。您可以使用以下命令允许它们:
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
接下来,重新加载 firewalld 以应用更改:
firewall-cmd --reload
完成后,您可以继续下一步。
访问 WordPress 后台
接下来,打开您的 Web 浏览器并使用 URL https://wordpress.example.com 访问 WordPress 管理面板。您将被重定向到以下页面:

选择您的语言并点击 Continue 。您应该会看到 WordPress 站点配置页面:

提供您的站点信息并点击 Install WordPress 按钮。安装完成后,您应该会看到以下页面:

点击 Login 按钮。您将被重定向到 WordPress 登录页面:

提供您的管理员用户名、密码并点击 Login 按钮。您应该在以下页面上看到 WordPress 后台:

结论
恭喜!您已在 AlmaLinux 8 上使用 Apache 和 Let's Encrypt SSL 成功安装 WordPress。您现在可以使用 WordPress 轻松创建自己的网站。如果您有任何问题,请随时问我。