登录drupal管理员
One of the struggles that developers face when moving to Drupal 8 is the lack of best practices in deploying Drupal sites. The Challenges in deployment revolve around Dependency Management, Drupal Contrib Modules/Themes, Configuration Management, and of course Code Base.
开发人员迁移到Drupal 8时面临的困难之一是缺乏部署Drupal站点的最佳实践。 部署中的挑战围绕着依赖性管理,Drupal Contrib模块/主题,配置管理,当然还有代码库。
Drupal 7 has no such problems. But ahhh, Drupal 8 comes with lots of stuff to manage. One of the biggest changes in Drupal 8 is the adoption of Composer. Good things come at a price.
Drupal 7没有这样的问题。 但是啊,Drupal 8有很多东西要管理。 Drupal 8中最大的变化之一就是采用了Composer。 好东西是有代价的。
We will use one codebase for one Drupal site and use Git for version control and deployment.
我们将为一个Drupal站点使用一个代码库,并使用Git进行版本控制和部署。
作曲家 (Composer)
Composer is a dependency manager for PHP (like npm for Node or pip for Python). Drupal core uses Composer to manage core dependencies like Symfony components and Guzzle. Composer allows us to systematically manage a list of dependencies and their subsidiary dependencies. Composer installs these dependencies via a manifest file called composer.json.
Composer是PHP的依赖项管理器(如Node的npm或Python的pip)。 Drupal核心使用Composer来管理Symfony组件和Guzzle等核心依赖项。 Composer允许我们系统地管理依赖项及其附属依赖项的列表。 Composer通过名为composer.json的清单文件安装这些依赖项。
This composer.json file contains the dependencies that the project requires. You can install it by running
此composer.json文件包含项目所需的依赖项。 您可以通过运行来安装它
composer install
for the first time. It locates, downloads, validates, and loads the packages. It also ensures that exactly the right versions for each package are used, and maintains the log file called composer.lock.
首次。 它查找,下载,验证和加载程序包。 它还确保为每个软件包使用完全正确的版本,并维护名为composer.lock的日志文件。
Note: always commit your composer.lock file, because it contains the exact version of the dependencies that you have defined in the project.
注意:始终提交composer.lock文件,因为它包含您在项目中定义的依赖项的确切版本。
If you want to update any specific package, it’s a good practice to run this command:
如果要更新任何特定的程序包,则运行此命令是一个好习惯:
composer update package/package-name
You should never run composer update, because composer will try to update every single dependency. This can cause problems to your site.
您永远不要运行composer update,因为composer会尝试更新每个依赖项。 这可能会导致您的网站出现问题。
Drupal作曲家 (Drupal Composer)
drupal-composer/drupal-project is the life-saver project for managing your site dependencies with Composer.
drupal-composer / drupal-project是用于通过Composer管理站点依赖项的救生项目。
To install this project template, run this command:
要安装此项目模板,请运行以下命令:
composer create-project drupal-composer/drupal-project:8.x-dev drupal8 — stability dev — no-interaction
It will automatically install a Drupal site with all the dependencies. It will also install Drupal console and Drush locally.
它将自动安装具有所有依赖项的Drupal站点。 它还将在本地安装Drupal控制台和Drush 。
Composer is one of the fastest ways to install dependencies, as it caches the dependencies and loads data from the cache the next time.
Composer是安装依赖项的最快方法之一,因为它会缓存依赖项并在下一次从缓存中加载数据。
目录结构 (Directory structure)
It is different from the Drupal directory structure. You can reassemble web directory with the public directory which contains Drupal files. All third party dependencies are outside the web folder.
它与Drupal目录结构不同。 您可以使用包含Drupal文件的公共目录重新组装Web目录。 所有第三方依赖项都在Web文件夹之外。
You can install any Drupal Modules, themes, and profiles through composer which will be downloaded in the contrib folder inside the modules, themes, and profiles, respectively. In this way, the composer.lock file will have a record of all the Drupal contrib modules along with the third party dependencies.
您可以通过composer安装任何Drupal模块,主题和配置文件,这些文件将分别下载到模块,主题和配置文件内的contrib文件夹中。 这样,composer.lock文件将具有所有Drupal contrib模块以及第三方依赖项的记录。
To download any modules and themes using composer, run the following:
要使用composer下载任何模块和主题,请运行以下命令:
composer require drupal/mediumish_blog
# For installing theme, we will use drupal console
drupal theme:install mediumish_blog
吉特尼奥雷 (Gitignore)
As are all the dependencies and Drupal contrib modules, themes are managed by composer. Therefore, we will not push this content to Git.
与所有依赖项和Drupal contrib模块一样,主题由作曲家管理。 因此,我们不会将此内容推送到Git。
# Ignore directories generated by Composer /drush/contrib /vendor/ /web/core/ /web/modules/contrib/ /web/themes/contrib/ /web/profiles/contrib/ /web/libraries/
# Ignore sensitive information /web/sites/*/settings.php /web/sites/*/settings.local.ph
配置管理 (Configuration management)
Deployment and configuration management are common actions of a project life cycle. We have installed various modules and configured our local site, but our production site has no such configuration.
部署和配置管理是项目生命周期中的常见操作。 我们已经安装了各种模块并配置了我们的本地站点,但是我们的生产站点没有这样的配置。
In Drupal 7, we had the features module, which is used to sync configuration. But Drupal 8 has an inbuilt solution for managing configurations. This allows you to export complete website configurations and store them in YAML files. Exported files can be imported to another website with the same result.
在Drupal 7中,我们具有功能模块,该模块用于同步配置。 但是Drupal 8具有用于管理配置的内置解决方案。 这使您可以导出完整的网站配置,并将其存储在YAML文件中。 导出的文件可以导入到另一个具有相同结果的网站。
Drupal’s configuration system helps to solve the config files synchronization problem in two ways: a unified way to store configuration, and a process to import/export changes between instances of the same site.
Drupal的配置系统以两种方式帮助解决配置文件同步问题:统一的方式来存储配置,以及在同一站点的实例之间导入/导出更改的过程。
如何同步配置文件 (How to synchronize config files)
Open /web/sites/default/settings.php and set $config_directories[‘sync’]
打开/web/sites/default/settings.php并设置$ config_directories ['sync']
$config_directories[‘sync’] = ‘../config/sync’;
It’s a good practice to store config files outside of the web directory to avoid making them accessible from the Internet.
好的做法是将配置文件存储在Web目录之外,以避免从Internet访问它们。
Now use the Drupal console to export the configuration:
现在使用Drupal控制台导出配置:
drupal config:export
# import on prod server
drupal config:import
Note: the production and local Drupal sites should have same UUIDs. Check here for more info.
注意:生产站点和本地Drupal站点应具有相同的UUID。 在此处查看更多信息。
Note: Drupal configuration management has a bug — custom blocks data should neither be imported nor be exported. View the Issue Link here.
注意: Drupal配置管理有一个错误-自定义块数据既不能导入也不能导出。 在此处查看问题链接。
吉特 (Git)
We will use Git to add, commit, and push the local site’s data with all the configuration. This will be pulled from the prod server/site. Let’s see the flow:
我们将使用Git通过所有配置添加,提交和推送本地站点的数据。 这将从产品服务器/站点中拉出。 让我们看一下流程:
# Local
git add .
git commit -m”Add commit message”
git push origin HEAD
# Server
git pull otigin HEAD
composer install # to install any new dependencies, drupal contrib modules, themes
drupal config:import # to import the configuration
drupal cache:rebuild all # rebuild the cache
更新模块,主题和配置文件 (Update modules, themes, and profiles)
composer update drupal/mediumish_blog
drupal update:execute mediumish_blog
drupal update:execute all
更新Drupal核心 (Update Drupal core)
Generally, we face problems in updating the Drupal core. But composer has a simple way to manage this, too:
通常,我们在更新Drupal核心时会遇到问题。 但是作曲家也有一种简单的方法来管理它:
composer update drupal/core — with-dependencies
It will update the Drupal core and all its associated dependencies.
它将更新Drupal核心及其所有相关的依赖项。
管理环境配置 (Managing environment configuration)
My favorite aspect of Drupal, as a developer, is the ability to manage different environment configurations. This can be done by using the vlucas/phpdotenv module, which also comes with the Drupal composer template.
作为开发人员,Drupal最喜欢的方面是管理不同环境配置的能力。 这可以通过使用vlucas / phpdotenv模块完成,该模块也随Drupal作曲家模板一起提供。
Anything that is likely to change between deployment environments — such as database credentials or credentials for 3rd party services — should be extracted from the code into environment variables. Basically, an .env
file is an easy way to load custom configuration variables that your application needs without having to modify any other files.
在部署环境之间可能发生任何更改的所有内容,例如数据库凭据或第三方服务的凭据,都应从代码中提取到环境变量中。 基本上, .env
文件是加载应用程序所需的自定义配置变量的简便方法,而无需修改任何其他文件。
Rename .env.example to .env file and add all the credentials as a key-value pair in .env file.
将.env.example重命名为.env文件,并将所有凭据添加为.env文件中的键值对。
load.environment.php file in the root will load this .env file and make it available for you.
根目录中的load.environment.php文件将加载此.env文件并使之可供您使用。
如何使用.env文件 (How to use .env file)
Open /web/sites/default/setting.php and add this set of code:
打开/web/sites/default/setting.php并添加以下代码集:
$databases[‘default’][‘default’] = [ ‘database’ => getenv(‘MYSQL_DATABASE’), ‘driver’ => ‘mysql’, ‘host’ => getenv(‘MYSQL_HOSTNAME’), ‘namespace’ => ‘Drupal\\Core\\Database\\Driver\\mysql’, ‘password’ => getenv(‘MYSQL_PASSWORD’), ‘port’ => getenv(‘MYSQL_PORT’), ‘prefix’ => ‘’, ‘username’ => getenv(‘MYSQL_USER’), ];
Open .env file and set the following credentials:
打开.env文件并设置以下凭据:
MYSQL_DATABASE=’db_name’ MYSQL_HOSTNAME=’localhost’ MYSQL_PASSWORD=’secret’ MYSQL_PORT=’3306' MYSQL_USER=’root’
Now, as all our credentials are stored in the .env file, we can push our settings.php to the server and manage its configuration through .env file.
现在,由于所有凭据都存储在.env文件中,因此我们可以将settings.php推送到服务器,并通过.env文件管理其配置。
We generally enable twig debugging while in development and disable upon production. This can also be done easily and smoothly through an .env file.
我们通常在开发时启用树枝调试,并在生产时禁用。 您也可以通过.env文件轻松,顺利地完成此操作。
Add a new key value pair in .env file:
在.env文件中添加一个新的键值对:
APP_ENV=’local’
Now copy web/sites/example.settings.local.php to web/sites/default/settings.local.php and add this code in web/sites/development.services.yml under parameters:
现在,将web / sites / example.settings.local.php复制到web / sites / default / settings.local.php并将此代码添加到web / sites / development.services.yml中的参数下:
twig.config: debug: true auto_reload: true cache: false
Now open web/sites/default/settings.php and add this code:
现在打开web / sites / default / settings.php并添加以下代码:
$env = getenv(‘APP_ENV’);
$base_path = $app_root . ‘/’ . $site_path; $settingsFile = $base_path . ‘/settings.’ . $env . ‘.php’;
if (file_exists($settingsFile)) { include $settingsFile; }
So, in this way, if you set your APP_ENV=’local’, Twig debug will be enabled. Upon production, you can disable by setting APP_ENV=’prod’. You can also configure different configurations for different environments.
因此,以这种方式,如果您将APP_ENV ='local'设置为,则会启用Twig调试。 生产后,可以通过设置APP_ENV ='prod'来禁用。 您还可以为不同的环境配置不同的配置。
结论 (Conclusion)
Drupal 8 offers a built-in solution for exporting and importing site configurations, which is way better than what you could do in D7. Dependencies and contrib modules/themes are managed by the composer itself.
Drupal 8提供了用于导出和导入站点配置的内置解决方案,这比您在D7中可以做的更好。 依赖关系和贡献模块/主题由作曲家自己管理。
It’s not yet perfect, as there is no standard approach, but the workflow described above is a simple and efficient solution. You can define your own workflow based on your needs.
由于尚无标准方法,因此它还不是完美的,但是上述工作流程是一种简单而有效的解决方案。 您可以根据需要定义自己的工作流程。
For reference, I have pushed the code to this repo.
作为参考,我已将代码推送到此repo 。
I hope you have found this article useful. I would love to hear your feedback :)
希望本文对您有所帮助。 我很想听听您的反馈意见:)
This article is from my own blog. To read more such articles, follow it here.
本文来自我自己的博客。 要阅读更多此类文章,请在此处关注 。
翻译自: https://www.freecodecamp.org/news/the-best-way-to-manage-your-drupal-workflow-ade9525a84c0/
登录drupal管理员