1137 字
6 分钟
Ubuntu 部署WordPress
2021-05-02

主要记录一些部署 Wordpress 时出现的问题——2021年8月27日 修复一些部署 Wordpress 的方法(2024.05.04) 2024.05.12 添加了花生壳内网穿透的配置方法

我的配置方案#

硬件:Jetson Nano B01 数据库:MySQL 服务器:Nginx 内网穿透:花生壳


MySQL安装和建立#

MySQL 的部署脚本:

Terminal window
sudo apt upate
sudo apt upgrade
# 安装 nginx
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
# 安装 mysql 数据库
sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql

设置 MySQL 的账号和密码:sudo mysql -u root -p 密码

更新 MySQL 的数据表

# 在 mysql 中添加如下内容
CREATE DATABASE 数据库名;
CREATE USER 用户名@域名 IDENTIFIED BY 密码;
GRANT ALL PRIVILEGES ON 数据库名.* TO 用户名@域名;
FLUSH PRIVILEGES;
EXIT;

PHP 和 Niginx 安装和配置#

安装 PHP:sudo apt install php php-fpm php-mysql -y

安装 Nginx:sudo apt install nginx

配置 Nginx:vim /etc/nginx/sites-available/default

  • 设置 WordPress 的安装目录
  • 添加 index.php
  • 设置 php 的位置和版本
listen 80 default_server;
listen [::]:80 default_server;
root (WordPress 的安装目录);
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php8.x-fpm.sock; # 注意PHP版本
# With php-cgi (or other tcp sockets):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}

重启 Nginx:sudo systemctl reload nginx


WordPress 安装与配置#

获取 WordPress 的相关文件:

Terminal window
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo chown -R www-data:www-data /var/www/html/wordpress

进入 WordPress 目录,配置 wp-config.php 文件:

Terminal window
sudo cp wp-config-sample.php wp-config.php
sudo vim wp-config.php

主要修改内容分别是数据库名、用户名和密码:

define( 'DB_NAME', 'MySQL 数据库名' );
define( 'DB_USER', 'MySQL 数据库用户名' );
define( 'DB_PASSWORD', '对应密码' );
define( 'DB_HOST', 'localhost' );

剩下的就是通过浏览器访问网页,完成后续的配置就行了。


花生壳外网映射#

注意修改 nginx 的配置文件 /etc/nginx/site-available/default

server_name localhost;

仅使用外网(推荐)#

wp-amdin >> 设置 里修改对应参数:

  • WordPress 地址(URL):域名
  • 站点地址(URL):域名

也可以通过数据操作实现:

SELECT option_val WHERE option_name IN ('home', 'siteurl');
UPDATE wp_options SET option_value = '域名' WHERE option_name IN ('home', 'siteurl');

兼容内网和外网#

  • WordPress
    • WordPress 地址(URL):本机 ip 地址
    • 站点地址(URL):本机 ip 地址

wp-config.php 中添加如下内容:

// FOR URL
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

这种方法貌似无法设置固定链接,就无法通过 cache 提速。


报错处理#

设置固定链接后访问文章 404#

sudo vim /etc/nginx/site_available/default 去除掉对应的规则:

location / {
#try_files $uri $uri/ /index.php?$args =404;
}

您点击的链接已过期#

Terminal window
sudo find / -type f -name php.ini
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
reboot

您的PHP缺少扩展gd,缺少这些扩展可能导致部分功能无法使用,请及时安装这些扩展。#

Terminal window
sudo apt install php-gd

Wordpress 添加备案号#

关于 Wordpress 添加备案号,是可以通过 仪表板>>设置>>常规>>ICP备案号 中进行设置,但有些坑,然后我看了不少教程,大多也都是一下两种方法进行添加的。

html语句#

由于 php 是兼容 html 语法的,所以我们就可以在 footer.php 中添加的备案信息

可以在 footer.php 的 <footer> </footer> 标签内,增加代码:

<a href="https://beian.miit.gov.cn/#/Integrated/index">你的备案号</a>

php函数#

在部分的主题中 <footer> 标签是隐藏在php函数中,就比如我之前使用的Richone主题中,就是如此,它将该标签隐藏在了 wp_footer() 中,而这个函数则位于在 functions.php 中。

<?php wp_footer(); ?>

在保留Richone主题说明和链接的情况下,在它前面添加了备案号。

function richone_datefo() {
echo('<div class="&#114;b row text-center">');
echo('<a href="https://beian.miit.gov.cn/#/Integrated/index">你的备案号</a>');
echo(' Wordpress With <a href="https://richwp.com">RichWP</a>');
echo('</div></footer>');
}
add_action( 'wp_footer', 'richone_datefo' );

Wordpress 如何修改文章显示宽度#

对于修改 Wordpress 的文章宽度,首先是去寻找对于的 css 文件,修改对应的参数,参考文章^1中都给出了方法,但是在实际修改参数时,我并没有找到对应的代码段,所以在实际操作后,给出一种修改网页 CSS 的思路。

寻找 CSS 样式的 id#

在浏览器(我的是 Firefox )的开发者工具中,找到文章的 div 标签,我这里文章的 div 标签的 id 是 container

RHbFDU.png

调整样式参数#

在右侧的元素中找到控制文章宽度的属性,我的是 .page #container, .single #containter,双击可进入样式编辑器。

RHbmCR.png

先在样式编辑器里修改 max-width 参数,现在浏览器里调整到一个不错的宽度,再去 style.css 中修改。

RHblDO.png

Ubuntu 部署WordPress
https://fuwari.vercel.app/posts/ubuntu/ubuntu-部署wordpress/
作者
Asuwee
发布于
2021-05-02
许可协议
CC BY-NC-SA 4.0