安装wordpress


目录
  1. 1. 安装 Nginx
  2. 2. 安装 MariaDB 数据库服务器
  3. 3. 创建 Nginx 服务器块
  4. 4. 下载 WordPress
  5. 5. 为 WordPress 网站创建数据库和用户
  6. 6. 为 WordPress 创建 Nginx 服务器块
  7. 7. 测试 Nginx 配置

wordpress需要web服务和数据库。

安装 Nginx

sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

让(Nginx 用户)成为 Web 目录的所有者。默认情况下,其权限归 root 用户所有。
sudo chown www-data:www-data /usr/share/nginx/html -R

安装 MariaDB 数据库服务器

sudo apt install mariadb-server mariadb-client
sudo systemctl status mariadb
如果它未运行,用以下命令启动它:
sudo systemctl start mariadb
要使 MariaDB 在启动时自动启动,需运行
sudo systemctl enable mariadb
sudo apt install php-fpm
sudo systemctl start php-fpm
sudo systemctl statu php-fpm
systemctl enable php8.2-fpm

创建 Nginx 服务器块

sudo rm /etc/nginx/sites-enabled/default
sudo xed /etc/nginx/conf.d/default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html/;
index index.php index.html index.htm index.nginx-debian.html;

location / {
try_files $uri $uri/ /index.php;
}

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

location ~* \..webp|jpeg|gif.webp|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}

# disable access to hidden files
location ~ /\.ht {
access_log off;
log_not_found off;
deny all;
}
}

sudo nginx -t
sudo systemctl reload nginx
至此,LNMP(Linux、Nginx、MariaDB/MySQL和PHP)堆栈安装完毕

下载 WordPress

wget https://wordpress.org/latest.zip
sudo mkdir -p /usr/share/nginx
sudo unzip latest.zip -d /usr/share/nginx/

为 WordPress 网站创建数据库和用户

以 root 身份登录 MariaDB shell
sudo mariadb -u root
登录后,使用以下命令为 WordPress 创建数据库。
create database wordpress;
然后输入以下命令为 WordPress 创建数据库用户。此命令还向用户授予 WordPress 数据库的所有权限。将 用户名和密码 替换为你自己要设置的用户名和密码。
grant all privileges on wordpress.* to 用户名@localhost identified by ‘密码’;
刷新权限表以使更改生效,然后退出 MariaDB shell。
flush privileges;
exit;
配置 WordPress
转到WordPress 所在目录。
cd /usr/share/nginx/wordpress/
复制示例配置文件并将其重命名为 :wp-config.php
sudo cp wp-config-sample.php wp-config.php
现在使用文本编辑器(如 vim)编辑新的配置文件。
sudo xed wp-config.php
找到以下行,并将下方中文文本替换为在上一步中创建的数据库名称、用户名和密码。

1
2
3
4
5
6
7
8
/** The name of the database for WordPress */
define('DB_NAME', '这里填你自己创建的数据库名称');

/** MySQL database username */
define('DB_USER', '这里是你自己创建的用户名');

/** MySQL database password */
define('DB_PASSWORD', '这里是你自己设置的密码');

将 Nginx 用户设置为 WordPress 站点目录的所有者。
sudo chown www-data:www-data /usr/share/nginx/wordpress/ -R

为 WordPress 创建 Nginx 服务器块

sudo xed /etc/nginx/conf.d/wordpress.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
server {
listen 80;
listen [::]:80;
server_name www.wordpress wordpress;
root /usr/share/nginx/wordpress/;
index index.php index.html index.htm index.nginx-debian.html;

location / {
try_files $uri $uri/ /index.php;
}

location ~ ^/wp-json/ {
rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
}

location ~* /wp-sitemap.*\.xml {
try_files $uri $uri/ /index.php$is_args$args;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

client_max_body_size 20M;

location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
fastcgi_buffers 1024 4k;
fastcgi_buffer_size 128k;

# Add headers to serve security related headers
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Permitted-Cross-Domain-Policies none;
add_header X-Frame-Options "SAMEORIGIN";
}

#enable gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 5;
gzip_types application/json text/css application/x-javascript application/javascript image/svg+xml;
gzip_proxied any;


location ~* \..webp|jpeg|gif.webp|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
}

测试 Nginx 配置

sudo nginx -t
sudo systemctl reload nginx

在浏览器地址栏中如下输入。
localhost/wp-admin/install.php
如果未显示安装向导,则可能需要安装一些 PHP 扩展。
sudo apt install php-imagick php-fpm php-mbstring php-bcmath php-xml php-mysql php-common php-gd php-json php-cli php-curl php-zip
sudo systemctl reload php8.2-fpm nginx
然后重新加载 PHP-FPM 和 Nginx服务。现在应该显示向导。
使用安装向导完成安装。