A previous version of this tutorial was written by Justin Ellingwood
本教程的先前版本由Justin Ellingwood撰写
介绍 (Introduction)
WordPress is the most popular CMS (content management system) on the internet. It’s a great choice for getting a website up and running quickly, and after the initial setup, almost all administration can be done through the web frontend.
WordPress是互联网上最流行的CMS(内容管理系统)。 这是使网站快速启动和运行的绝佳选择,并且在初始设置后,几乎所有管理都可以通过Web前端完成。
WordPress is designed to pull content – including posts, comments, user profiles, and other data – from a database backend. As a website grows and must satisfy more and more traffic, it can eventually outgrow its initial database. To resolve this, one can scale up their database by migrating their data to a machine with more RAM or CPU, but this is a tedious process that runs the risk of data loss or corruption. This is why some WordPress developers choose to build their websites on managed databases, which allow users to scale their database automatically with a far lower risk of data loss.
WordPress旨在从数据库后端提取内容,包括帖子,评论,用户个人资料和其他数据。 随着网站的发展,并且必须满足越来越多的流量,它最终可能超出其初始数据库的范围。 要解决此问题,可以通过将其数据迁移到具有更多RAM或CPU的计算机上来扩展其数据库,但这是一个繁琐的过程,存在数据丢失或损坏的风险。 这就是为什么某些WordPress开发人员选择在托管数据库上构建网站的原因,该网站使用户可以自动扩展数据库,而数据丢失的风险要低得多。
In this guide, we’ll focus on setting up a WordPress instance with a managed MySQL database and an Ubuntu 18.04 server. This will require you to install PHP and Apache to serve the content over the web.
在本指南中,我们将重点介绍使用托管的MySQL数据库和Ubuntu 18.04服务器设置WordPress实例。 这将需要您安装PHP和Apache才能通过Web提供内容。
先决条件 (Prerequisites)
In order to complete this tutorial, you will need:
为了完成本教程,您将需要:
Access to an Ubuntu 18.04 server: This server should have a non-root sudo-enabled user and a firewall configured. You can set this up by following our Ubuntu 18.04 initial server setup guide.
访问一个Ubuntu 18.04服务器 :此服务器应该有非根启用sudo的用户,并配置了防火墙。 您可以按照我们的Ubuntu 18.04初始服务器设置指南进行设置 。
A managed MySQL database: To provision a Managed MySQL Database from DigitalOcean, see our Managed Databases product documentation. Note that this guide will refer to DigitalOcean Managed Databases in examples, but the instructions provided here should also generally work for managed MySQL databases from other cloud providers.
托管MySQL数据库 :要从DigitalOcean供应托管MySQL数据库,请参阅我们的托管数据库产品文档 。 请注意,本指南将在示例中引用DigitalOcean托管数据库,但此处提供的说明通常也应适用于其他云提供商的托管MySQL数据库。
A LAMP stack installed on your server: In addition to a database, WordPress requires a web server and PHP to function correctly. Setting up a complete LAMP stack (Linux, Apache, MySQL, and PHP) fulfills all of these requirements. Follow this guide to install and configure this software. As you follow this guide, make sure that you set up a virtual host to point to a domain name that you own. Additionally, be sure to skip Step 2, as installing
mysql-server
on your machine will make your managed database instance redundant.服务器上安装的LAMP堆栈 :除了数据库之外,WordPress还需要Web服务器和PHP才能正常运行。 设置完整的LAMP堆栈(Linux,Apache,MySQL和PHP)可以满足所有这些要求。 按照本指南安装和配置此软件。 在遵循本指南的过程中,请确保将虚拟主机设置为指向您拥有的域名。 另外,请确保跳过步骤2 ,因为在您的计算机上安装
mysql-server
将使您的托管数据库实例冗余。TLS/SSL security implemented for your site: If you have a domain name, the easiest way to secure your site is with Let’s Encrypt, which provides free, trusted certificates. Follow our Let’s Encrypt guide for Apache to set this up. Note that this will also require you to obtain a domain name and set up DNS records on your server. Follow this introduction to DigitalOcean DNS for details on how to configure this. Altneratively, if you don’t have a domain name, you use a self-signed certificate for your site.
为您的站点实施的TLS / SSL安全性 :如果您拥有域名,最简单的保护站点安全的方法就是使用我们提供免费,受信任证书的加密技术。 请遵循我们的Apache Let's Encrypt指南进行设置。 请注意,这还需要您获取域名并在服务器上设置DNS记录。 请遵循DigitalOcean DNS简介,以获取有关如何配置它的详细信息。 替代地,如果您没有域名,则可以对网站使用自签名证书 。
When you are finished with the setup steps, log into your server as your non-root user and continue below.
完成设置步骤后,以非root用户身份登录服务器,然后继续进行以下操作。
第1步–添加MySQL软件存储库并安装mysql-client
(Step 1 – Adding the MySQL Software Repository and Installing mysql-client
)
In order to configure your managed MySQL instance, you will need to install a client that will allow you to access the database from your server. This step will walk you through the process of installing the mysql-client
package.
为了配置托管MySQL实例,您将需要安装一个客户端,该客户端将允许您从服务器访问数据库。 此步骤将引导您完成安装mysql-client
软件包的过程。
In many cases, you can just install mysql-client
with the apt
command, but if you’re using the default Ubuntu repositories this will install version 5.7 of the program. In order to access a DigitalOcean Managed MySQL database, you will need to install version 8.0 or above. To do so, you must first add the MySQL software repository before installing the package.
在许多情况下,您可以使用apt
命令安装mysql-client
,但是如果您使用默认的Ubuntu存储库,则将安装程序的5.7版。 为了访问DigitalOcean托管MySQL数据库,您将需要安装8.0或更高版本。 为此,必须先添加MySQL软件存储库,然后再安装该软件包。
Begin by navigating to the MySQL APT Repository page in your web browser. Find the Download button in the lower-right corner and click through to the next page. This page will prompt you to log in or sign up for an Oracle web account. You can skip that and instead look for the link that says No thanks, just start my download. Right-click the link and select Copy Link Address (this option may be worded differently, depending on your browser).
首先,在Web浏览器中导航至“ MySQL APT存储库”页面 。 在右下角找到“ 下载”按钮,然后单击进入下一页。 该页面将提示您登录或注册Oracle Web帐户。 您可以跳过该链接,而是寻找显示“ 不,谢谢”的链接,只需开始下载即可 。 右键单击链接,然后选择“ 复制链接地址” (此选项的措词可能有所不同,具体取决于您的浏览器)。
Now you’re ready to download the file. On your server, move to a directory you can write to:
现在您可以下载文件了。 在服务器上,移至您可以写入以下目录:
- cd /tmp cd / tmp
Download the file using curl
, remembering to paste the address you just copied in place of the highlighted portion of the following command. You also need to pass two command line flags to curl
. -O
instructs curl
to output to a file instead of standard output. The L
flag makes curl
follow HTTP redirects, which is necessary in this case because the address you copied actually redirects to another location before the file downloads:
使用curl
下载文件,记住记住将刚复制的地址粘贴到以下命令的突出显示部分。 您还需要传递两个命令行标志以进行curl
。 -O
指示curl
输出到文件而不是标准输出。 L
标志使curl
遵循HTTP重定向,在这种情况下这是必需的,因为您复制的地址实际上在文件下载之前重定向到另一个位置:
curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
The file should now be downloaded in your current directory. List the files to make sure:
现在应将文件下载到当前目录中。 列出文件以确保:
- ls ls
You will see the filename listed in the output:
您将在输出中看到列出的文件名:
Output
mysql-apt-config_0.8.13-1_all.deb
. . .
Now you can add the MySQL APT repository to your system’s repository list. The dpkg
command is used to install, remove, and inspect .deb
software packages. The following command includes the -i
flag, indicating that you’d like to install from the specified file:
现在,您可以将MySQL APT存储库添加到系统的存储库列表中。 dpkg
命令用于安装,删除和检查.deb
软件包。 以下命令包含-i
标志,指示您要从指定的文件安装:
- sudo dpkg -i mysql-apt-config* 须藤dpkg -i mysql-apt-config *
During the installation, you’ll be presented with a configuration screen where you can specify which version of MySQL you’d prefer, along with an option to install repositories for other MySQL-related tools. The defaults will add the repository information for the latest stable version of MySQL and nothing else. This is what we want, so use the down arrow to navigate to the Ok
menu option and hit ENTER
.
在安装过程中,将为您提供一个配置屏幕,您可以在其中指定所需MySQL版本,并可以选择安装其他MySQL相关工具的存储库。 默认值将添加最新稳定版本MySQL的存储库信息,仅此而已。 这就是我们想要的,因此请使用向下箭头导航至“ Ok
菜单选项,然后按ENTER
。
Following that, the package will finish adding the repository. Refresh your apt
package cache to make the new software packages available:
之后,软件包将完成添加存储库。 刷新您的apt
软件包缓存以使新软件包可用:
- sudo apt update sudo apt更新
Next, you can clean up your system a bit and delete the file you downloaded, as you won’t need it in the future:
接下来,您可以稍微清理一下系统并删除下载的文件,因为以后将不再需要它:
- rm mysql-apt-config* rm mysql-apt-config *
Note: If you ever need to update the configuration of these repositories, just run the following command to select your new options:
注意:如果您需要更新这些存储库的配置,只需运行以下命令以选择新选项:
- sudo dpkg-reconfigure mysql-apt-config 须藤dpkg重新配置mysql-apt-config
After selecting your new options, run the following command to refresh your package cache:
选择新选项后,运行以下命令以刷新程序包缓存:
- sudo apt update sudo apt更新
Now that you’ve added the MySQL repositories, you’re ready to install the actual MySQL client software. Do so with the following apt
command:
现在,您已经添加了MySQL存储库,现在可以安装实际MySQL客户端软件了。 使用以下apt
命令执行此操作:
- sudo apt install mysql-client sudo apt安装mysql客户端
Once that command finishes, check the software version number to ensure that you have the latest release:
该命令完成后,请检查软件版本号以确保您具有最新版本:
- mysql --version mysql --version
Output
mysql Ver 8.0.17-cluster for Linux on x86_64 (MySQL Community Server - GPL)
You’re now able to connect to your managed database and begin preparing it to function with WordPress.
现在,您可以连接到托管数据库,并开始准备使其与WordPress一起使用。
第2步–为WordPress创建MySQL数据库和用户 (Step 2 – Creating a MySQL Database and User for WordPress)
WordPress uses MySQL to manage and store site and user information. Assuming you have completed all the prerequisite tutorials, you will have already provisioned a managed MySQL instance. Here, we’ll take the preparatory step of creating a database and a user for WordPress to use.
WordPress使用MySQL来管理和存储站点和用户信息。 假设您已经完成了所有先决条件教程 ,那么您将已经配置了托管MySQL实例。 在这里,我们将采取准备步骤,为WordPress使用创建数据库和用户。
Most managed database providers provide a uniform resource identifier (URI) used for connecting to the database instance. If you’re using a DigitalOcean Managed Database, you can find the relevant connection information in your Cloud Control Panel.
大多数托管数据库提供程序都提供用于连接数据库实例的统一资源标识符 (URI)。 如果您使用的是DigitalOcean托管数据库,则可以在“云控制面板”中找到相关的连接信息。
First, click Databases in the left-hand sidebar menu and select the MySQL database you want to use for your WordPress installation. Scroll down to the Connection Details section and copy the link in the host field. Then paste this link into the following command, replacing host_uri
with the information you just copied. Likewise, copy the port number in the port field – which will be 25060
on a DigitalOcean Managed Database – and replace port
with that number. Additionally, if this is your first time connecting to your managed database and you’ve not created your own administrative MySQL user, copy the value in the username field and paste it into the command, replacing user
:
首先,在左侧边栏菜单中单击数据库 ,然后选择要用于WordPress安装MySQL数据库。 向下滚动到“ 连接详细信息”部分,然后在主机字段中复制链接。 然后将此链接粘贴到以下命令中,将host_uri
替换为刚刚复制的信息。 同样,在端口字段中复制端口号(在DigitalOcean托管数据库上将为25060
,然后用该port
号替换port
。 此外,如果这是您第一次连接到您管理的数据库,你还没有建立自己的管理MySQL用户,在用户名字段复制值,并将其粘贴到命令,免去user
:
mysql -u user -p -h host_uri -P port
mysql -u 用户 -p -h host_uri -P 端口
This command includes the -p
flag, which will prompt you for the password of the MySQL user you specified. For a DigitalOcean Managed Database’s default doadmin user, you can find this by clicking the show link in the Connection Details section to reveal the password. Copy and paste it into your terminal when prompted.
该命令包括-p
标志,它将提示您输入指定MySQL用户的密码。 对于DigitalOcean托管数据库的默认doadmin用户,可以通过单击“ 连接详细信息”部分中的显示链接以显示密码来找到此用户。 出现提示时,将其复制并粘贴到您的终端中。
Note: If you are not using a DigitalOcean Managed Database, your connection options may differ. If that’s the case, you should consult your provider’s documentation for instructions on connecting third party applications to your database.
注意:如果您不使用DigitalOcean托管数据库,则连接选项可能会有所不同。 如果是这种情况,则应查阅提供商的文档,以获取有关将第三方应用程序连接到数据库的说明。
From the MySQL prompt, create a new database that WordPress will control. You can call this whatever you would like, but we will use the name wordpress in this guide to keep it simple. Create the database for WordPress by typing:
在MySQL提示符下,创建一个由WordPress控制的新数据库。 您可以随意命名,但在本指南中我们将使用wordpress这个名称来简化它。 通过键入以下内容为WordPress创建数据库:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
创建数据库wordpress默认字符集utf8 COLLATE utf8_unicode_ci;
Note: Every MySQL statement must end in a semi-colon (;
). Check to make sure this is present if you are running into any issues.
注意:每个MySQL语句必须以分号( ;
)结尾。 如果遇到任何问题,请检查以确保存在此问题。
Next, create a new MySQL user account that you will use exclusively to operate on the new database. Creating single-purpose databases and accounts is a good idea from a management and security standpoint. We will use the name wordpressuser in this guide, but feel free to change this if you’d like.
接下来,创建一个新MySQL用户帐户,该帐户将专用于在新数据库上进行操作。 从管理和安全的角度来看,创建单一用途的数据库和帐户是一个好主意。 我们将在本指南中使用wordpressuser这个名称,但是如果您愿意,可以随时更改它。
Run the following command, but replace your_server_ip
with your Ubuntu server’s IP address. Be aware, though, that this will limit wordpressuser to only be able to connect from your LAMP server; if you plan to manage WordPress from your local computer, you should enter that machine’s IP address instead. Additionally, be sure to choose a strong password for your database user.
运行以下命令,但将your_server_ip
替换为Ubuntu服务器的IP地址。 但是请注意,这将限制wordpressuser仅能够从您的LAMP服务器进行连接。 如果您打算从本地计算机管理WordPress,则应输入该计算机的IP地址。 此外,请确保为数据库用户选择一个强密码。
Notice that this command specifies that wordpressuser will use the mysql_native_password
plugin to authenticate. In MySQL 8.0 and later, the default authentication plugin is caching_sha2_password
, which is generally considered to be more secure than mysql_native_password
. As of this writing, though, PHP does not support caching_sha2_password
, which is why we specify mysql_native_password
in this command:
请注意,此命令指定wordpressuser将使用mysql_native_password
插件进行身份验证。 在MySQL 8.0和更高版本中,默认的身份验证插件为caching_sha2_password
,通常认为它比mysql_native_password
更安全。 但是,在撰写本文时,PHP不支持caching_sha2_password
,这就是为什么我们在此命令中指定mysql_native_password
的原因:
CREATE USER 'wordpressuser'@your_server_ip IDENTIFIED WITH mysql_native_password BY 'password';
使用mysql_native_password创建用户' wordpressuser '@ your_server_ip ID' 密码 ';
Note: If you do not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.
注意 :如果您不知道服务器的公共IP地址是什么,可以通过多种方法找到它。 通常,这是您用于通过SSH连接到服务器的地址。
One method is to use the curl
utility to contact an outside party to tell you how it sees your server. For example, you can use curl
to contact an IP-checking tool like ICanHazIP:
一种方法是使用curl
实用程序与外部curl
联系,以告诉您它如何看待服务器。 例如,您可以使用curl
来联系诸如ICanHazIP的IP检查工具:
- curl http://icanhazip.com 卷曲http://icanhazip.com
This command will return your server’s public IP address in your output.
此命令将在输出中返回服务器的公共IP地址。
Then grant this user access to the database you just created. Do so by running the following command:
然后,向该用户授予对您刚创建的数据库的访问权限。 通过运行以下命令来这样做:
GRANT ALL ON wordpress.* TO 'wordpressuser'@your_server_ip;
授予对wordpress的所有许可 。* TO'wordpressuser '@ your_server_ip ;
You now have a database and user account, each made specifically for WordPress. Go ahead and exit out of MySQL by typing:
现在,您有一个数据库和用户帐户,每个帐户和帐户都是专门为WordPress创建的。 继续并输入以下内容退出MySQL:
- exit 出口
That takes care of configuring your managed MySQL database to function with WordPress. In the next step, you will install a few PHP extensions in order to get more functionality out of the CMS.
这需要配置托管MySQL数据库以使其与WordPress一起使用。 在下一步中,您将安装一些PHP扩展,以从CMS中获得更多功能。
步骤3 –安装其他PHP扩展 (Step 3 – Installing Additional PHP Extensions)
Assuming you followed the prerequisite LAMP stack tutorial, you will have installed a few extensions intended to get PHP to properly communicate with MySQL. WordPress and many of its plugins leverage additional PHP extensions to add additional functionalities.
假设您已遵循了前提条件LAMP堆栈教程 ,则将安装一些扩展,以使PHP能够与MySQL正确通信。 WordPress及其许多插件利用附加PHP扩展来添加附加的功能。
To download and install some of the more popular PHP extensions for use with WordPress, run the following command:
要下载并安装一些更流行PHP扩展以用于WordPress,请运行以下命令:
- sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip sudo apt安装php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
Note: Each WordPress plugin has its own set of requirements. Some may require you to install additional PHP packages. Check your plugin documentation to see which extensions it requires. If they are available, they can be installed with apt
as demonstrated above.
注意:每个WordPress插件都有其自己的一组要求。 有些可能需要您安装其他PHP软件包。 查看您的插件文档,以了解它需要哪些扩展。 如果可用,则可以如上所述安装apt
。
You will restart Apache to load these new extensions in the next section. If you’re returning here to install additional plugins, though, you can restart Apache now by typing:
在下一部分中,您将重新启动Apache以加载这些新扩展。 不过,如果您要返回此处以安装其他插件,则可以输入以下命令立即重新启动Apache:
- sudo systemctl restart apache2 sudo systemctl重新启动apache2
Otherwise, continue on to Step 4.
否则,请继续执行步骤4。
步骤4 –调整Apache的配置以允许.htaccess覆盖和重写 (Step 4 – Adjusting Apache’s Configuration to Allow for .htaccess Overrides and Rewrites)
In order for Apache to be able to properly serve your WordPress installation, you must make a few minor adjustments to your Apache configuration.
为了使Apache能够正确地为您的WordPress安装提供服务,您必须对Apache配置进行一些小的调整。
If you followed the prerequisite tutorials, you should already have a configuration file for your site in the /etc/apache2/sites-available/
directory. We’ll use /etc/apache2/sites-available/your_domain.conf
as an example here, but you should substitute the path to your configuration file where appropriate.
如果您遵循了先决条件教程,则应该在/etc/apache2/sites-available/
目录中已经具有站点的配置文件。 我们将在此处使用/etc/apache2/sites-available/ your_domain .conf
作为示例, 但是您应该在适当的地方替换配置文件的路径 。
Additionally, we will use /var/www/your_domain
as the root directory in this example WordPress install. You should use the web root specified in your own configuration.
此外,在此示例WordPress安装中,我们将/var/www/ your_domain
用作根目录。 您应该使用自己的配置中指定的Web根目录 。
Note: It’s possible you are using the 000-default.conf
default configuration (with /var/www/html
as your web root). This is fine to use if you’re only going to host one website on this server. If not, it’s best to split the necessary configuration into logical chunks, one file per site.
注意:可能您正在使用000-default.conf
默认配置(以/var/www/html
作为您的Web根)。 如果您只打算在此服务器上托管一个网站,则可以使用此方法。 如果没有,最好将必要的配置分成逻辑块,每个站点一个文件。
Currently, the use of .htaccess
files is disabled. WordPress and many WordPress plugins use these files extensively for in-directory tweaks to the web server’s behavior.
当前,禁用.htaccess
文件的使用。 WordPress和许多WordPress插件广泛使用这些文件对Web服务器的行为进行目录内调整。
Open the Apache configuration file for your website:
打开您的网站的Apache配置文件:
sudo nano /etc/apache2/sites-available/your_domain.conf
须藤纳米/ etc / apache2 / sites-available / your_domain.conf
To allow .htaccess
files, you need to set the AllowOverride
directive within a Directory
block pointing to your document root. Add the following block of text inside the VirtualHost
block in your configuration file, being sure to use the correct web root directory:
若要允许.htaccess
文件,您需要在指向您的文档根Directory
块中设置AllowOverride
指令。 在配置文件的VirtualHost
块内添加以下文本块,确保使用正确的Web根目录:
<Directory /var/www/your_domain>
AllowOverride All
</Directory>
When you are finished, save and close the file.
完成后,保存并关闭文件。
Next, enable mod_rewrite
so that you can employ the WordPress permalink feature:
接下来,启用mod_rewrite
以便您可以使用WordPress永久链接功能:
- sudo a2enmod rewrite sudo a2enmod重写
Before implementing the changes you’ve just made, check to make sure there aren’t any syntax errors in your configuration file:
在实施刚刚进行的更改之前,请检查以确保配置文件中没有任何语法错误:
- sudo apache2ctl configtest 须藤apache2ctl configtest
The output might have a message that looks like this:
输出中可能会显示一条消息,如下所示:
Output
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
If you wish to suppress the top line, just add a ServerName
directive to your main (global) Apache configuration file at /etc/apache2/apache2.conf
. The ServerName
can be your server’s domain or IP address. However, this is just a message; it doesn’t affect the functionality of your site and as long as the output contains Syntax OK
, you’re all set to continue.
如果要取消显示第一行,只需在/etc/apache2/apache2.conf
主(全局)Apache配置文件中添加ServerName
指令。 ServerName
可以是服务器的域或IP地址。 但是,这只是一条消息。 它不会影响您网站的功能,只要输出包含Syntax OK
,您就可以继续。
Restart Apache to implement the changes:
重新启动Apache以实施更改:
- sudo systemctl restart apache2 sudo systemctl重新启动apache2
With that, you’re ready to download and set up WordPress itself.
这样,您就可以下载并设置WordPress本身了。
步骤5 –下载WordPress (Step 5 – Downloading WordPress)
Now that your server software is configured, you can install and configure WordPress. For security reasons, it is always recommended to get the latest version of WordPress from their site.
现在已经配置了服务器软件,您可以安装和配置WordPress。 出于安全原因,始终建议从其站点获取最新版本的WordPress。
First, navigate to into a writable directory. /tmp
will work for the purposes of this step:
首先,导航到可写目录。 /tmp
可用于此步骤:
- cd /tmp cd / tmp
Then download the compressed release by typing:
然后通过键入以下内容下载压缩版本:
- curl -O https://wordpress.org/latest.tar.gz curl -O https://wordpress.org/latest.tar.gz
Extract the compressed file to create the WordPress directory structure:
解压缩压缩文件以创建WordPress目录结构:
- tar xzvf latest.tar.gz tar xzvf Latest.tar.gz
You will move these files into your document root momentarily. Before doing so, add a dummy .htaccess
file so that this will be available for WordPress to use later.
您将立即将这些文件移到文档的根目录中。 在这样做之前,添加一个虚拟的.htaccess
文件,以便WordPress可以稍后使用它。
Create the file by typing:
通过键入以下内容来创建文件:
- touch /tmp/wordpress/.htaccess 触摸/tmp/wordpress/.htaccess
Also, copy over the sample configuration file to the filename that WordPress actually reads:
另外,将示例配置文件复制到WordPress实际读取的文件名:
- cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
Create an upgrade
directory, so that WordPress won’t run into permissions issues when trying to do this on its own following an update to its software:
创建一个upgrade
目录,以使WordPress在对其软件进行更新后尝试自行执行此操作时不会遇到权限问题:
- mkdir /tmp/wordpress/wp-content/upgrade mkdir / tmp / wordpress / wp-content / upgrade
Then copy the entire contents of the directory into your document root. The following command uses a period at the end of the source directory to indicate that everything within the directory should be copied, including hidden files (like the .htaccess
file you just created):
然后将目录的全部内容复制到文档根目录中。 以下命令在源目录的末尾使用句点来指示应复制目录中的所有内容,包括隐藏文件(如您刚刚创建的.htaccess
文件):
sudo cp -a /tmp/wordpress/. /var/www/your_domain
须藤cp -a / tmp / wordpress /。 / var / www / your_domain
That takes care of downloading WordPress onto your server. At this point, though, you still won’t be able to access the WordPress setup interface in your browser. To fix that, you’ll need to make a few changes to your server’s WordPress configuration.
这样就可以将WordPress下载到您的服务器上。 不过,此时您仍然无法在浏览器中访问WordPress设置界面。 要解决此问题,您需要对服务器的WordPress配置进行一些更改。
第6步–配置WordPress目录 (Step 6 – Configuring the WordPress Directory)
Before going through the web-based WordPress setup, you need to adjust some items in your WordPress directory. One important configuration change involves setting up reasonable file permissions and ownership.
在进行基于Web的WordPress设置之前,您需要调整WordPress目录中的某些项目。 一项重要的配置更改涉及设置合理的文件许可权和所有权。
Start by giving ownership of all the files to the www-data user and group. This is the user that the Apache web server runs as on Debian and Ubuntu systems, and Apache will need to be able to read and write WordPress files in order to serve the website and perform automatic updates.
首先,将所有文件的所有权提供给www-data用户和组。 这是Apache Web服务器在Debian和Ubuntu系统上运行的用户,并且Apache将需要能够读写WordPress文件才能为网站提供服务并执行自动更新。
Update the ownership of your web root directory with chown
:
使用chown
更新您的Web根目录的所有权:
sudo chown -R www-data:www-data /var/www/your_domain
须藤chown -R www-data:www-data / var / www / your_domain
Next run the following two find
commands to set the correct permissions on the WordPress directories and files:
接下来,运行以下两个find
命令来对WordPress目录和文件设置正确的权限:
sudo find /var/www/your_domain/ -type d -exec chmod 750 {} \;
sudo查找/ var / www / your_domain / -type d -exec chmod 750 {} \;
sudo find /var/www/your_domain/ -type f -exec chmod 640 {} \;
sudo查找/ var / www / your_domain / -type f -exec chmod 640 {} \;
These should be a reasonable permissions set to start with. Be aware, though, that some plugins and procedures might require additional updates.
这些应该是一个合理的权限设置。 但是请注意,某些插件和过程可能需要其他更新。
Now, you need to make some changes to the main WordPress configuration file.
现在,您需要对主WordPress配置文件进行一些更改。
When you open the file, the first order of business will be to replace some secret keys to provide security for your installation. WordPress provides a secure generator for these values so that you do not have to try to come up with good values on your own. These are only used internally, so it won’t hurt usability to have complex, secure values here.
当您打开文件时,首要任务是替换一些秘密密钥,以为安装提供安全性。 WordPress为这些值提供了安全的生成器,因此您不必自己尝试提出好的值。 这些仅在内部使用,因此在这里拥有复杂,安全的值不会损害可用性。
To grab secure values from the WordPress secret key generator, run the following command:
要从WordPress密钥生成器获取安全值,请运行以下命令:
- curl -s https://api.wordpress.org/secret-key/1.1/salt/ curl -s https://api.wordpress.org/secret-key/1.1/salt/
You will get back unique values that look something like this:
您将获得看起来像这样的唯一值:
Warning! It is important that you request unique values each time. Do NOT copy the values shown here!
警告! 每次都要求唯一值很重要。 不要复制此处显示的值!
Output
define('AUTH_KEY', '1jl/vqfs<XhdXoAPz9 DO NOT COPY THESE VALUES c_j{iwqD^<+c9.k<J@4H');
define('SECURE_AUTH_KEY', 'E2N-h2]Dcvp+aS/p7X DO NOT COPY THESE VALUES {Ka(f;rv?Pxf})CgLi-3');
define('LOGGED_IN_KEY', 'W(50,{W^,OPB%PB<JF DO NOT COPY THESE VALUES 2;y&,2m%3]R6DUth[;88');
define('NONCE_KEY', 'll,4UC)7ua+8<!4VM+ DO NOT COPY THESE VALUES #`DXF+[$atzM7 o^-C7g');
define('AUTH_SALT', 'koMrurzOA+|L_lG}kf DO NOT COPY THESE VALUES 07VC*Lj*lD&?3w!BT#-');
define('SECURE_AUTH_SALT', 'p32*p,]z%LZ+pAu:VY DO NOT COPY THESE VALUES C-?y+K0DK_+F|0h{!_xY');
define('LOGGED_IN_SALT', 'i^/G2W7!-1H2OQ+t$3 DO NOT COPY THESE VALUES t6**bRVFSD[Hi])-qS`|');
define('NONCE_SALT', 'Q6]U:K?j4L%Z]}h^q7 DO NOT COPY THESE VALUES 1% ^qUswWgn+6&xqHN&%');
These are configuration lines that you can paste directly into your configuration file to set secure keys. Copy the output you received now.
这些配置行可以直接粘贴到配置文件中以设置安全密钥。 复制您现在收到的输出。
Then, open the WordPress configuration file:
然后,打开WordPress配置文件:
sudo nano /var/www/your_domain/wp-config.php
须藤纳米/ var / www / your_domain /wp-config.php
Find the section that contains the dummy values for those settings. It will look something like this:
找到包含那些设置的虚拟值的部分。 它看起来像这样:
. . .
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
. . .
Delete those lines and paste in the values you copied from the command line:
删除这些行,然后粘贴从命令行复制的值:
. . .
define('AUTH_KEY', 'VALUES COPIED FROM THE COMMAND LINE');
define('SECURE_AUTH_KEY', 'VALUES COPIED FROM THE COMMAND LINE');
define('LOGGED_IN_KEY', 'VALUES COPIED FROM THE COMMAND LINE');
define('NONCE_KEY', 'VALUES COPIED FROM THE COMMAND LINE');
define('AUTH_SALT', 'VALUES COPIED FROM THE COMMAND LINE');
define('SECURE_AUTH_SALT', 'VALUES COPIED FROM THE COMMAND LINE');
define('LOGGED_IN_SALT', 'VALUES COPIED FROM THE COMMAND LINE');
define('NONCE_SALT', 'VALUES COPIED FROM THE COMMAND LINE');
. . .
Next you need to modify some of the database connection settings at the beginning of the file. First, update the 'DB_NAME'
, 'DB_USER'
, and 'DB_PASSWORD'
fields to point to the database name, database user, and the associated password that you configured within MySQL:
接下来,您需要在文件开头修改一些数据库连接设置。 首先,更新'DB_NAME'
, 'DB_USER'
和'DB_PASSWORD'
字段以指向数据库名称,数据库用户以及您在MySQL中配置的关联密码:
. . .
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wordpressuser');
/** MySQL database password */
define('DB_PASSWORD', 'password');
. . .
You will also need to replace localhost
in the 'DB_HOST'
field with your managed database’s host. Additionally, append a colon (:
) and your database’s port number to the host:
您还需要用托管数据库的主机替换'DB_HOST'
字段中的localhost
。 此外,附加一个冒号( :
)和数据库的端口号主机:
. . .
/** MySQL hostname */
define( 'DB_HOST', 'managed_database_host:managed_database_port' );
. . .
The last change you need to make is to set the method that WordPress will use to write to the filesystem. Since you’ve already given the web server permission to write where it needs to, you can explicitly set the filesystem method to direct
port. Failure to set this with your current settings would result in WordPress prompting for FTP credentials when you perform certain actions.
您需要做的最后更改是设置WordPress将用于写入文件系统的方法。 由于已经授予了Web服务器写入所需位置的权限,因此您可以将文件系统方法显式设置为direct
端口。 如果不使用当前设置对此进行设置,则当您执行某些操作时,WordPress会提示您输入FTP凭据。
This setting can be added below the database connection settings, or anywhere else in the file:
此设置可以添加到数据库连接设置下面,或文件中的其他任何位置:
. . .
define('FS_METHOD', 'direct');
. . .
Save and close the file when you are finished.
完成后保存并关闭文件。
After making those changes, you’re all set to finish the process of installing WordPress in your web browser. However, there’s one more step that we recommend you complete to add an extra layer of security to your configuration.
进行了这些更改之后,您都准备完成在Web浏览器中安装WordPress的过程。 但是,建议您再完成一个步骤,为配置增加一层安全性。
步骤7 –(推荐)配置WordPress以通过TLS / SSL与MySQL进行通信 (Step 7 – (Recommended) Configuring WordPress to Communicate with MySQL Over TLS/SSL)
At this point, your WordPress installation is communicating with your managed MySQL database. However, there’s no guarantee that data transfers between the two machines are secure. In this step, we will configure WordPress to communicate with your MySQL instance over a TLS/SSL connection to ensure secure communications between the two machines.
至此,您的WordPress安装正在与您的托管MySQL数据库进行通信。 但是,不能保证两台计算机之间的数据传输是安全的。 在此步骤中,我们将配置WordPress以通过TLS / SSL连接与您MySQL实例进行通信,以确保两台计算机之间的安全通信。
To do so, you’ll need your managed database’s CA certificate. For a DigitalOcean Managed Database, you can find this by once again navigating to the Databases tab in your Control Panel. Click on your database, and find the Connection Details section. There will be a button there that reads Download the CA certificate. Click this button to download the certificate to your local machine.
为此,您需要托管数据库的CA证书。 对于DigitalOcean托管数据库,您可以通过再次导航到“ 控制面板”中的“ 数据库”选项卡找到它。 单击您的数据库,然后找到“ 连接详细信息”部分。 那里会有一个按钮,显示“ 下载CA证书” 。 单击此按钮将证书下载到本地计算机。
Then transfer this file to your WordPress server. If your local machine is running Linux or macOS, you can use a tool like scp
:
然后将此文件传输到您的WordPress服务器。 如果本地计算机运行的是Linux或macOS,则可以使用诸如scp
类的工具:
scp /path/to/file/ca-certificate.crt sammy@your_server_ip:/tmp
scp / path / to / file / ca-certificate.crt sammy @ your_server_ip :/ tmp
If your local machine is running Windows, you can use an alternative tool like WinSCP.
如果您的本地计算机运行Windows,则可以使用WinSCP之类的替代工具。
Once the CA certificate is on your server, move it to the /user/local/share/ca-certificates/
directory, Ubuntu’s trusted certificate store:
将CA证书放置在服务器上后,将其移动到Ubuntu可信证书存储区的/user/local/share/ca-certificates/
目录中:
- sudo mv /tmp/ca-certificate.crt /usr/local/share/ca-certificates/ 须藤MV /tmp/ca-certificate.crt / usr / local / share / ca-certificates /
Following this, run the update-ca-certificates
command. This program looks for certificates within /usr/local/share/ca-certificates
, adds any new ones to the /etc/ssl/certs/
directory, and generates a list of trusted SSL certificates based on its contents:
之后,运行update-ca-certificates
命令。 该程序在/usr/local/share/ca-certificates
查找/usr/local/share/ca-certificates
,将所有新/usr/local/share/ca-certificates
添加到/etc/ssl/certs/
目录,并根据其内容生成可信SSL证书列表:
- sudo update-ca-certificates sudo update-ca-certificates
Then, reopen your wp-config.php
file:
然后,重新打开wp-config.php
文件:
nano /var/www/your_domain/wp-config.php
纳米/ var / www / your_domain /wp-config.php
Somewhere in the file, add the following line:
在文件中的某处,添加以下行:
. . .
define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL);
. . .
Save and close the file.
保存并关闭文件。
Following that, WordPress will securely communicate with your managed MySQL database.
之后,WordPress将安全地与您托管MySQL数据库进行通信。
步骤8 –通过Web界面完成安装 (Step 8 – Completing the Installation Through the Web Interface)
Now that the server configuration is complete, you can complete the installation through the WordPress web interface.
现在服务器配置已完成,您可以通过WordPress Web界面完成安装。
In your web browser, navigate to your server’s domain name or public IP address:
在Web浏览器中,导航到服务器的域名或公共IP地址:
https://server_domain_or_IP
Assuming there aren’t any errors in your WordPress or Apache configurations, you’ll see the WordPress language selection splash page. Select the language you would like to use:
假设您的WordPress或Apache配置中没有任何错误,您将看到WordPress语言选择的初始页面。 选择您要使用的语言:
After selecting your language, you will see the main setup page.
选择语言后,您将看到主设置页面。
Select a name for your WordPress site and choose a username (it is recommended not to choose something like “admin” for security purposes). A strong password is generated automatically. Save this password or enter an alternative strong password.
为您的WordPress网站选择一个名称,然后选择一个用户名(出于安全考虑,建议不要选择“ admin”之类的名称)。 系统会自动生成一个强密码。 保存此密码或输入其他强密码。
Enter your email address and select whether you want to discourage search engines from indexing your site:
输入您的电子邮件地址,然后选择是否要阻止搜索引擎将您的网站编入索引:
When you click ahead, you will be taken to a page that prompts you to log in:
当您单击前进时,将转到一个页面,提示您登录:
Once you log in, you will be taken to the WordPress administration dashboard:
登录后,您将被带到WordPress管理仪表板:
From here, you can begin customizing your new WordPress site and start publishing content. If this is your first time using WordPress, we encourage you to explore the interface a bit to get acquainted with your new CMS.
从这里开始,您可以开始自定义您的新WordPress网站并开始发布内容。 如果这是您第一次使用WordPress,建议您稍微浏览一下界面以熟悉新的CMS。
Warning: Depending on your managed database provider, you may need to adjust which SQL modes the database is using. SQL modes define how MySQL validates data and the syntax it supports, and certain SQL modes can negatively impact WordPress’s performance. For instance, the default SQL modes on DigitalOcean Managed MySQL Databases enable MySQL’s strict mode which can cause errors in a WordPress database.
警告:根据托管数据库提供程序的不同,您可能需要调整数据库使用的SQL模式 。 SQL模式定义MySQL如何验证数据及其支持的语法,某些SQL模式可能会对WordPress的性能产生负面影响。 例如,DigitalOcean托管MySQL数据库上的默认SQL模式启用MySQL的严格模式 ,这可能会导致WordPress数据库出错。
There are a number of SQL modes that can affect WordPress’s performance, as well as a number of ways to disable strict mode, so you should review the official MySQL documentation on the subject to determine which settings are best suited to your needs. If you’re using a DigitalOcean Managed Database, see our Managed MySQL product documentation for details on how to change the default SQL mode settings.
有很多SQL模式可能会影响WordPress的性能,还有许多禁用严格模式的方法,因此您应该查看有关该主题MySQL官方文档,以确定最适合您的设置。 如果您使用的是DigitalOcean托管数据库,请参阅我们的托管MySQL产品文档,以获取有关如何更改默认SQL模式设置的详细信息。
结论 (Conclusion)
By completing this guide, you will have WordPress installed and ready to use on your server. Additionally, your WordPress installation is dynamically pulling posts, pages, and other content from your managed MySQL database.
完成本指南后,您将安装WordPress并可以在服务器上使用。 此外,您的WordPress安装会从托管MySQL数据库中动态提取帖子,页面和其他内容。
Some common next steps are to choose the permalinks setting for your posts. This setting can be found under Settings > Permalinks. You could also select a new theme in Appearance > Themes. Once you start loading some content into your site, you could also configure a CDN to speed up your site’s asset delivery.
接下来的一些常见步骤是为您的帖子选择永久链接设置。 可以在“设置” >“ 固定链接”下找到此设置。 您也可以在外观 > 主题中选择一个新主题 。 一旦开始将一些内容加载到站点中,您还可以配置CDN以加快站点的资产交付速度 。