在Ubuntu24上安装部署magento 2.4
目录
前奏
rambo@ubuntu24-2:~$ cat /etc/apt/sources.list.d/ubuntu.sources
Types: deb
URIs: http://cn.archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 更新软件包存储库 & 升级现有软件包
rambo@ubuntu24-2:~$ sudo apt update -y && sudo apt upgrade -y
安装必备工具:
rambo@ubuntu24-2:~$ sudo apt install -y curl git zip unzip net-tools
.
.
.
如有报错
.
.
.
服务器堆栈安装
安装Apache/Nginx:本次使用Apache
rambo@ubuntu24-2:~$ sudo apt install apache2 -y
rambo@ubuntu24-2:~$ sudo systemctl enable apache2 && sudo systemctl restart apache2
rambo@ubuntu24-2:~$ sudo systemctl status apache2
PHP8.3安装
rambo@ubuntu24-2:~$ sudo apt install -y php8.3 \
php8.3-common php8.3-gd php8.3-mysql \
php8.3-xml php8.3-mbstring \
libapache2-mod-php8.3 php8.3-exif php8.3-bz2 \
php8.3-bcmath php8.3-intl php8.3-soap php8.3-zip php8.3-curl
rambo@ubuntu24-2:~$ php -v
PHP 8.3.6 (cli) (built: Mar 19 2025 10:08:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
# 更新 php.ini 文件
rambo@ubuntu24-2:~$ sudo vim /etc/php/8.3/apache2/php.ini
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 128M
max_execution_time = 3600
rambo@ubuntu24-2:~$ sudo systemctl restart apache2
设置MySQL或MariaDB数据库: Magento依赖于数据库
rambo@ubuntu24-2:~$ sudo apt install -y mysql-server
rambo@ubuntu24-2:~$ sudo systemctl enable mysql && sudo systemctl restart mysql
rambo@ubuntu24-2:~$ sudo systemctl status mysql
# 安全设置
rambo@ubuntu24-2:~$ sudo mysql_secure_installation
Press y|Y for Yes, any other key for No: y
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 # 自定义密码强度
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
# 配置数据库
rambo@ubuntu24-2:~$ sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Aa1122**';
mysql> exit
# 使用root用户登陆
rambo@ubuntu24-2:~$ mysql -uroot -p
Enter password: # 密码是上一步定义的Aa1122**
mysql> CREATE DATABASE magentodb;
mysql> CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'Str0ngPa$$w0rd';
mysql> GRANT ALL PRIVILEGES ON magentodb.* TO 'magentouser'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
rambo@ubuntu24-2:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld] # 追加
log_bin_trust_function_creators = 1
rambo@ubuntu24-2:~$ sudo systemctl restart mysql
Composer安装: Magento 使用 Composer 进行包依赖管理
rambo@ubuntu24-2:~$ sudo apt install -y composer
rambo@ubuntu24-2:~$ composer --version
Composer version 2.7.1 2024-02-09 15:26:28
安装 Elasticsearch(或 OpenSearch)
rambo@ubuntu24-2:~$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
rambo@ubuntu24-2:~$ echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
rambo@ubuntu24-2:~$ sudo apt update -y && sudo apt install -y elasticsearch
rambo@ubuntu24-2:~$ sudo vim /etc/elasticsearch/elasticsearch.yml
# 添加/修改
cluster.name: magento
node.name: "node-1"
network.host: 127.0.0.1
http.port: 9200
xpack.security.enabled: false
rambo@ubuntu24-2:~$ sudo systemctl enable elasticsearch && sudo systemctl restart elasticsearch
# 验证
rambo@ubuntu24-2:~$ curl -X GET "localhost:9200/"
{
"name" : "node-1",
"cluster_name" : "magento",
"cluster_uuid" : "_na_",
"version" : {
"number" : "8.18.1",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "df116ec6455476a07daafc3ded80e2bb1a3385ed",
"build_date" : "2025-04-30T10:07:44.026929518Z",
"build_snapshot" : false,
"lucene_version" : "9.12.1",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
安装Magento
rambo@ubuntu24-2:~$ cd /var/www/html/
rambo@ubuntu24-2:/var/www/html$ sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento
以下是回显
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes # 输入yes
Creating a "magento/project-community-edition" project at "./magento"
Warning from repo.magento.com: You haven't provided your Magento authentication keys. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html
Authentication required (repo.magento.com):
Username: # 这是用户名
Password: # 这是密码,输入密码时不显示
Do you want to store credentials for repo.magento.com in /root/.config/composer/auth.json ? [Yn] Y # 输入Y
Installing magento/project-community-edition (2.4.8)
- Downloading magento/project-community-edition (2.4.8)
- Installing magento/project-community-edition (2.4.8): Extracting archive
Created project in /var/www/html/magento
Loading composer repositories with package information
....
.... # 以下信息是正常的
...No special rule applied for Magento_InventorySetupFixtureGenerator module
...Module Magento_InventoryShipping recognized as part of Magento Multi Source Inventory implementation
...No special rule applied for Magento_InventoryShipping module
...Module Magento_InventoryShippingAdminUi recognized as part of Magento Multi Source Inventory implementation
...No special rule applied for Magento_InventoryShippingAdminUi module
...Module Magento_InventorySourceSelection recognized as part of Magento Multi Source Inventory implementation
...No special rule applied for Magento_InventorySourceSelection module
...Module Magento_InventorySwatchesFrontendUi recognized as part of Magento Multi Source Inventory implementation
...No special rule applied for Magento_InventorySwatchesFrontendUi module
...Module Magento_InventoryVisualMerchandiser recognized as part of Magento Multi Source Inventory implementation
...No special rule applied for Magento_InventoryVisualMerchandiser module
...Module Magento_InventoryWishlist recognized as part of Magento Multi Source Inventory implementation
...No special rule applied for Magento_InventoryWishlist module
107 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package laminas/laminas-config is abandoned, you should avoid using it. No replacement was suggested.
Package laminas/laminas-json is abandoned, you should avoid using it. No replacement was suggested.
Package laminas/laminas-loader is abandoned, you should avoid using it. No replacement was suggested.
Package laminas/laminas-text is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
137 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
PHP CodeSniffer Config installed_paths set to ../../magento/magento-coding-standard,../../magento/php-compatibility-fork,../../phpcsstandards/phpcsutils
No security vulnerability advisories found.
==========================================================================
注:
上述日志主要是 Composer 在安装或更新 Magento 相关依赖时的输出信息,内容包含:
识别了多个 Magento Multi Source Inventory (多源库存) 相关模块,但没有针对这些模块应用特殊规则。
新依赖增加了107个建议包,可以用 composer suggest 查看具体建议。
报告了几个 laminas 组件已经废弃(abandoned),建议避免使用,不过没有给出替代方案。
自动加载文件生成完成。
137个依赖包正在寻求资金支持,可以用 composer fund 查看详情。
PHP CodeSniffer 配置了几个代码规范路径。
没有检测到安全漏洞提示。
废弃组件:
laminas/laminas-config
laminas/laminas-json
laminas/laminas-loader
laminas/laminas-text
这些包被标记为 abandoned,意味着官方不再维护,且可能不再提供安全更新或新功能。
无替代建议:
日志中未给出明确替代方案,说明需要手动寻找官方文档或社区方案。
==========================================================================
# 设置权限
rambo@ubuntu24-2:/var/www/html$ sudo chown -R www-data:www-data /var/www/html/magento
# 进入 Magento 目录
rambo@ubuntu24-2:/var/www/html$ cd magento
# 安装依赖项: Composer会自动处理这个问题,但您可以根据需要运行更新:
rambo@ubuntu24-2:/var/www/html/magento$ sudo composer update
Continue as root/super user [yes]? yes # 输入yes
# 安装 Magento
rambo@ubuntu24-2:/var/www/html/magento$ sudo bin/magento setup:install \
--base-url=http://<DOMAIN_NAME> \
--db-host=localhost \
--db-name=magentodb \
--db-user=magentouser \
--db-password='PASSWORD' \
--admin-firstname=Admin \
--admin-lastname=User \
--admin-email=admin@admin.com \
--admin-user=admin \
--admin-password=AdminPass7788! \
--backend-frontname=admin \
--search-engine=opensearch \
--opensearch-host=localhost \
--opensearch-port=9200
以下是回显
[Progress: 1471 / 1472]
Indexing...
Indexing Error: Unknown 503 error from OpenSearch null
[Progress: 1472 / 1472]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin
Nothing to import.
# 启用Magento模块
rambo@ubuntu24-2:/var/www/html/magento$ sudo bin/magento setup:upgrade
....
....
Module 'Magento_PaypalGraphQl':
Module 'Magento_Persistent':
Module 'Magento_ProductAlert':
Module 'Magento_ProductVideo':
Module 'Magento_ServicesId':
Running data recurring...
Module 'Magento_CheckoutAgreements':
Module 'Magento_QuoteAnalytics':
Module 'Magento_QuoteBundleOptions':
Module 'Magento_QuoteConfigurableOptions':
Module 'Magento_QuoteDownloadableLinks':
Module 'Magento_InventoryConfigurableProductAdminUi':
Module 'Magento_ReCaptchaAdminUi':
Module 'Magento_ReCaptchaCheckout':
Module 'Magento_ReCaptchaCheckoutSalesRule':
Module 'Magento_ReCaptchaContact':
Module 'Magento_ReCaptchaCustomer':
Module 'Magento_ReCaptchaFrontendUi':
Module 'Magento_ReCaptchaMigration':
Module 'Magento_ReCaptchaNewsletter':
Module 'Magento_ReCaptchaPaypal':
Module 'Magento_ReCaptchaResendConfirmationEmail':
Module 'Magento_ReCaptchaReview':
Module 'Magento_ReCaptchaSendFriend':
Module 'Magento_ReCaptchaStorePickup':
Module 'Magento_ReCaptchaUi':
Module 'Magento_ReCaptchaUser':
Module 'Magento_ReCaptchaValidation':
Module 'Magento_ReCaptchaValidationApi':
Module 'Magento_ReCaptchaVersion2Checkbox':
Module 'Magento_ReCaptchaVersion2Invisible':
Module 'Magento_ReCaptchaVersion3Invisible':
Module 'Magento_ReCaptchaWebapiApi':
Module 'Magento_ReCaptchaWebapiGraphQl':
Module 'Magento_ReCaptchaWebapiRest':
Module 'Magento_ReCaptchaWebapiUi':
Module 'Magento_ReCaptchaWishlist':
Module 'Magento_RelatedProductGraphQl':
Module 'Magento_ReleaseNotification':
Module 'Magento_RemoteStorage':
Module 'Magento_InventoryLowQuantityNotificationAdminUi':
Module 'Magento_RequireJs':
Module 'Magento_Review':
Module 'Magento_ReviewAnalytics':
Module 'Magento_ReviewGraphQl':
Module 'Magento_AwsS3':
Module 'Magento_Rss':
Module 'Magento_PageBuilderAdminAnalytics':
Module 'Magento_ServicesIdGraphQlServer':
Module 'Magento_CatalogRuleConfigurable':
Module 'Magento_SalesAnalytics':
Module 'Magento_ServicesIdLayout':
Module 'Magento_SalesGraphQl':
Module 'Magento_SalesInventory':
Module 'Magento_OfflineShipping':
Module 'Magento_SalesRuleGraphQl':
Module 'Magento_ConfigurableProductSales':
Module 'Magento_UrlRewrite':
Module 'Magento_UrlRewriteGraphQl':
Module 'Magento_CustomerAnalytics':
Module 'Magento_Securitytxt':
Module 'Magento_SendFriend':
Module 'Magento_SendFriendGraphQl':
Module 'Magento_PaymentServicesBase':
Module 'Magento_SaaSCommon':
Module 'Magento_SalesDataExporter':
Module 'Magento_StoreDataExporter':
Module 'Magento_PaymentServicesPaypal':
Module 'Magento_InventoryInStorePickupSalesAdminUi':
Module 'Magento_AwsS3PageBuilder':
Module 'Magento_AsyncConfig':
Module 'Magento_PaymentServicesSaaSExport':
Module 'Magento_CompareListGraphQl':
Module 'Magento_Swagger':
Module 'Magento_SwaggerWebapi':
Module 'Magento_SwaggerWebapiAsync':
Module 'Magento_Swatches':
Module 'Magento_SwatchesGraphQl':
Module 'Magento_SwatchesLayeredNavigation':
Module 'Magento_Tax':
Module 'Magento_TaxGraphQl':
Module 'Magento_TaxImportExport':
Module 'Magento_InventoryGraphQl':
Module 'Magento_ThemeGraphQl':
Module 'Magento_Translation':
Module 'Magento_AdminAdobeImsTwoFactorAuth':
Module 'Magento_GoogleOptimizer':
Module 'Magento_Ups':
Module 'Magento_SampleData':
Module 'Magento_CatalogUrlRewriteGraphQl':
Module 'Magento_AsynchronousOperations':
Module 'Magento_Usps':
Module 'Magento_InventoryConfigurableProductFrontendUi':
Module 'Magento_PaypalCaptcha':
Module 'Magento_VaultGraphQl':
Module 'Magento_Version':
Module 'Magento_InventoryInStorePickupWebapiExtension':
Module 'Magento_WebapiAsync':
Module 'Magento_WebapiSecurity':
Module 'Magento_Weee':
Module 'Magento_WeeeGraphQl':
Module 'Magento_CurrencySymbol':
Module 'Magento_Wishlist':
Module 'Magento_WishlistAnalytics':
Module 'Magento_WishlistGraphQl':
Module 'PayPal_Braintree':
Module 'PayPal_BraintreeCustomerBalance':
Module 'PayPal_BraintreeGiftCard':
Module 'PayPal_BraintreeGiftCardAccount':
Module 'PayPal_BraintreeGiftWrapping':
Module 'PayPal_BraintreeGraphQl':
Module 'PayPal_BraintreeReward':
Nothing to import.
Media files stored outside of 'Media Gallery Allowed' folders will not be available to the media gallery.
Please refer to Developer Guide for more details.
rambo@ubuntu24-2:/var/www/html/magento$ sudo bin/magento setup:static-content:deploy -f
Deploy using quick strategy
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
frontend/Magento/blank/en_US 1908/1908 ============================ 100% 5 secs
adminhtml/Magento/backend/en_US 2543/2543 ============================ 100% 6 secs
frontend/Magento/luma/en_US 1924/1924 ============================ 100% 6 secs
Execution time: 19.301555871964
Apache 虚拟主机配置
rambo@ubuntu24-2:/var/www/html/magento$ sudo vim /etc/apache2/sites-available/magento.conf
<VirtualHost *:80>
ServerName <DOMAIN_NAME>
DocumentRoot /var/www/html/magento/
<Directory /var/www/html/magento/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/magento_error.log
CustomLog ${APACHE_LOG_DIR}/magento_access.log combined
</VirtualHost>
# 启用站点和重写模块并重启Apache
sudo a2ensite magento.conf
sudo a2enmod rewrite
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
后续操作
# 编译依赖和部署静态资源
rambo@ubuntu24-2:/var/www/html/magento$ sudo bin/magento setup:di:compile
Compilation was started.
Plugin list generation... 9/9 [============================] 100% 38 secs 420.0 MiB
Generated code and dependency injection configuration successfully.
rambo@ubuntu24-2:/var/www/html/magento$ sudo bin/magento setup:static-content:deploy -f
Deploy using quick strategy
frontend/Magento/blank/en_US 1908/1908 ============================ 100% < 1 sec
frontend/Magento/blank/en_US 1908/1908 ============================ 100% < 1 sec
frontend/Magento/blank/en_US 1908/1908 ============================ 100% < 1 sec
adminhtml/Magento/backend/en_US 2543/2543 ============================ 100% 1 sec
frontend/Magento/luma/en_US 1924/1924 ============================ 100% 1 sec
Execution time: 3.524493932724
# 设置权限再次确认
rambo@ubuntu24-2:/var/www/html/magento$ sudo chown -R www-data:www-data /var/www/html/magento
# 清缓存
rambo@ubuntu24-2:/var/www/html/magento$ sudo bin/magento cache:flush
Flushed cache types:
config
layout
block_html
collections
reflection
db_ddl
compiled_config
eav
customer_notification
config_integration
config_integration_api
graphql_query_resolver_result
full_page
config_webservice
translate

其他
# 查看为什么会依赖某个包
rambo@ubuntu24-2:/var/www/html/magento$ composer why laminas/laminas-config
laminas/laminas-modulemanager 2.17.0 requires laminas/laminas-config (^3.7)
# 查看建议的依赖包
rambo@ubuntu24-2:/var/www/html/magento$ composer suggest
carlos-mg89/oauth suggests:
- predis/predis: Allows using the Redis storage backend.
magento/magento2-functional-testing-framework suggests:
- hoa/console: Enables <pause /> action and interactive console functionality
105 additional suggestions by transitive dependencies can be shown with --all
# 查看可升级版本
rambo@ubuntu24-2:/var/www/html/magento$ composer outdated
Warning from repo.magento.com: You haven't provided your Magento authentication keys. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html
Authentication required (repo.magento.com):
Username: 932acd44eb3fdf6fb54e3b50b303f58f
Password:
Do you want to store credentials for repo.magento.com in /home/rambo/.config/composer/auth.json ? [Yn] Y
# 更新依赖
rambo@ubuntu24-2:/var/www/html/magento$ sudo composer update laminas/laminas-config --with-dependencies
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Package laminas/laminas-config is abandoned, you should avoid using it. No replacement was suggested.
Package laminas/laminas-json is abandoned, you should avoid using it. No replacement was suggested.
Package laminas/laminas-loader is abandoned, you should avoid using it. No replacement was suggested.
Package laminas/laminas-text is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
137 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
对于已废弃的组件

找出这些废弃包是被谁依赖的(由哪些包引入的)
rambo@ubuntu24-2:/var/www/html/magento$ composer why laminas/laminas-config
laminas/laminas-modulemanager 2.17.0 requires laminas/laminas-config (^3.7)
rambo@ubuntu24-2:/var/www/html/magento$ composer why laminas/laminas-json
laminas/laminas-view 2.39.0 requires laminas/laminas-json (^3.3)
rambo@ubuntu24-2:/var/www/html/magento$ composer why laminas/laminas-loader
laminas/laminas-http 2.22.0 requires laminas/laminas-loader (^2.10)
rambo@ubuntu24-2:/var/www/html/magento$ composer why laminas/laminas-text
laminas/laminas-captcha 2.18.0 requires laminas/laminas-text (^2.9.0)
处理建议(按模块)
1. laminas/laminas-modulemanager → 引入 laminas-config
Magento 并不直接使用 laminas-modulemanager,它是 Zend Framework 时代的模块加载器。说明这个依赖很可能来自某个第三方模块或开发工具包。
👉 建议:
运行 composer why laminas/laminas-modulemanager
如果是你自定义模块或工具引入,请考虑移除或替换
2. laminas/laminas-view → 引入 laminas-json
Magento 使用 Laminas View 进行模板渲染,这仍是 Magento 框架的一部分。
👉 建议:
无需立即移除,但建议关注 Magento 后续版本是否替换或移除对 laminas-json 的依赖
可通过插件方式覆盖 json_encode 相关逻辑(谨慎操作)
3. laminas/laminas-http → 引入 laminas-loader
此组件处理 HTTP 请求与响应,是 Magento HTTP 核心一部分。
👉 建议:
无法轻易替换,保持兼容性为主
可在开发中尽量不显式依赖 laminas-loader,使用 PSR-4 自动加载即可
4. laminas/laminas-captcha → 引入 laminas-text
Magento 默认验证码使用该组件。如果你不使用验证码,或使用 Google reCAPTCHA v2/v3 插件,可尝试移除:
👉 建议:
如果未启用 laminas-captcha 功能,可从 composer.json 中排除
若在使用,建议等待 Magento 官方替换或考虑替用 google reCAPTCHA 插件(如 msp/recaptcha)
Magento 2.4.8 环境中这些废弃依赖不会导致短期功能异常,但你已经处于维护尾声阶段。
建议关注 Magento **2.4.9 / 2.5.x(或 Adobe Commerce)**版本是否移除 Laminas Legacy 依赖。
如你是开发者,可尝试使用 composer replace 或 patch 移除不必要模块,但需测试充分。


浙公网安备 33010602011771号