[TOC]
文档里明确写了不支持 Windows ,但还是可以在 Windows 里安装的,实在不行就用 docker-desktop 或 wsl2 https://experienceleague.adobe.com/en/docs/commerce-operations/installation-guide/system-requirements
需要注意的事项
bcmath
ctype
curl
dom
fileinfo
filter
gd
hash
iconv
intl
json
libxml
mbstring
openssl
pcre
pdo_mysql
simplexml
soap
sockets
sodium
tokenizer
xmlwriter
xsl
zip
zlib
post_max_size = 128M
memory_limit = 2048M
upload_max_filesize = 64M
short_open_tag = On
max_execution_time = 900
max_input_time = 300
max_input_vars = 500
realpath_cache_size=10M
realpath_cache_ttl=7200
调整这两个值能有效地提升性能
PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS
大概就是在环境变量里设置
set PHP_FCGI_MAX_REQUESTS=1000
set PHP_FCGI_CHILDREN=32
下载源码
.
,命令最后的参数是安装目录, .
是安装到当前目录 composer create-project --repository=https://repo.magento.com/ magento/project-community-edition=2.4.6 .
通过 github
clone 的命令
clone 整个仓库后再切换分支
git clone https://github.com/magento/magento2.git .
git pull
git switch 2.4.6-p1
只 clone 一个分支
git clone -b 2.4.6-p1 https://github.com/magento/magento2.git --depth=1 .
切换到对应的 tag
git pull
git checkout 2.4.5
git clone -b 2.4.6-p7 --single-branch --no-tags https://github.com/magento/magento2.git --depth=1 .
composer install
使用 patch 修改源码
create database magento2ce;
php bin/magento setup:install \
--base-url=http://localhost-magento.com/ \
--db-host=localhost \
--db-name=magento2ce \
--db-user=root \
--db-password=1234 \
--admin-firstname=admin \
--admin-lastname=admin \
--admin-email=admin@admin.com \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--search-engine=elasticsearch7 \
--elasticsearch-host=localhost \
--elasticsearch-port=9200 \
--elasticsearch-index-prefix=magento2
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush
修改 nginx 的配置
upstream fastcgi_backend {
server 127.0.0.1:9001;
}
server {
listen 80;
# listen 443 ssl http2;
# ssl_certificate ./cert/localhost-magento.com/domain.crt;
# ssl_certificate_key ./cert/localhost-magento.com/rsa_private_key.pem;
access_log logs/localhost-magento.access.log;
error_log logs/localhost-magento.error.log;
server_name localhost-magento.com;
set $MAGE_ROOT C:/code/magento-community; # 这里是 magento 的根目录
set $MAGE_DEBUG_SHOW_ARGS 1;
include C:/code/magento-community/nginx.conf.sample; # 这里是 magento 的根目录里的 nginx.conf.sample
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
php bin/magento deploy:mode:show
php bin/magento deploy:mode:set developer
php bin/magento sampledata:deploy
git clone https://github.com/magento/magento2-sample-data
php -f /d/magento2-sample-data/dev/tools/build-sample-data.php -- --sample-data-source="/d/magento2-sample-data" --ce-source="/d/magento-ce" --command=link
<!-- 示例数据的图片路径好像还有一点问题,但又不是不能用 --> php bin/magento setup:static-content:deploy -f
php bin/magento setup:upgrade
http://localhost-magento.com
http://localhost-magento.com/admin
php bin/magento module:disable Magento_TwoFactorAuth
php bin/magento cache:flush
需要注意的事项
var/log
,安装过程中遇到什么问题可以在这里找日志看app/etc/env.php
里查看 admin 的 url ,运行完安装命令后也会输出 admin 的 url 'backend' => [
'frontName' => 'admin_1a3uev'
],
admin >> STORE >> Configuration >> SALES >> Payment Methods
php -S 127.0.0.1:8082 -t ./pub/ ./phpserver/router.php
php bin/magento cache:clean
php bin/magento indexer:reindex
php bin/magento cache:flush
rm -rf var/di/* var/generation/* var/cache/* var/page_cache/* var/view_preprocessed/* var/composer_home/cache/*
rm -rf generated/code/* generated/metadata/*
rm -rf pub/static/*
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '2',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'log_level' => '4',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
'first_lifetime' => '600',
'bot_first_lifetime' => '60',
'bot_lifetime' => '7200',
'disable_locking' => '0',
'min_lifetime' => '60',
'max_lifetime' => '2592000'
]
],
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379'
],
]
]
],
'system' => [
'default' => [
'dev' => [
'js' => [
'merge_files' => '0',
'minify_files' => '0',
'enable_js_bundling' => '0'
],
'css' => [
'minify_files' => '0',
'merge_css_files' => '0'
],
]
]
],
opcache.enable_cli=1
composer dumpautoload -o
后速度也有提升php bin/magento setup:di:compile
后面php bin/magento setup:static-content:deploy -f
前面 php bin/magento setup:static-content:deploy
vendor/magento/framework
lib/internal/Magento/Framework
没有url重写 /index.php/admin
有url重写 /admin
magento2 的官网推荐使用 nginx 做 es 的反向代理,这样就可以给 es 加上 http 认证
配置 vscode 的 xml 文件语法高亮
XML Language Support by Red Hat
php bin/magento dev:urn-catalog:generate --ide vscode -- .vscode/misc.xml
.vscode/settings.json
里加上这几句 {
"xml.catalogs": [
".vscode\\misc.xml"
],
"xml.validation.resolveExternalEntities": true,
"xml.codeLens.enabled": true,
}
在 vscode 中关掉 git 的自动刷新
"git.autorefresh": false
在 vscode 中可以用这样的配置来忽略一些文件的解析
"intelephense.references.exclude": [
"**/vendor/**",
"generated",
"dev",
"var",
"pub/media",
"pub/static"
]
禁用 es 相关的模块,用这个库来替换
https://github.com/swissup/module-search-mysql-legacy
因为 Elasticsearch 的存在使得门槛高了不少。笔者感觉奥多比正在抛弃中小用户。 笔者感到很奇怪,没有 redis 也可以安装,但没有 Elasticsearch 却不行。
就是安装命令里删掉这几个参数
--search-engine
--elasticsearch-host
--elasticsearch-port
--elasticsearch-index-prefix
就是安装命令里加上这一个参数
--disable-modules=Magento_Elasticsearch,Magento_Elasticsearch7,Magento_OpenSearch
2.4 的小版本里,有好几个和 es 相关的模块
Magento_Elasticsearch
Magento_Elasticsearch6
Magento_Elasticsearch7
Magento_InventoryElasticsearch
Magento_OpenSearch
php bin/magento setup:install \
--base-url=http://localhost-magento.com/ \
--db-host=localhost \
--db-name=magento2ce \
--db-user=root \
--db-password=1234 \
--admin-firstname=admin \
--admin-lastname=admin \
--admin-email=admin@admin.com \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=Asia/Shanghai \
--use-rewrites=0 \
--disable-modules=Magento_Elasticsearch,Magento_Elasticsearch7,Magento_OpenSearch
composer require swissup/module-search-mysql-legacy --prefer-source --ignore-platform-reqs
php bin/magento module:enable Swissup_SearchMysqlLegacy Swissup_Core
php bin/magento setup:upgrade --safe-mode=1
php bin/magento setup:di:compile
php bin/magento config:set catalog/search/engine 'lmysql'
AdminPortal > Store > Settings > Configuration > CATALOG > Catalog > Catalog Search > Search Engine
php bin/magento indexer:reindex
php bin/magento maintenance:enable
php bin/magento module:disable Magento_Elasticsearch Magento_Elasticsearch7 Magento_OpenSearch
2.4 的小版本里,有好几个和 es 相关的模块
最好先用 php bin/magento module:status 看看有哪些和 es 相关的模块再禁用
php bin/magento maintenance:disable
patch 文件的内容
--- lib/internal/Magento/Framework/Image/Adapter/Gd2.php 2023-12-19 17:16:58.149464800 +0800
+++ lib/internal/Magento/Framework/Image/Adapter/Gd2.php 2023-12-19 17:22:09.469880100 +0800
@@ -91,10 +91,12 @@ class Gd2 extends AbstractAdapter
*/
private function validateURLScheme(string $filename) : bool
{
- $allowed_schemes = ['ftp', 'ftps', 'http', 'https'];
- $url = parse_url($filename);
- if ($url && isset($url['scheme']) && !in_array($url['scheme'], $allowed_schemes)) {
- return false;
+ if (!file_exists($filename)) { // if file not exist
+ $allowed_schemes = ['ftp', 'ftps', 'http', 'https'];
+ $url = parse_url($filename);
+ if ($url && isset($url['scheme']) && !in_array($url['scheme'], $allowed_schemes)) {
+ return false;
+ }
}
return true;
--- lib/internal/Magento/Framework/View/Element/Template/File/Validator.php 2023-12-19 17:17:02.495932300 +0800
+++ lib/internal/Magento/Framework/View/Element/Template/File/Validator.php 2023-12-19 17:23:23.889091300 +0800
@@ -135,7 +135,8 @@ class Validator
if (!is_array($directories)) {
$directories = (array)$directories;
}
- $realPath = $this->fileDriver->getRealPath($path);
+ // $realPath = $this->fileDriver->getRealPath($path);
+ $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
foreach ($directories as $directory) {
if ($directory !== null && 0 === strpos($realPath, $directory)) {
return true;
--- lib/internal/Magento/Framework/App/StaticResource.php 2023-12-19 17:16:54.560803000 +0800
+++ lib/internal/Magento/Framework/App/StaticResource.php 2023-12-19 17:21:47.440984600 +0800
@@ -285,6 +285,7 @@ class StaticResource implements \Magento
*/
private function isThemeAllowed(string $theme): bool
{
+ $theme = str_replace('\\', '/', $theme); // fix windows path
return in_array($theme, array_keys($this->themePackageList->getThemes()));
}
}
--- lib/internal/Magento/Framework/Interception/PluginListGenerator.php 2023-12-19 17:16:58.383831000 +0800
+++ lib/internal/Magento/Framework/Interception/PluginListGenerator.php 2023-12-19 17:22:39.196325500 +0800
@@ -153,7 +153,8 @@ class PluginListGenerator implements Con
if (false === in_array($scope, $this->scopePriorityScheme, true)) {
$this->scopePriorityScheme[] = $scope;
}
- $cacheId = implode('|', $this->scopePriorityScheme) . "|" . $this->cacheId;
+ // $cacheId = implode('|', $this->scopePriorityScheme) . "|" . $this->cacheId;
+ $cacheId = implode('-', $this->scopePriorityScheme) . "-" . $this->cacheId;
[
$virtualTypes,
$this->scopePriorityScheme,
这是针对使用 github 新建的项目,如果是通过 composer 新建的项目,要替换一些路径
sed -i 's/lib\/internal\/Magento\/Framework\//vendor\/magento\/framework\//g' windows.patch;
执行 patch 的命令,要在项目的根目录运行
patch --verbose -p0 --no-backup-if-mismatch < windows.patch;
生成 patch 文件的方式
\define('PATCH_FILE', 'windows.patch');
$targetArr = [
'lib/internal/Magento/Framework/Image/Adapter/Gd2.php',
'lib/internal/Magento/Framework/View/Element/Template/File/Validator.php',
'lib/internal/Magento/Framework/App/StaticResource.php',
'lib/internal/Magento/Framework/Interception/PluginListGenerator.php',
];
echo "
先确定好哪些文件需要修改
复制一份
修改文件
生成 patch 文件,patch 的文件结尾要有一个空行
替换 patch 文件里的路径
在项目根目录执行 patch
";
echo PHP_EOL . PHP_EOL;
$cpArr = array_map(function($item) {
return 'cp ' . $item . ' ' . str_replace('.php', '2.php', $item);
}, $targetArr);
$diffArr = array_map(function($item) {
return str_replace('cp ', 'diff -up ', $item) . ' >> ' . PATCH_FILE;
}, $cpArr);
$sedArr = array_map(function($item) {
$item = str_replace('/', '\/', $item);
$a = str_replace('.php', '2.php', $item);
$b = $item;
return sprintf("sed -i 's/%s/%s/g' " . PATCH_FILE, $a, $b);
}, $targetArr);
array_map(function($item) {
echo join(';' . PHP_EOL, $item) . PHP_EOL . PHP_EOL;
return $item;
}, [$cpArr, $diffArr, $sedArr]);
echo 'patch --verbose -p0 --no-backup-if-mismatch < ' . PATCH_FILE . PHP_EOL;
<!--
cat > windows.patch <<- 'EOF'
EOF
cat windows.patch; \
patch --verbose -p0 --no-backup-if-mismatch < windows.patch;
if [ -d lib/internal/Magento/Framework ]; \
then patch -p0 --no-backup-if-mismatch -d lib/internal/Magento/Framework < windows.patch; \
else patch -p0 --no-backup-if-mismatch -d vendor/magento/framework < windows.patch; fi
sed -i 's/lib\/internal\/Magento\/Framework\///g' windows.patch
sed -i 's/lib\/internal\/Magento\/Framework\//vendor\/magento\/framework\//g' windows.patch
sed -i 's/vendor\/magento\/framework\//lib\/internal\/Magento\/Framework\//g' windows.patch
php -a <<- 'EOF'
EOF
-->
安装指南 https://experienceleague.adobe.com/zh-hans/docs/commerce-operations/installation-guide/overview
开发文档 https://developer.adobe.com/commerce/php/development/
github https://github.com/magento/magento2
magento 相关的博客
<!-- 通过 alias 设置当前命令行里的 php 版本 alias php="/c/Users/a/dev/php-7.4.29-Win32-vc15-x64/php.exe"; \ alias composer="php /c/Users/a/dev/php-7.4.29-Win32-vc15-x64/composer.phar"; 暂时最完善的构建脚本 startAtTimestamp=$(date +%s); \ startAt=$(date -d @$startAtTimestamp +%FT%T%:z); \ echo startAt: $startAt; \ rm -rf var/di/* var/generation/* var/cache/* var/page_cache/* var/view_preprocessed/* var/composer_home/cache/* var/tmp/* && \ rm -rf generated/code/* generated/metadata/* pub/static/* && \ php bin/magento setup:upgrade && \ php bin/magento setup:di:compile && \ php bin/magento setup:static-content:deploy -f && \ php bin/magento indexer:reindex && \ php bin/magento cache:flush; \ endAtTimestamp=$(date +%s); \ endAt=$(date -d @$endAtTimestamp +%FT%T%:z); \ seconds=$(echo $startAtTimestamp $endAtTimestamp | awk '{printf("%d", $2-$1)}') ; \ hour=$(echo $seconds | awk '{printf("%d", $1/3600)}'); \ min=$(echo $seconds $hour | awk '{printf("%d", ($1-$2*3600)/60)}'); \ sec=$(echo $seconds $hour $min | awk '{printf("%d", $1-$2*3600-$3*60)}'); \ echo startAt: $startAt; \ echo endAt: $endAt; \ echo $hour $min $sec | awk '{printf("Runtime: %02d:%02d:%02d\n", $1, $2, $3)}'; \ echo $startAtTimestamp $endAtTimestamp | awk '{printf("Runtime: %d\n", $2-$1)}' 查看当前版本 php bin/magento --version magento 的备份命令 备份功能自2.1.16、2.2.7和2.3.0起已弃用。我们建议研究其他备份技术和二进制备份工具(如Percona XtraBackup)。 默认情况下备份功能会禁用,要先启用备份功能 php bin/magento config:set system/backup/functionality_enabled 1 备份和恢复备份都需要先进入维护模式 备份 bin/magento setup:backup [--code] [--media] [--db] 查看已有的备份 php bin/magento info:backups:list 恢复备份 php bin/magento setup:rollback [-c|--code-file="<name>"] [-m|--media-file="<name>"] [-d|--db-file="<name>"] 选项 含义 备份文件名和位置 --code 备份文件系统(var和pub/static目录除外)。 var/backups/<timestamp>/_filesystem.tgz --media 备份pub/media目录 var/backups/<timestamp>/_filesystem_media.tgz --db 备份数据库。 var/backups/<timestamp>/_db.sql 新建一个这样的文件就是进入维护模式了,把这个文件删了就是退出维护模式了 var/.maintenance.flag 如何升级 magento 升级 magento 时要注意些什么 修改 base-url php bin/magento setup:store-config:set --base-url="http://localhost-magento.com/" SELECT * FROM magento245.core_config_data WHERE value like '%localhost-magento.com%' 查看所有模块 php bin/magento module:status 有空就把 2.3 的安装过程删掉,全部换成 2.4 的,2.3 也算过时了 apache的配置 2.4 2.2 和 2.4 的配置会有一点不一样 需要在默认设置的基础上加上这三个模块 modules/mod_rewrite.so modules/mod_version.so php_module 这是站点的配置 <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "D:\magento-ce\pub" ServerName localhost-magento.com <Directory /> Options FollowSymlinks AllowOverride All Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> ErrorLog "logs/localhost-magento.com-error.log" CustomLog "logs/localhost-magento.com-access.log" common </VirtualHost> openssl req -new -x509 -days 365 -nodes -out apache-selfsigned.crt -keyout apache-selfsigned.key <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "${SRVROOT}" ServerName localhost-magento.com ServerAlias localhost-magento.com RewriteEngine On RewriteCond "%{HTTPS}" "!=on" RewriteRule ^/?(.*)$ https://%{HTTP_HOST}/$1 [L,R=302] </VirtualHost> <VirtualHost *:443> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "D:\magento-ce\pub" ServerName localhost-magento.com ServerAlias localhost-magento.com SSLEngine on SSLCertificateFile "${SRVROOT}/conf/ssl/localhost-magento.com/apache-selfsigned.crt" SSLCertificateKeyFile "${SRVROOT}/conf/ssl/localhost-magento.com/apache-selfsigned.key" Protocols h2 h2c http/1.1 http/1.0 # Force clients from the Internet to use HTTPS # RewriteEngine on # RewriteCond "%{REMOTE_ADDR}" "!^192\.168\.1\.[0-9]+$" # RewriteCond "%{HTTPS}" "!=on" # RewriteRule "." "-" [F] <Directory "/"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> ErrorLog "logs/localhost-magento.com-error.log" CustomLog "logs/localhost-magento.com-access.log" common </VirtualHost> 用于替换 nginx配置中magento根目录的命令 newpath=$(echo "C:\Users\a\dev\magento-core\2647-magento" | sed 's/\\/\\\\/g'); \ confpath=$(cygpath -u "C:\Users\a\dev\nginx-1.25.3\conf\nginx.conf"); \ oldpath=$(cat $confpath | grep -o -P "(?<=MAGE_ROOT)(.*)(?=;)"); \ oldpath=$(echo $oldpath | sed 's/\\/\\\\/g'); \ oldpath=$(echo $oldpath | xargs ); \ oldpath=$(echo $oldpath | sed 's/\\/\\\\/g'); \ echo "confpath: " $confpath; \ echo "oldpath: " $oldpath; \ echo "newpath: " $newpath; \ sed -i 's/'$oldpath'/'$newpath'/g' $confpath; \ cat $confpath; 用于替换 apache配置中magento根目录的命令 newpath=$(echo "D:\code\magento-core\2121-magento" | sed 's/\\/\//g'); \ confpath=$(cygpath -u "C:\Users\a\dev\Apache24\conf\extra\httpd-vhosts-magentoee.conf"); \ oldpath=$(cat $confpath | grep -o -P '(?<=DocumentRoot ")(.*)(?=\/pub")'); \ oldpath=$(echo $oldpath | sed 's/\//\\\//g'); \ newpath=$(echo $newpath | sed 's/\//\\\//g'); \ echo "confpath: " $confpath; \ echo "oldpath: " $oldpath; \ echo "newpath: " $newpath; \ sed -i 's/'$oldpath'/'$newpath'/g' $confpath; \ cat $confpath; 额外新建了一个文件 conf\extra\httpd-vhosts-magentoee.conf 并且在 conf/extra/httpd-vhosts.conf 里引入 Include conf/extra/httpd-vhosts-magentoee.conf -->