我们今天使用 Composer 安装 Drupal,并且搭建开发环境,做个详细的记录。
- IDE:PyCharm
- CMS:Drupal 8(9也可以,但是目前有很多插件不支持)
- PHP环境:PHP7.4 + Composer 2
- 开发环境:WSL2(Ubuntu 20.04) + Docker(3.2.2)
- 系统环境:Windows 10
相关目录:
- 项目目录
APP_DIR
环境配置
Windows 配置
安装好 PyCharm、Docker
WSL2 安装配置
WSL 是 Windows Subsystem for Linux 的缩写。
详细配置参考(待写)
系统选择 Ubuntu 20.04。
Docker 配置

这里,我们把 Ubuntu-20.04 勾选上。
创建 Drupal 项目文件
我们由于我们使用的是 Composer 安装 Drupal,所以先要准备好 PHP 和 Composer,注意要在 WSL(Ubuntu)中安装。
PHP 和 Composer 的安装参考:Ubuntu 20.04 PHP 7.4 部署
使用 Composer 安装 Drupal 参考官方文档:Using Composer to Install Drupal and Manage Dependencies
在 Ubuntu 中,进入准备生成项目的路径,输入命令:
composer create-project drupal/recommended-project:8.9.13 MY_SITE_NAME_DIR

创建项目成功页面。
添加 git 保存
新建 .gitignore 文件,并将其余几个文件都放到 git 提交。
# 自定义## IDE/.idea/.vscode## 挂载目录/volume## 下载模块和插件目录/web/modules/contrib/web/themes/contrib# Created by .ignore support plugin (hsz.mobi)### Drupal template# gitignore template for Drupal 8 projects## earlier versions of Drupal are tracked in `community/PHP/`# Ignore configuration files that may contain sensitive information/web/sites/*/*settings*.php/web/sites/*/*services*.yml# Ignore paths that may contain user-generated content/web/sites/*/files/web/sites/*/public/web/sites/*/private/web/sites/*/files-public/web/sites/*/files-private# Ignore paths that may contain temporary files/web/sites/*/translations/web/sites/*/tmp/web/sites/*/cache# Ignore testing related files/web/sites/simpletest# Ignore drupal core (if not versioning drupal sources)/web/core/vendor/web/modules/README.txt/web/profiles/README.txt/web/themes/README.txt/web/sites/README.txt/web/sites/example.sites.php/web/sites/example.settings.local.php/web/sites/development.services.yml/web/.csslintrc/web/.editorconfig/web/.eslintignore/web/.eslintrc.json/web/.gitattributes/web/.ht.router.php/web/.htaccess/web/autoload.php/web/example.gitignore/web/index.php/web/INSTALL.txt/web/LICENSE.txt/web/README.txt/web/robots.txt/web/update.php/web/web.config
添加 .dockerignore ,减小不必要的打包本地文件体积,提高速度。排除文件基本与 .gitignore 相同,但要注意,路径要使用 web/core 而不是 /web/core 。
配置 PyCharm
使用 PyCharm 打开项目目录,菜单栏“文件” -> “设置”-> “PHP”
先按如上图所示,新建并选择 Ubuntu-20.04 作为 CLI解释器。

再按照如上图所示,选择并配置 composer.json 文件和解释器。
