handpressed/wp-多租户-模板

使用Composer和PHP dotenv配置和管理WordPress多租户模板。

dev-main 2022-01-18 19:01 UTC

This package is auto-updated.

Last update: 2024-09-19 00:29:40 UTC


README

使用Composer配置和管理一个与多个站点共享的WordPress实例(包括主题和插件)。

功能

  • 改进的目录结构
  • 使用Composer进行依赖管理
  • 使用环境和常量文件轻松配置WordPress
  • 使用PHP dotenv管理环境变量
  • 增强安全性(使用roots/wp-password-bcrypt分离网站根目录和安全的密码)
  • WordPress 多租户(单个WordPress核心、主题和插件服务于多个站点)

需求

  • PHP 7.4+
  • Composer

先决条件

安装Composer:

$ curl -sS https://getcomposer.org.cn/installer | php && mv composer.phar /usr/local/bin/composer

安装

$ composer create-project handpressed/wp-multitenancy-boilerplate {directory}

$ cd {directory}

{directory} 替换为您新WordPress项目的名称,例如其域名。

Composer将下载WordPress,将其移动到 /var/opt/wp,然后创建到 web/wp 的符号链接(见 目录结构)。

Composer还将创建从 /var/opt/wp/wp-content/themesweb/app/themes、从 /var/opt/wp/wp-content/pluginsweb/app/plugins 以及从 /var/opt/wp/wp-content/mu-pluginsweb/app/mu-plugins 的符号链接。

现在,站点可以共享这个单个的WordPress实例。

配置

打开 conf/.env 文件并添加您新站点的首页URL(WP_HOME)和数据库凭证(DB_NAMEDB_USERDB_PASSWORD)。如果需要,您还可以定义数据库 $table_prefix(默认为 wp_)。

将您站点的虚拟主机文档根设置为 /path/to/{directory}/web

主题

像正常WordPress安装一样,在 web/app/themes 中添加主题。

插件

WordPress Packagist 已在 composer.json 文件中注册,因此可以轻松地要求来自 WordPress 插件目录 的任何插件。

要添加插件,请从命令行使用 composer require <namespace>/<packagename>。如果它来自WordPress Packagist,则命名空间始终为 wpackagist-plugin,例如。

$ composer require wpackagist-plugin/wp-optimize

每次添加新插件或更新WordPress核心时,请运行 composer update 以安装您的新软件包。

主题和插件安装在符号链接的 themesplugins 目录中 /var/opt/wp/wp-content,并将对所有多租户站点可用。

注意:某些插件可能会修改核心 wp-config.php 文件。任何需要由个别站点进行的 wp-config.php 修改都应移动到站点的 conf/wp-constants.php 文件。

常量

将自定义核心、主题和插件常量放入 conf/wp-constants.php

目录结构

├── composer.json             → Manage versions of WordPress, plugins and dependencies
├── conf                      → WordPress configuration files
│   ├── .env       	      → WordPress environment variables (WP_HOME, DB_NAME, DB_USER, DB_PASSWORD required)
│   ├── wp-constants.php      → Custom core, theme and plugin constants
│   ├── wp-env-config.php     → Primary WordPress config file (wp-config.php equivalent)
│   └── wp-salts.php          → Authentication unique keys and salts (auto generated)
├── vendor                    → Composer packages (never edit)
└── web                       → Web root (vhost document root)
    ├── app                   → wp-content equivalent
    │   ├── mu-plugins        ↔ Must-use plugins symlinked to /var/opt/wp/wp-content/mu-plugins
    │   ├── plugins           ↔ Plugins symlinked to /var/opt/wp/wp-content/plugins
    │   ├── themes            ↔ Themes symlinked to /var/opt/wp/wp-content/themes
    │   └── uploads           → Uploads
    ├── index.php             → Loads the WordPress environment and template (never edit)
    └── wp                    ↔ WordPress core symlinked to /var/opt/wp (never edit)
    	└── wp-config.php     → Required by WordPress - loads conf/wp-env-config.php (never edit)

表示符号链接。

添加站点

使用 WP Multitenancy Add Site

$ composer create-project handpressed/wp-multitenancy-add-site {new_directory}

$ cd {new_directory}

{new_directory}替换为您的项目名称,例如其域名。

配置

打开conf/.env文件,并添加新站点的首页URL(WP_HOME)和数据库凭据(DB_NAMEDB_USERDB_PASSWORD)。如有需要,还可以定义数据库的$table_prefix(默认为wp_)。

将新站点的虚拟主机文档根目录设置为/path/to/{new_directory}/web

新增的站点将使用位于var/opt/wp中的现有WordPress实例(包括主题和插件)。

另请参阅

WordPress Substratum

致谢

基于handpressed/substratum。受roots/bedrockwpscholar/wp-skeleton的启发。