0%

wordpress | wordpress 搭建(包含多站点)

现在我使用的整体架构是

AWS + ubuntu20.04 + wordpress + PHP7.4 + mysql8.0 + nginx

配置安全组

更新操作系统

1
2
sudo apt-get update
sudo apt-get upgrade -y

mysql

ubuntu20.04 默认安装 8.0

1
sudo apt-get install mysql-server

默认安装的 mysql 是没有密码的,需要通过 sudo 权限进入,然后修改密码参考

创建数据库

1
2
3
4
5
6
sudo mysql -u root -p
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY '弄一个不一样的密码';
CREATE DATABASE `wordpress-db`;
GRANT ALL PRIVILEGES ON `wordpress-db`.* TO "wordpress"@"localhost";
FLUSH PRIVILEGES;
exit

删除使用

1
drop database `wordpress-db`;

安装 nginx

配置文件如下

1
2
3
4
sudo apt-get install nginx -y # 安装
sudo systemctl status nginx # nginx 运行状态
sudo systemctl start nginx # 启动
sudo systemctl enable nginx # 开机自启动

我的 nginx 的配置文件是 /etc/nginx/nginx.conf 里面有一段

1
2
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

表示这个两个文件夹下的配置文件也算。其中 sites-enabled 表示的是站点配置,里面存放的是软链接。

进入 /etc/nginx/sites-available/ 注意是 available,发现有一个文件是 default,而 /etc/nginx/sites-enabled/ 里面也有一个 default。其中 enableddefalutavailable 的软链接。

如果,你现在直接通过 IP 访问,会出现 nginx 的默认界面。

删除默认

/etc/nginx/sites-available//etc/nginx/sites-enabled/ 删掉即可。

使用站点配置

/etc/nginx/sites-available/ 创建一个文件叫做 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
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}

server {
listen 80;
## Your website name goes here.
server_name _;
## Your only path reference.
root /var/www/wordpress; # wordpress 的放置路径
## This should be in your http block and if it is, it's not needed here.
index index.php;

location = /favicon.ico {
log_not_found off;
access_log off;
}

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

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

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

多站点配置

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
59
60
61
62
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}

server {
listen 80;
## Your website name goes here.
server_name _;
## Your only path reference.
root /var/www/wordpress; # wordpress 的放置路径
## This should be in your http block and if it is, it's not needed here.
index index.php;

location = /favicon.ico {
log_not_found off;
access_log off;
}

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

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

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

#ignored: “-” thing used or unknown variable in regex/rew
rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ permanent;

if (-f $request_filename){
set $rule_2 1;
}
if (-d $request_filename){
set $rule_2 1;
}
if ($rule_2 = "1"){
#ignored: “-” thing used or unknown variable in regex/rew
}
rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 last;
rewrite ^/([_0-9a-zA-Z-]+/)?(.*.php)$ /$2 last;
rewrite /. /index.php last;
}

然后进行软链接创建,相当于给 nginx 通知其中一个站点的配置。

1
sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/wordpress.conf

但是,这样直接访问会出现 502 的错误,这是和 php-fpm 有关系的。具体参考下面。

ps: 上面的配置不对

1
2
3
4
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}

需要把上面的配置改成

1
2
3
4
upstream php {
server unix:/tmp/php-cgi.socket;
# server 127.0.0.1:9000;
}

参考资料

安装 PHP 7.4

1
sudo apt-get install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-json php7.4-opcache php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl -y

php7.4-fpm 用法

上面直接用 ip 访问会出现 502,查看了一下 nginxerror log,注:log 的文件路径在 /etc/nginx/nginx.conf 中得到。

log 中出现

1
2
3
4
5
6
7
8
2024/02/28 07:23:20 [crit] 35387#35387: *36 connect() to unix:/tmp/php-cgi.socket failed (2: No such file or directory) while connecting to upstream, client: 163.53.18.75, 
server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.socket:", host: "16.162.45.243"
2024/02/28 07:23:20 [error] 35387#35387: *36 connect() failed (111: Connection refused) while connecting to upstream, client: 163.53.18.75, server: _, request: "GET / HTTP/
1.1", upstream: "fastcgi://127.0.0.1:9000", host: "16.162.45.243"
2024/02/28 07:23:23 [error] 35387#35387: *36 no live upstreams while connecting to upstream, client: 163.53.18.75, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi:
//php", host: "16.162.45.243"
2024/02/28 07:23:25 [error] 35387#35387: *36 no live upstreams while connecting to upstream, client: 163.53.18.75, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi:
//php", host: "16.162.45.243"

这主要是因为 fpmsocket 路径错了。

我的 fpm 的配置在 /etc/php/7.4/fpm/php-fpm.conf,打开这个文件,出现

1
include=/etc/php/7.4/fpm/pool.d/*.conf

说明,在 /etc/php/7.4/fpm/pool.d/ 下的配置文件也是配置。

进入该目录使用 cat * | grep listen

出现 listen = /run/php/php7.4-fpm.sock

然后将上述 nginxwordpress.conf

1
2
3
4
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}

改为

1
2
3
4
upstream php {
server unix:/run/php/php7.4-fpm.sock;
server 127.0.0.1:9000;
}

重启 nginx

1
2
sudo nginx -t
sudo nginx -s reload

安装 wordpress

1
2
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz

我将 wordpress 放置在 /var/www/wordpress

1
2
sudo mv wordpress/* /var/www/wordpress/
sudo chmod -R 777 /var/www/wordpress

访问 IP 后,输入参数。

注: 这里不建议用 IP 访问,而是用域名访问,因为用 IP 访问,会导致,在数据库里面会写入 ipurl,后期修改不方便。

增加中文语言包

但是,英文版默认只有英文语言,如果想要切换到中文,需要下载中文语言包。最简单的方式就是下载一份中文的 wordpress 然后找到语言包。

语言在 wp-content 中。上服务器,将中文版本的 languages 复制到线上服务器的 wp-content 中。

不需要刷权限,也不需要重启 wordpress。

要给予权限

1
chmod -R 777 ./languages

否则后面更新不了翻译。

多站点

需要先把所有插件关闭。

找到 wp-config.php 文件(应该在 /var/www/wordpress)打开,后面添加以下代码:

1
define( 'WP_ALLOW_MULTISITE', true );

刷新网站,点击【工具】的配置网络,然后复制 wordpress 提示的内容,然后按照要求放在 wp-config.php

1
2
3
4
5
6
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', true ); # 如果是 false 就是子目录模式,如果是 true 就是子域名模式,想好,后面不能更换了
define( 'DOMAIN_CURRENT_SITE', '域名' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

下面就是配置网络,如果你想多站点配置域名,那此时必须是域名配置,不能是 ip 配置,否则后面很难替换。

当然,nginx 要使用上面的多站点配置。

ps: 一定要按照顺序来,不然会出错。

解决输入 FTP

我们安装插件会出现输入 FTP 名字和密码等。

  1. 授权目录
1
chmod -R 777 /var/www/wordpress

把红色部分替换成自己站点的目录

  1. 修改wp-config.php文件
1
2
3
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);

wp-config.php 文件最后添加上面三行。

请我喝杯咖啡吧~