使用MySQL、Nginx和PHP搭建Wordpress博客。
1. 安装Wordpress
1.1 配置MySQL
创建Wordpress相应的用户和数据库:
$ sudo mysql -u root -p
mysql> CREATE DATABASE database_name_here;
mysql> CREATE USER 'username_here'@'localhost' IDENTIFIED BY 'password_here';
mysql> GRANT ALL ON database_name_here.* TO 'username_here'@'localhost' IDENTIFIED BY 'password_here' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
查看新建的用户和数据库:
mysql> SELECT user,host FROM mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
| wp_admin | localhost |
+---------------+-----------+
mysql> SHOW databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wp |
+--------------------+
用户和数据库均以创建成功,配置MySQL完毕。
1.2 下载、配置Wordpress
从官网下载Wordpress压缩包:
$ wget https://wordpress.org/latest.zip
解压Wordpress压缩包,解压后应该是一个wordpress的文件夹,复制文件夹到Nginx访问路径:
$ sudo mv wordpress .../html/
进入wordpress目录,设置Wordpress配置文件:
$ sudo cp wp-config-sample.php wp-config.php
$ sudo vim wp-config.php
配置数据库相应参数,和上文的数据库设置对应:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
更改wordpress文件夹的用户和用户组,使Nginx有读写权限:
sudo chown -R nginx:nginx /var/www/html/wordpress
1.3 配置Nginx
创建Wordpress在Nginx的配置文件:
$ sudo vim /etc/nginx/conf.d/wp.conf
并配置Wordpress的server block:
server {
listen 80;
listen [::]:80;
server_name _; # domain or IP
root .../wordpress; # path to wordpress
index index.php index.html index.htm;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php{
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAMEdocument_rootfastcgi_script_name;
include fastcgi_params;
}
location / {
try_filesuri uri/ /index.phpis_args$args;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
检查配置正确:
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启Nginx:
$ sudo systemctl restart nginx.service
至此,打开wordpress的网页链接,会自动跳转Wordpress的初始化页面,安装完成。
2. 安装中遇到的问题
2.1 安装Plugin,提示需要FTP
当尝试安装plugin,有以下提示:
To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.
这是因为Wordpress使用以下几种方式将plugin传输到本地1:
– direct
– ssh2
– ftpext
– ftpsockets
在wp-config.php文件添加以下内容,使用直接传输的方式:
/** user-defined **/
define('FS_METHOD','direct'); /* directly upload files */
2.2 PHP Fatal error: … Call to undefined function json_decode()
2020/06/08 21:47:22 [error] 3975#0: *9 FastCGI sent in stderr: “PHP message: PHP Fatal error: Uncaught Error: Call to undefined function json_decode() in /usr/share/nginx/html/wordpress/wp-includes/blocks/shortcode.php:25
Stack trace:
…
安装php-json问题解决:
$ sudo dnf install php-json
2.3 Installation failed: Could not create directory
安装plugin出现错误提示:
Installation failed: Could not create directory.
如果已经将wordpress文件夹的用户和用户组改为了nginx,但是仍然出现提示,那么就可能是php-fpm的问题。
Nginx对php的处理是调用了php-fpm,所以安装plugin真正进行操作的是php-fpm,如果php-fpm没有对wordpress的文件有写的权限,那就不能传输plugin的文件夹。
参考《CentOS安装PHP7.3》2配置PHP-FPM。