手把手教你从零搭建一个wordpress博客


需求:搭建一个wordpress 用来直接写博客

上效果:

前台效果:
image.png
后台界面:
image.png

部署过程:

环境条件: ubuntu 20.0 + php7.0 + nginx + mysql 8.0

  • 一、配置mysql
# 登录数据库
mysql -uroot -p 

#mysql 8.0 的加密方式发生了改变,后边如果不注意会遇到数据库连不上的报错,开启bug 模式后会遇到下面一行报错。所以创建用户时候应该修改8.0以前的加密方式
# mysqli_real_connect(): (hy000/2054): the server requested authentication method unknown to the client in /usr/share/nginx/html/wordpress/wp-includes/wp-db.php on line 1531

#首先更改 php7.0 版本连接mysql 8.0 的加密方式 php 默认是mysql_native_password , mysqld 8.0 的默认是cahing_sha2_password
alter user 'root'@'localhost' identified with mysql_native_password by 'password'
flush privileges;

# 创建数据库以及字符编码
create database wp_database default charset utf8 cokkate utf8_general_ci;
#数据库所有权限进行授权
grant all on wp_database.* to 'user'@'localhost'

# 刷新数据配置
flush privileges;
  • 二、下载wordlpress 并配置
# a.通过yum命令下载的WordPress保存在/usr/share/wordpress目录下。
yum -y install wordpress

# b.将下载的WordPress移动至网站根目录
mv /usr/share/wordpress /usr/share/nginx/html/wordpress

# c.修改配置文件
# 配置 软连接
cd /usr/share/nginx/html/wordpress
ln -snf /etc/wordpress/wp-config.php wp-config.php

#编辑wp-config.php 设置相关配置
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'user');

/** MySQL数据库密码 */
define('DB_PASSWORD', 'PASSword123.');

/** MySQL主机 */
define('DB_HOST', 'localhost');
配置完成后保存
  • 三、安装php
#php 安装也会遇到坑, ubuntu 默认的apt install php 版本是8.0, 而我默认安装的wordpress 版本的语法还未支持,报错内容如下:后边果断将版本切回到了7.0
#PHP 8: "Array and string offset access syntax with curly braces is no longer supported"
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
  • 四、配置nginx 访问
# 在nginx 中做转发
server {
  listen 80 ;
  server_name 域名;
  root /usr/share/nginx/html/wordpress;
  location / {
    try_files $uri/ /index.php?$args;
    index index.php;
  }
  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;// php运行服务
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
  location ~ /\.ht {
    deny all;
  }
  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }
  // 静态文件路径
  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
  }
  • 五、 屏蔽配置ftp
#在wp-config.php 配置文件中进行ftp 认证跳过

define("FS_METHOD","direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
# 同时记得给根目录下的wordpress  读写权限
chmod -R 755 wordpress/

参考链接:

https://help.aliyun.com/document_detail/151691.html
https://blog.csdn.net/qq_34142812/article/details/102645058
https://www.kingsonho.com/install-wordpress-with-nginx-on-ubuntu/
https://www.cnblogs.com/weifeng1463/p/15787287.html
https://www.lylinux.net/article/2018/7/29/49.html