最近在宝塔里新建了一个WordPress网站,由于没有提前准备域名,所以暂时使用IP充当域名倒腾网站。
宝塔面板WordPress域名更换步骤
域名买好之后,开始按照下面的步骤更换:文章源自ERI博客-https://eriboke.one/49.html
- 先解析好新的域名,一般是添加2条A记录解析到IP地址,ERI这里使用的CloudFlare;
- 进入WordPress后台,在设置-常规中将WordPress地址URL和站点地址URL改为新域名的HTTPS URL;
- 进入宝塔面板,如果原来的域名安装了SSL的话需要先删掉SSL证书;
- 在宝塔中的域名管理中添加根域名和www.域名,删掉原来的IP或域名;
- 为新域名申请SSL证书,打开强制使用 HTTPS;
- 如果使用的CloudFlare解析域名,记得在SSL/TLS-概述中将SSL/TLS 加密模式改为完全(严格)。
正常情况下,应该已经可以通过新域名访问网站了。但是我更换完之后不太正常,接着往下看:文章源自ERI博客-https://eriboke.one/49.html
宝塔面板更换WordPress网站域名后静态资源丢失
通过上面的步骤更换完域名后访问网站时我发现,网站上的静态资源丢失了!图片和各种样式都没能加载,这是因为WordPress和有些主题使用绝对路径的静态资源URL,虽然你将域名更换了,但是静态资源的URL依旧使用的旧路径,而你删除了原本的域名,所以无法通过旧的静态资源URL进行访问。文章源自ERI博客-https://eriboke.one/49.html
不要慌,这很好解决,只要更新数据库中的URL就OK。可以尝试下面的3种方法:文章源自ERI博客-https://eriboke.one/49.html
方法一、使用Better Search Replace插件替换数据库中的URL
在插件中搜索Better Search Replace,安装并启用后在后台-工具中点击Better Search Replace。文章源自ERI博客-https://eriboke.one/49.html
在 “搜索/替换” 选项中:文章源自ERI博客-https://eriboke.one/49.html
- 搜索:
http://原URL
- 替换为:
https://新URL
随便点击选中一个数据表,然后按住Ctrl+A选择所有数据表,附加设置中不需要勾选任何选项,最后运行替换。文章源自ERI博客-https://eriboke.one/49.html
方法二、使用SSH命令直接在服务器上执行数据库替换
这里使用WP-CLI工具。在SSL登录你的服务器。文章源自ERI博客-https://eriboke.one/49.html
首先,安装WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
1、验证文件是否可执行
php wp-cli.phar --info
会返回类似下面的结果:文章源自ERI博客-https://eriboke.one/49.html
OS: Linux 5.10.0-30-amd64 #1 SMP Debian 5.10.218-1 (2024-06-01) x86_64
Shell: /bin/bash
PHP binary: /www/server/php/83/bin/php
PHP version: 8.3.7
php.ini used: /www/server/php/83/etc/php-cli.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 14.14 Distrib 5.7.40, for Linux (x86_64) using EditLine wrapper
SQL modes:
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /root
WP-CLI packages dir:
WP-CLI cache dir: /root/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.10.0
3、将WP-CLI安装到全局(依次执行)
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
4、验证WP-CLI是否安装成功
wp --info
得到和前面一样的结果即为成功。文章源自ERI博客-https://eriboke.one/49.html
然后,使用WP-CLI替换URL
1、cd到你的网站根目录
cd /www/wwwroot/你的根目录名称
2、运行搜索和替换命令
wp search-replace 'http://旧域名' 'https://新域名' --all-tables
如果你是以root身份运行的命令,可能会遇到以下报错:
Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.
If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
If you'd like to continue as root, please run this again, adding this flag: --allow-root
If you'd like to run it as the user that this site is under, you can run the following to become the respective user:
sudo -u USER -i -- wp <command>
只需根据提示在命令后面添加--allow-root
即可,下面的完整的命令:
wp search-replace 'http://旧域名' 'https://新域名' --all-tables --allow-root
等待执行完毕。
至此,问题已经解决了。如果你在搭建网站时还遇到其他问题,欢迎留言提问。
1F
感谢分享