[内部部署]{class="badge informative" title="仅适用于Adobe Commerce本地项目。"}

为内部部署安装Nginx nginx

本指南将指导您完成为Adobe Commerce内部部署安装Nginx以及配置Commerce所需的Nginx设置。 其中包括针对Ubuntu和CentOS的特定于操作系统的过程,以及配置PHP-FPM的指南。 Adobe建议遵循本指南中提供的配置说明,以保留Commerce应用程序的功能和安全性。

Adobe支持您的Adobe Commerce版本的系统要求中列出的Nginx版本。 支持的版本因版本而异。 Nginx还需要支持的PHP-FPM配置。 有关相关的PHP要求,请参阅PHP

在Ubuntu上安装

使用本节在Ubuntu上通过Nginx、PHP和MySQL安装Adobe Commerce。

安装Nginx

sudo apt -y install nginx

您也可以从源构建Nginx。

完成以下部分并安装应用程序后,请使用示例配置文件来配置Nginx。 此推荐的配置保留了Commerce应用程序的功能和安全性。

安装和配置PHP-FPM

Adobe Commerce需要多个PHP扩展才能正常运行。 除了这些扩展之外,如果您使用Nginx,则还必须安装和配置php-fpm扩展。

要安装和配置php-fpm,请执行以下操作:

  1. 安装Adobe Commerce版本支持的PHP版本的php-fpmphp-cli包。 在Ubuntu上,包名称通常遵循以下模式:

    code language-bash
    apt-get -y install php<php-version>-fpm php<php-version>-cli
    
    note note
    NOTE
    <php-version>替换为您正在安装的Adobe Commerce版本的系统要求中列出的受支持的PHP次要版本。 在以下步骤中,在文件路径、服务名称和套接字路径中使用相同的值。
  2. 在编辑器中打开php.ini文件:

    code language-bash
    vim /etc/php/<php-version>/fpm/php.ini
    
    code language-bash
    vim /etc/php/<php-version>/cli/php.ini
    
  3. 编辑这两个文件以匹配以下行:

    code language-conf
    memory_limit = 2G
    max_execution_time = 1800
    zlib.output_compression = On
    
    note note
    NOTE
    Adobe建议在测试Adobe Commerce时将内存限制设置为2 GB。 有关详细信息,请参阅必需的PHP设置
  4. 保存并退出编辑器。

  5. 重新启动php-fpm服务:

    code language-bash
    systemctl restart php<php-version>-fpm
    

安装和配置MySQL

有关详细信息,请参阅MySQL

安装Adobe Commerce

您可以通过多种方式下载Adobe Commerce:

此示例显示了使用命令行进行的基于编辑器的安装。

  1. 文件系统所有者的身份登录到您的应用程序服务器。

  2. 转到Web服务器docroot目录或您已配置为虚拟主机docroot的目录。 在此示例中,我们使用Ubuntu默认/var/www/html

    code language-bash
    cd /var/www/html
    
  3. 全局安装编辑器。 在安装Adobe Commerce之前,需要编辑器更新依赖项:

    code language-bash
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
    
  4. 使用Adobe Commerce中继资料创建编辑器项目。

    Magento Open Source

    code language-bash
    composer create-project --repository=https://repo.magento.com/ magento/project-community-edition <install-directory-name>
    

    Adobe Commerce

    code language-bash
    composer create-project --repository=https://repo.magento.com/ magento/project-enterprise-edition <install-directory-name>
    

    出现提示时,输入您的身份验证密钥。 您的​ 公钥 ​是您的用户名;私钥​是您的密码。

  5. 在安装应用程序之前,为Web服务器组设置读写权限。 这是必要的,以便命令行可以将文件写入文件系统。

    code language-bash
    cd /var/www/html/<magento install directory>
    
    code language-bash
    find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
    
    code language-bash
    find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
    
    code language-bash
    chown -R :www-data . # Ubuntu
    
    code language-bash
    chmod u+x bin/magento
    
  6. 命令行安装。 此示例假定安装目录名为magento2ee,并且数据库主机位于同一台计算机(localhost)上:

    code language-bash
    bin/magento setup:install \
    --base-url=http://localhost/magento2ee \
    --db-host=localhost \
    --db-name=<db-name> \
    --db-user=<db-user> \
    --db-password=<db-password> \
    --backend-frontname=<backend-uri> \
    --admin-firstname=<admin-first-name> \
    --admin-lastname=<admin-last-name> \
    --admin-email=<admin-email> \
    --admin-user=<admin-user> \
    --admin-password=<admin-password> \
    --language=en_US \
    --currency=USD \
    --timezone=America/Chicago \
    --use-rewrites=1 \
    --search-engine=<search-engine-value> \
    --<search-engine-host-parameter>=search-host.example.com \
    --<search-engine-port-parameter>=9200
    
    note note
    NOTE
    使用--search-engine值和要安装的Adobe Commerce版本所需匹配的主机/端口选项。 对于低于2.4.6的版本,请将elasticsearch7与Elasticsearch 7或OpenSearch的--elasticsearch-*选项一起使用。 对于版本2.4.6及更高版本,请使用该版本支持的搜索引擎值和相应的CLI选项。
  7. 切换到开发人员模式:

    code language-bash
    cd /var/www/html/magento2/bin
    
    code language-bash
    ./magento deploy:mode:set developer
    

配置Nginx

Adobe建议使用安装目录中提供的nginx.conf.sample配置文件和Nginx虚拟主机配置来配置Nginx,以保留Commerce应用程序的功能和安全性。

IMPORTANT
nginx.conf.sample文件提供了所需的应用程序路由以及安全增强规则。 例如,它限制上传到服务器的有害脚本的执行。 如果您不使用此文件或修改其规则,则您负责在自定义nginx配置中实施等效的安全控件。

这些说明假定您正在使用Nginx虚拟主机的Ubuntu默认位置(如/etc/nginx/sites-available)和Ubuntu默认docroot(如/var/www/html)。 您可以更改这些位置以适合您的环境。

  1. 为您的站点创建新的虚拟主机:

    code language-bash
    vim /etc/nginx/sites-available/magento
    
  2. 添加以下配置:

    code language-conf
    upstream fastcgi_backend {
      server  unix:/run/php/php<php-version>-fpm.sock;
    }
    
    server {
    
      listen 80;
      server_name www.magento-dev.com;
      set $MAGE_ROOT /var/www/html/magento2;
      include /var/www/html/magento2/nginx.conf.sample;
    }
    
    note note
    NOTE
    include指令必须指向安装目录中的nginx配置文件示例。
  3. www.magento-dev.com替换为您的域名。 此名称必须匹配您在安装Adobe Commerce时指定的基本URL。

  4. 保存并退出编辑器。

  5. 通过在/etc/nginx/sites-enabled目录中创建指向新创建的虚拟主机的符号链接来激活该虚拟主机:

    code language-bash
    ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled
    
  6. 验证语法是否正确:

    code language-bash
    nginx -t
    
  7. 重新启动Nginx:

    code language-bash
    systemctl restart nginx
    

验证安装

要验证安装,请打开Web浏览器并导航到站点的基本URL。 有关详细信息,请参阅验证安装

在CentOS 7上安装

使用本节在CentOS 7(带有Nginx、PHP和MySQL)上安装Adobe Commerce。

安装Nginx

yum -y install epel-release
yum -y install nginx

安装完成后,启动nginx并将其配置为在引导时启动:

systemctl start nginx
systemctl enable nginx

完成以下部分并安装应用程序后,请使用示例配置文件来配置Nginx。

安装和配置PHP-FPM

Adobe Commerce需要多个PHP扩展才能正常运行。 除了这些扩展之外,如果您使用Nginx,则还必须安装和配置php-fpm扩展。

  1. 安装php-fpm

    code language-bash
    yum -y install <php-fpm-package>
    
  2. 在编辑器中打开/etc/php.ini文件。

    note note
    NOTE
    安装为要安装的Adobe Commerce版本支持的PHP版本提供php-fpm的包。 软件包名称因存储库和操作系统而异。 请参阅系统要求
  3. 取消注释cgi.fix_pathinfo行并将值更改为0

  4. 编辑文件以匹配以下行:

    code language-conf
    memory_limit = 2G
    max_execution_time = 1800
    zlib.output_compression = On
    
    note note
    NOTE
    Adobe建议在测试Adobe Commerce时将内存限制设置为2 GB。 有关详细信息,请参阅必需的PHP设置
  5. 取消注释会话路径目录并设置路径:

    code language-conf
    session.save_path = "/var/lib/php/session"
    
  6. 保存并退出编辑器。

  7. 在编辑器中打开/etc/php-fpm.d/www.conf

  8. 编辑文件以匹配以下行:

    code language-conf
    user = nginx
    group = nginx
    listen = /run/php-fpm/php-fpm.sock
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660
    
  9. 取消对环境行的注释:

    code language-conf
    env[HOSTNAME] = $HOSTNAME
    env[PATH] = /usr/local/bin:/usr/bin:/bin
    env[TMP] = /tmp
    env[TMPDIR] = /tmp
    env[TEMP] = /tmp
    
  10. 保存并退出编辑器。

  11. 为PHP会话路径创建一个目录,并将所有者更改为nginx用户和组:

    code language-bash
    mkdir -p /var/lib/php/session/
    
    code language-bash
    chown -R nginx:nginx /var/lib/php/
    
  12. 为PHP-FPM套接字创建一个目录,并将所有者更改为nginx用户和组:

    code language-bash
    mkdir -p /run/php-fpm/
    
    code language-bash
    chown -R nginx:nginx /run/php-fpm/
    
  13. 启动php-fpm服务并将其配置为在启动时启动:

    code language-bash
    systemctl start php-fpm
    
    code language-bash
    systemctl enable php-fpm
    
  14. 验证php-fpm服务是否正在运行:

    code language-bash
    netstat -pl | grep php-fpm.sock
    

安装和配置MySQL

有关详细信息,请参阅MySQL

安装Adobe Commerce

您可以通过多种方式下载Adobe Commerce:

此示例显示了使用命令行进行的基于编辑器的安装。

  1. 文件系统所有者的身份登录到您的应用程序服务器。

  2. 转到Web服务器docroot目录或您已配置为虚拟主机docroot的目录。 对于此示例,使用CentOS默认值/usr/share/nginx/html

    code language-bash
    cd /usr/share/nginx/html
    
  3. 全局安装编辑器。 在安装Adobe Commerce之前,需要编辑器更新依赖项:

    code language-bash
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
    
  4. 使用Adobe Commerce中继资料创建编辑器项目。

    Magento Open Source

    code language-bash
    composer create-project --repository=https://repo.magento.com/ magento/project-community-edition <install-directory-name>
    

    Adobe Commerce

    code language-bash
    composer create-project --repository=https://repo.magento.com/ magento/project-enterprise-edition <install-directory-name>
    

    出现提示时,输入您的身份验证密钥。 您的​ 公钥 ​是您的用户名;私钥​是您的密码。

  5. 在安装应用程序之前,为Web服务器组设置读写权限。 这是必要的,以便命令行可以将文件写入文件系统。

    code language-bash
    cd /usr/share/nginx/html/<magento install directory>
    
    code language-bash
    find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
    
    code language-bash
    find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
    
    code language-bash
    chown -R :nginx . # CentOS
    
    code language-bash
    chmod u+x bin/magento
    
  6. 命令行安装。 此示例假定安装目录名为magento2ee,并且数据库主机位于同一台计算机(localhost)上:

    code language-bash
    bin/magento setup:install \
    --base-url=http://localhost/magento2ee \
    --db-host=localhost \
    --db-name=<db-name> \
    --db-user=<db-user> \
    --db-password=<db-password> \
    --backend-frontname=<backend-uri> \
    --admin-firstname=<admin-first-name> \
    --admin-lastname=<admin-last-name> \
    --admin-email=<admin-email> \
    --admin-user=<admin-user> \
    --admin-password=<admin-password> \
    --language=en_US \
    --currency=USD \
    --timezone=America/Chicago \
    --use-rewrites=1
    
  7. 切换到开发人员模式:

    code language-bash
    cd /usr/share/nginx/html/magento2/bin
    
    code language-bash
    ./magento deploy:mode:set developer
    

配置Nginx

我们建议使用安装目录中的nginx.conf.sample文件和Nginx虚拟主机配置来配置Nginx。

IMPORTANT
nginx.conf.sample文件提供了所需的应用程序路由以及安全增强规则。 例如,它限制上传到服务器的有害脚本的执行。 如果您不使用此文件或修改其规则,则您负责在自定义nginx配置中实施等效的安全控件。

这些说明假定您正在使用Nginx虚拟主机的CentOS默认位置(如/etc/nginx/conf.d)和默认docroot(如/usr/share/nginx/html)。 您可以更改这些位置以适合您的环境。

  1. 为您的站点创建新的虚拟主机:

    code language-bash
    vim /etc/nginx/conf.d/magento.conf
    
  2. 添加以下配置:

    code language-conf
    upstream fastcgi_backend {
      server  unix:/run/php-fpm/php-fpm.sock;
    }
    
    server {
    
      listen 80;
      server_name www.magento-dev.com;
      set $MAGE_ROOT /usr/share/nginx/html/magento2;
      include /usr/share/nginx/html/magento2/nginx.conf.sample;
    }
    
    note note
    NOTE
    include指令必须指向安装目录中的nginx配置文件示例。
  3. www.magento-dev.com替换为您的域名。

  4. 保存并退出编辑器。

  5. 验证语法是否正确:

    code language-bash
    nginx -t
    
  6. 重新启动Nginx:

    code language-bash
    systemctl restart nginx
    

配置SELinux和firewalld

在CentOS 7上,SELinux默认处于启用状态。 使用以下命令确认其正在运行:

sestatus

要配置SELinux和firewald:

  1. 安装SELinux管理工具:

    code language-bash
    yum -y install policycoreutils-python
    
  2. 运行以下命令以更改安装目录的安全上下文:

    code language-bash
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/app/etc(/.*)?'
    
    code language-bash
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/var(/.*)?'
    
    code language-bash
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/pub/media(/.*)?'
    
    code language-bash
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/magento2/pub/static(/.*)?'
    
    code language-bash
    restorecon -Rv '/usr/share/nginx/html/magento2/'
    
  3. 安装防火墙包:

    code language-bash
    yum -y install firewalld
    
  4. 启动防火墙服务并将其配置为在引导时启动:

    code language-bash
    systemctl start firewalld
    
    code language-bash
    systemctl enable firewalld
    
  5. 运行以下命令以打开HTTP和HTTPS端口,以便您可以从Web浏览器访问基本URL:

    code language-bash
    firewall-cmd --permanent --add-service=http
    
    code language-bash
    firewall-cmd --permanent --add-service=https
    
    code language-bash
    firewall-cmd --reload
    

验证安装

要验证安装,请打开Web浏览器并导航到站点的基本URL。 有关详细信息,请参阅验证安装

recommendation-more-help
0f8e7db5-0e9c-4002-a5b8-a0088077d995