在 CentOS/RHEL 8 上安装开源版 Magento

我们现在可以使用 composer 安装 Magento 开源版本,如下所示。

注意:在安装 Magento 电子商务软件期间,我们可能会被要求提供用户名/密码。
用户名是公钥,密码是私钥。
这些公钥/私钥可以在 Magento Marketplace 网站上生成。

我们需要注册/登录才能生成这些密钥。

# composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /var/www/magento

调整 Magento 文件的文件权限如下。

# cd /var/www/magento
# find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
# find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
# chown -R magento:apache /var/www/magento/
# chmod u+x bin/magento

调整 SELinux 文件上下文和布尔值。

# restorecon -R /var/www/magento
# setsebool -P httpd_unified 1

为 Magento 创建 Apache 虚拟主机配置。

# vi /etc/httpd/conf.d/magento.conf

并在此文件中添加以下指令。

<VirtualHost *:80>
    ServerName magento-2.onitroad.com
    DocumentRoot /var/www/magento
    ErrorLog /var/log/httpd/magento_error.log
    CustomLog /var/log/httpd/magento_access.log combined
    <Directory /var/www/magento >
        Options FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

重启 Apache 服务以加载新添加的配置。

# systemctl restart httpd.service

Magento 网络设置在 Magento 2.4.0(或者更高版本)中不再可用。
我们必须使用 Magento 命令行来安装它。

# php /var/www/magento/bin/magento setup:install \
> --base-url="http://magento-2.onitroad.com/magento/" \
> --db-host="localhost" \
> --db-name="magentodb" \
> --db-user="magento" \
> --db-password="Str0ngP@ssw0rd" \
> --search-engine="elasticsearch7" \
> --admin-firstname="admin" \
> --admin-lastname="admin" \
> --admin-email="admin@onitroad.com" \
> --admin-user="admin" \
> --admin-password="JackLi@1234" \
> --language="en_US" \
> --currency="USD" \
> --timezone="America/Chicago" \
> --use-rewrites="1" \
> --backend-frontname="admin"

为以下 Magento 目录调整 SELinux 文件上下文。

# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento/var(/.*)?'
# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento/generated(/.*)?'
# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento/vendor(/.*)?'
# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento/pub/static(/.*)?'
# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento/pub/media(/.*)?'
# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento/app/etc(/.*)?'
# restorecon -R /var/www/magento

创建 Linux 用户和目录

创建一个 Linux 用户来拥有 Magento 目录和文件。

# adduser magento
# usermod -a -G apache magento

创建用于部署 Magento 电子商务软件的目录。

# mkdir /var/www/magento
# chown magento:apache /var/www/magento

在 CentOS/RHEL 8 上安装 Composer

Composer 是 PHP 的依赖管理器。

目前,Composer v2.0 已在 Internet 上可用。
但是 Magento 2.4.1 不支持它。

因此,我们必须将 Composer v1.0 用于电子商务服务器。

使用 wget 命令下载 Composer。

# wget https://getcomposer.org/composer-1.phar

为 composer 文件添加执行权限,并将其复制到 /usr/local/bin 目录中,以便用户可以从任何位置运行它。

# chmod +x composer-1.phar
# mv composer-1.phar /usr/local/bin/composer
欢迎来到之路教程(on itroad-com)

为 Magento 创建 MySQL 数据库

Magento 使用 MySQL 作为其后端数据库。
我们已经在 Linux 服务器上安装了 MariaDB。
现在,以 root 用户身份登录 MySQL shell。

# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

为 Magento 电子商务软件创建 MySQL 数据库。

MariaDB [(none)]> CREATE DATABASE magentodb;
Query OK, 1 row affected (0.001 sec)

将 magentodb 数据库的所有权限授予 magento 用户。
此命令还会创建 magento 用户(如果尚未创建)。

MariaDB [(none)]> GRANT ALL ON magentodb.* TO magento@localhost IDENTIFIED BY 'Str0ngP@ssw0rd';
Query OK, 0 rows affected (0.001 sec)

退出 MySQL shell。

MariaDB [(none)]> exit
Bye

为 Magento 配置 Linux 防火墙

要允许传入流量到 HTTP 服务端口,我们可以在 Linux 防火墙中添加 http 服务或者端口 80/tcp。

# firewall-cmd --permanent --add-service=http
success
# firewall-cmd --reload
success

在 Linux 服务器上安装 ElasticSearch

从 Magento 2.4.0 开始,ElasticSearch 现在被用于搜索。
因此,我们还需要在 Linux 服务器上安装 ElasticSearch。

ElasticSearch 需要 Java Runtime,因此,在安装 ElasticSearch 之前,我们必须在 CentOS 8 机器上安装 OpenJDK。

# dnf install -y java-11-openjdk

验证已安装软件的 Java 版本。

# java -version
openjdk version "11.0.9" 2020-10-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.9+11-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.9+11-LTS, mixed mode, sharing)

使用 rpm 命令导入 ElasticSearch GPG 密钥。

# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

使用 vi 文本编辑器创建 yum 存储库文件。

# vi /etc/yum.repos.d/elasticsearch.repo

在此 repo 文件中添加以下指令。

[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md

我们现在可以使用 dnf 命令在 Linux 服务器上轻松安装 ElasticSearch。

# dnf install -y --enablerepo=elasticsearch elasticsearch

因为,我们安装在内存有限的虚拟机上。
因此,我们正在减少 Java 虚拟机 (JVM) 内存选项,如下所示。

在 vi 文本编辑器中打开 jvm.option 文件。

# vi /etc/elasticsearch/jvm.options

其中查找并调整以下参数。

-Xms256m
-Xmx512m

启用并启动 ElasticSearch 服务。

# systemctl enable --now elasticsearch.service
在 CentOS/RHEL 8 上安装 Magento 电子商务平台

Magento 是一个开源电子商务平台。
本教程将向我们展示如何在 CentOS/RHEL 8 上安装电子商务平台。

安装 Magento 软件准备工作

Magento 需要以下必备软件。

  • Nginx 或者 Apache 网络服务器
  • PHP(7.3 或者更高版本)
  • MySQL 数据库服务器
  • 弹性搜索

Apache、PHP 和 MySQL 在标准 yum 存储库中可用,因此,我们可以使用单个 dnf 命令安装所有这些。

# dnf install -y httpd mariadb-server @php:7.3 mod_ssl wget unzip

根据电子商务软件的要求配置 PHP 设置。

# vi /etc/php.ini

在此文件中找到并调整以下参数。

memory_limit = 1024M

启用并启动 Apache、MySQL 和 PHP 服务。

# systemctl enable --now mariadb.service httpd.service php-fpm.service

配置 MySQL 数据库服务器安全如下。

# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

什么是 Magento?

Magento 是一个用 PHP 编写的开源电子商务平台,使用多个框架,例如 Laminas 和 Symfony。

Magento 软件有企业版和社区版。

  • 企业版被命名为 Magento Commerce 并收取许可费,但提供许多企业级功能。查看完整的 Magento 定价。
  • 而社区版被命名为 Magento Open Source ,它是免费的,并在开放软件许可证 (OSL) v3.0 下分发。它的源代码可在 Magento Github 上找到。
日期:2020-09-17 00:16:40 来源:oir作者:oir