Skip to content

独立站搭建流程记录

· 12 min · wordpress / litespeed / mariadb-server

站点搭建#

Terminal window
# 创建放置证书的目录(主机商提供或者自己购买) 也可放到/usr/local/lsws/conf/cert目录下
sudo mkdir -p /usr/local/ssl
sudo chown -R root:root /usr/local/ssl
sudo chmod -R 400 /usr/local/ssl
sudo apt-get update
sudo apt-get upgrade
#安装 lsws web服务 (litespeedwebserver)
wget -O - https://repo.litespeed.sh | sudo bash
apt-get install openlitespeed
sudo apt install lsphp74 lsphp74-common lsphp74-curl lsphp74-imap lsphp74-json lsphp74-mysql lsphp74-opcache lsphp74-imagick
#需要对象缓存可以安装以下缓存插件
#redis
sudo apt install lsphp74-redis
sudo apt install redis
sudo systemctl start redis-server
sudo systemctl enable redis-server
# memcached
sudo apt install lsphp74-memcached
sudo apt-get install memcached
#修改php配置
vi /usr/local/lsws/lsphp74/etc/php/7.4/litespeed/php.ini
upload_max_filesize = 50M
post_max_size=50M
memory_limit = 512M
systemctl start lsws
# 安装数据库
sudo apt update
sudo apt install mariadb-server
sudo mysql_secure_installation
sudo mysql
# 创建数据库
CREATE DATABASE databaseName;
CREATE USER username@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON databaseName.* TO username@'localhost';
or
# GRANT ALL PRIVILEGES ON databaseName.* TO username@'%' identified by 'password'
FLUSH PRIVILEGES;
# 按需打开mariadb远程访问权限
vi /etc/mysql/mariadb.conf.d/50-server.conf
# 注释掉下面内容
# bind-address = 127.0.0.1
# 安装wordpress
wget -P /usr/local/lsws/surpoutlets/html/ https://wordpress.org/latest.tar.gz
sudo tar xvfz /usr/local/lsws/surpoutlets/html/latest.tar.gz -C /usr/local/lsws/surpoutlets/html/
sudo chown -R nobody:nogroup /usr/local/lsws/surpoutlets/html/wordpress
sudo find /usr/local/lsws/surpoutlets/html/wordpress/ -type d -exec chmod 750 {} \;
sudo find /usr/local/lsws/surpoutlets/html/wordpress/ -type f -exec chmod 640 {} \;
sh /usr/local/lsws/admin/misc/admpass.sh
# 防火墙和端口设置
sudo apt update
sudo apt install ufw -y
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 7080/tcp #lsws服务
sudo ufw allow 8088/tcp
sudo ufw allow 3360/tcp #数据库访问端口
sudo ufw enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw status verbose

已有站点-迁移#

wp-stage插件#

使用该插件直接可恢复到指定版本, 选择备份文件即可

手动备份#

备份数据库数据#

Terminal window
# 1. 原服务器导出
mysqldump -uusername -ppassword dbname | gzip > /data/wwwroot/www/dbname.sql.gz
# 2. 传输
scp /data/wwwroot/www/dbname.sql.gz root@newserverip:/root/dbname.sql.gz
# 3. 新服务器导入
gunzip < /root/dbname.sql.gz | mysql -uusernameb -ppasswordb dbnamenew

迁移原有站点的数据内容#

Terminal window
scp -R /usr/local/lsws/wordpress root@newserver:/usr/local/lsws/wordpress

域名变更#

update wp_posts set guid = replace(guid, 'current.domain.com' ,'target.domain.com')
where guid like 'current.domain.com%'
update wp_posts set post_content = replace(post_content, 'https://surpoutlets.com' ,'https://us.surpoutlets.com')
where post_content like 'https://surpoutlets.com%'
update wp_posts_meta

迁移过后可能出现的问题#

访问页面或者发布页面出现404#

新增/修改页面时提示 此响应不是合法的JSON响应。 可以尝试以下解决办法

域名修改后新域名出现重定向301到老域名#

define( 'WP_DEBUG', true ); // 启用调试模式
define( 'WP_DEBUG_LOG', true ); // 将错误记录到 debug.log 文件
define( 'WP_DEBUG_DISPLAY', false ); // 禁止在前端显示错误
@ini_set( 'display_errors', 0 ); // 禁止 PHP 错误显示

elementor编辑器打不开问题, 排查流程:#

wordpress 已有用户删除的问题,#

导致删除此用户的之前上传或操作的内容资源不可见, 图片资源等等

开始使用#

相关概念#

wordpress是CMS内容管理系统, 本身并不具备电商销售功能,所以需要集成相关插件来实现

插件#

站点设置#

用户设置#

自定义站点#

wordpress 设置#

Astra主题自定义设置#

WooCommerce设置#

Wordpress Gutenberg(古腾堡)编辑器#

多语言#

优化专题#

AB- Cloak#

Cache#

Analytics#

SEO#

ADS#

其他#

数据库操作#

查询用户类型

SELECT
u.ID
FROM
wp_users u
INNER JOIN
wp_usermeta um ON u.ID = um.user_id
WHERE
um.meta_key = 'wp_capabilities'
AND (um.meta_value LIKE '%subscriber%')

文章处理相关sql

DELETE
FROM wp_comments
WHERE comment_type = 'order_note';
DELETE
FROM wp_postmeta
WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'shop_order');
DELETE
FROM wp_posts
WHERE post_type = 'shop_order';
DELETE
FROM wp_posts
WHERE post_type = 'post'
and post_author = 4;
DELETE
FROM wp_posts
WHERE post_status = 'trash';
SELECT *
FROM wp_posts
WHERE post_type = 'page'
AND post_status = 'publish';
select count(1)
from wp_posts
WHERE post_type = 'post'
and post_author = 4;
select *
from wp_postmeta
where post_id = '1007'
select *
from wp_users
delete
from wp_users
where ID = 4
select *
from wp_options
where option_id in (1, 2)
select *
from wp_posts
where guid like 'https://surpoutlets.com%'
select *
from wp_posts
where post_content like '%us.surpoutlets.com%'
select *
from wp_postmeta
where meta_value like '%us.surpoutlets.com%'
select *
from wp_posts
where id = '25482'
select *
from wp_posts
where post_parent = '25482'

价格处理相关sql

DELETE
FROM wp_woocommerce_order_itemmeta
where 1 = 1;
DELETE
FROM wp_woocommerce_order_items
where 1 = 1;
# 查询价格
select *
from wp_options
where option_name like '%_transient_wc_var_prices%'
and option_name in (select distinct concat('_transient_wc_var_prices_', w.post_parent)
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent = '25482');
# 先查单个产品
select id, post_title
from wp_posts
where post_type = 'product'
and post_parent = '0'
and guid like '%us.surpoutlets.com%';
# 查询单个产品的价格信息
select *
from wp_postmeta
where post_id in (select id
from wp_posts
where post_type = 'product'
and post_parent = '0'
and guid like '%us.surpoutlets.com%'
and id not in ('25482'))
and meta_key like '%price%'
and meta_value != '';
# 再查可变产品
select *
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent not in ('25482');
# 查询可变产品的价格信息
select *
from wp_postmeta
where post_id in (select w.id
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent not in ('25482'))
and meta_key like '%price%'
and meta_value != '';
# 查询可变产品的价格范围
select *
from wp_wc_product_meta_lookup
where product_id in (select w.id
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent not in ('25482'));
update wp_posts
set guid = replace(guid, 'https://us.surpoutlets.com', 'https://surpoutlets.com')
where guid like 'https://us.surpoutlets.com%';
update wp_posts
set post_content = replace(post_content, 'https://us.surpoutlets.com', 'https://surpoutlets.com')
where post_content like '%https://us.surpoutlets.com%'
update wp_postmeta
set meta_value = replace(meta_value, 'us.surpoutlets.com', 'surpoutlets.com')
where meta_value like '%us.surpoutlets.com%'
# 批量更新单个产品价格
update IGNORE wp_postmeta
set meta_value = round(cast(meta_value as integer) / 20, 0)
where post_id in (select id
from wp_posts
where post_type = 'product'
and post_parent = 0
and guid like '%us.surpoutlets.com%'
and id not in ('25482'))
and meta_key like '%price%'
and meta_value != '';
# 批量更新可变产品价格
update IGNORE wp_postmeta
set meta_value = round(cast(meta_value as decimal) / 20, 0)
where post_id in (select w.id
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent not in ('25482'))
and meta_key like '%_price%'
and meta_value != '';
# 可变产品需要更新价格区间
update ignore wp_wc_product_meta_lookup
set min_price = round(cast(min_price as decimal) / 20, 0),
max_price = round(cast(max_price as decimal) / 20, 0)
where product_id in (select w.id
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent not in ('25482'));
#删除可变产品的价格区间缓存
delete
from wp_options
where option_name in (select distinct concat('_transient_wc_var_prices_', w.post_parent)
from wp_posts w
join wp_posts w1 on w1.id = w.post_parent
where w1.post_type = 'product'
and w.post_type = 'product_variation'
and w.guid like '%us.surpoutlets.com%'
and w.post_parent not in ('25482'));
commit;
rollback;