安装WordPress

记于:2024-10-27 晚上
地点:浙江省·温州市·家里
天气:阴天

背景#

随便玩玩先

过程#

准备环境#

根据【参考资料】中的【环境要求】,先进行环境的准备;

安装PHP#

1
2
3
4
5
6
7
8
# apt update
# apt install -y php php-fpm php-mysql

# php -v
PHP 8.1.2-1ubuntu2.19 (cli) (built: Sep 30 2024 16:25:25) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.19, Copyright (c), by Zend Technologies

安装mysql#

参考本博客文章【docker形式安装mysql8 】

https#

暂略

安装nginx#

1
# apt install -y nginx

配置与安装#

配置wordpress与nginx,以及php

配置wordpress#

下载wordpress,解压,示例:/var/www/html/wordpress
复制wp-config-sample.phpwp-config.php,并修改数据库连接信息,示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...略

// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

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

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

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

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', 'utf8mb4_general_ci' );

...略

创建数据库wordpress,数据库名称跟上面的DB_NAME一致;

配置nginx#

/etc/nginx/conf.d/example.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 default_server;
listen [::]:80 default_server;
server_name _;

root /var/www/html/wordpress;

# wordpress
set $wp_root "/var/www/html/wordpress";
location / {
root $wp_root;
index index.php index.html index.htm;
try_files $uri $uri/ /wordpress/index.php?$args;
}

location ~* ^/wordpress/.*\.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
root $wp_root;
expires max;
log_not_found off;
}

# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
}

注意:nginx到php的连接方式有两种,一种是unix socket,一种是tcp socket;
如果使用unix socket,需要在php-fpm的配置文件中配置listen = /run/php/php-fpm.sock
如果使用tcp socket,需要在php-fpm的配置文件中配置listen = 127.0.0.1:9000
php-fpm配置文件位置示例:/etc/php/8.1/fpm/pool.d/www.conf
修改后重启php-fpm,示例:systemctl restart php8.1-fpm.service
最终重新加载nginx配置:nginx -s reload

安装wordpress#

配置好上述内容后,访问wordpress的安装页面,示例:https://example.com/wp-admin/install.php

问题与解决#

wordpress连接数据库失败#

1
2
3
4
5
6
7
8
9
Warning:  mysqli_real_connect(): (HY000/2002): No such file or directory in /var/www/html/wordpress/wp-includes/class-wpdb.php on line 1982

No such file or directory
Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or that contact with the database server at localhost could not be established. This could mean your host’s database server is down.
- Are you sure you have the correct username and password?
- Are you sure you have typed the correct hostname?
- Are you sure the database server is running?
If you are unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress support forums.

由于mysql是docker安装,当wp-config.php中DB_HOST为localhost时,mysql会尝试通过unix套接字文件连接(通常是 /var/run/mysqld/mysqld.sock),而/var/run/mysqld/mysqld.sock文件未做映射,导致连接失败;
解决方法:将DB_HOST修改为127.0.0.1,从而使用tcp socket连接;

安装主题权限不足#

弹出ftp连接框,提示需要输入ftp的用户名和密码;
解决方法:修改wordpress的文件夹权限,示例:chown -R www-data:www-data /var/www/html/wordpress
www-data是nginx的运行用户,需要将wordpress的文件夹所有者修改为nginx的运行用户;

其他#

wordpress调试信息#

wp-config.php中打开调试开关:

1
define( 'WP_DEBUG', true );

这样可以在页面输出错误信息;

参考资料#