[comment]: # translation:outdated

[comment]: # ({eb5f704e-af2eaf2f})

# 4 从源代码安装

你可以通过从源代码编译获得最新的Zabbix版本。

这里教程提供了从源代码安装Zabbix的详细步骤。

[comment]: # ({/eb5f704e-af2eaf2f})

[comment]: # ({0040d992-0040d992})

#### - 安装Zabbix守护进程

[comment]: # ({/0040d992-0040d992})

[comment]: # ({d597695a-8cc2c1b1})
##### 1 下载源代码压缩包

前往 [Zabbix 下载页面](http://www.zabbix.com/download_sources) 
并下载源代码压缩包。下载完成后，通过运行以下命令解压源代码：

    tar -zxvf zabbix-7.0.0.tar.gz

::: notetip
在命令中输入正确的 Zabbix 版本号。它必须与下载的压缩包的名称相匹配。
:::

[comment]: # ({/d597695a-8cc2c1b1})

[comment]: # ({87e1101d-87e1101d})

##### 2 创建用户账户

所有的Zabbix守护进程都必须要一个非特权用户。如果一个非特权用户启动了一个Zabbix守护进程，它就会以这个用户运行。

然而，如果一个守护进程以‘root’账户运行，它会切换到一个‘zabbix‘用户，这个用户是必须存在的。要创建这样一个用户（在它自己的“zabbix”组里），

在基于RedHat的系统里，运行：

    groupadd --system zabbix
    useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix

在基于Debian的系统里，运行：

    addgroup --system --quiet zabbix
    adduser --quiet --system --disabled-login --ingroup zabbix --home /var/lib/zabbix --no-create-home zabbix

::: noteimportant
Zabbix进程不许要home目录，因此我们不推荐创建它。然而，如果你将要使用的某些功能需要它（比如在`$HOME/.my.cnf`里存放MySQL凭证) ，你可以使用如下命令去创建home目录。\
\

在基于RedHat的系统里，运行：

    mkdir -m u=rwx,g=rwx,o= -p /usr/lib/zabbix
    chown zabbix:zabbix /usr/lib/zabbix

在基于Debian的系统里，运行：

    mkdir -m u=rwx,g=rwx,o= -p /var/lib/zabbix
    chown zabbix:zabbix /var/lib/zabbix

:::

安装Zabbix前端不许要单独的用户。

如果Zabbix [server](/manual/concepts/server)和[agent](/manual/concepts/agent)运行在同一台机器上，建议使用与agent不同的用户来运行。否则，如果两者使用相同的用户，agent可以访问server的配置文件，Zabbix里任何Admin级别的用户可以轻易地获取诸如数据可密码等信息。

::: noteimportant
以`root`、`bin`或者其他任何有特殊权限的账户运行Zabbix都有安全风险。
:::

[comment]: # ({/87e1101d-87e1101d})

[comment]: # ({06256db0-06256db0})

##### 3 创建Zabbix数据库

对Zabbix[server](/manual/concepts/server)和[proxy](/manual/concepts/proxy)守护进程，还有Zabbix前端，必须要有一个数据库。但运行Zabbix[agent](/manual/concepts/agent)不需要。

此处SQL[脚本](/manual/appendix/install/db_scripts)用于创建数据库模式和插入数据集。Zabbix proxy数据库只需要数据库模式，而Zabbix server数据库在数据库模式之上还需要数据集。

创建了Zabbix数据库之后，执行以下步骤来编译Zabbix。

[comment]: # ({/06256db0-06256db0})

[comment]: # ({699de06a-8ff1b41c})

##### 4 配置源代码

当为Zabbix server或proxy配置源码时，必须指定要使用的数据库类型。同一时间，只用一种数据库类型可以与server或proxy编译。

要查看所有支持的配置选项，在提取的Zabbix源代码目录运行：

    ./configure --help

要为Zabbix server和agent配置源代码，你可以执行类似如下命令：

    ./configure --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 --with-openipmi

要为Zabbix server（和，比如PostgreSQL）配置源代码，你可以执行：

    ./configure --enable-server --with-postgresql --with-net-snmp

要为Zabbix server（和，比如SQLite）配置源代码，你可以执行：

    ./configure --prefix=/usr --enable-proxy --with-net-snmp --with-sqlite3 --with-ssh2

要为Zabbix agent配置源代码，你可以执行：

    ./configure --enable-agent

或者Zabbix agent 2：

    ./configure --enable-agent2

::: noteclassic
构建Zabbix agent 2需要一个用当前支持的[Go版本](https://golang.org/doc/devel/release#policy)配置的Go环境。安装指导详见[golang.org](https://golang.org/doc/install)。
:::

编译选项注意事项：

-    如果使用了--enable-agent选项命令行实用程序zabbix\_get和zabbix\_sender会被编译。
-   --with-libcurl 和 --with-libxml2 配置选项对虚拟机监控是必须的； --with-libcurl 对 SMTP 身份验证和`web.page.*`Zabbix agent[监控项](/manual/config/items/itemtypes/zabbix_agent).也是必须的。请注意，cURL 7.20.0或更高版本[要求](/manual/installation/requirements)有 --with-libcurl 配置选项。
-   Zabbix （从 3.4.0 版本开始）始终使用 PCRE 库进行编译；安装它不是可选的。 --with-libpcre=\[DIR\] 只允许指向特定的基本安装目录，而不是搜索 libpcre 文件的一些常见位置。
-   你可以使用 --enable-static 标志来静态链接库。如果你计划在不同的服务器之间分发编译的二进制文件， 你必须使用这个标志来使这些二进制文件在没有必须的库的情况下工作。请注意，--enable-static 在[Solaris](https://docs.oracle.com/cd/E18659_01/html/821-1383/bkajp.html)不同做
-   构建服务器时不建议使用 --enable-static 选项。为了静态构建服务器，你必须每个需要的外部库的静态版本。配置脚本中不会严格地检查这些。
-   将可选路径添加到 MySQL 配置文件 --with-mysql=/<path\_to\_the\_file>/mysql\_config， 以便在需要使用不在默认位置的 MySQL 客户端库时选择所需的 MySQL 客户端。当在同一系统上安装了多个版本的 MySQL 或 MariaDB 与 MySQL 一起安装时，这个配置很有用。
-   使用 --with-oracle 标志指定 OCI API 的位置。

::: noteimportant
 如果./configure由于缺少库或者其他条件而失败，请查看`config.log`文件获取错误的更多详细信息。例如，如果`libssl`缺失，即时错误信息可能具有误导性：
 
    checking for main in -lmysqlclient... no
    configure: error: Not found mysqlclient library

而`config.log`则有更多详细的描述：

    /usr/bin/ld: cannot find -lssl
    /usr/bin/ld: cannot find -lcrypto


:::

另见：

-   [用加密支持编译Zabbix](/manual/encryption#compiling_zabbix_with_encryption_support)
-   已知问题[在HP-UX上编译Zabbix](/manual/installation/known_issues#compiling_zabbix_agent_on_hp-ux)

[comment]: # ({/699de06a-8ff1b41c})

[comment]: # ({d6de1968-08edd3dc})

##### 5 Make 和 install 所有

::: noteclassic
如果从[Zabbix Git
repository](https://git.zabbix.com/projects/ZBX/repos/zabbix/browse)安装，必须先执行：`$ make dbschema` 
:::

    make install

这一步应该以具有足够权限的用户身份运行（通常是 'root'，或使用 `sudo`）。

运行 `make install` 将默认在 /usr/local/sbin 目录安装守护进程二进制文件 (zabbix\_server, zabbix\_agentd, zabbix\_proxy) ，在 /usr/local/bin ，目录安装客户端二进制文件（zabbix\_get、zabbix\_sender）。

::: noteclassic
要指定与 /usr/local 不同的位置，在之前配置源的步骤中使用 --prefix 键，例如 --prefix=/home/zabbix。在这种情况下，守护程序二进制文件将安装在 <prefix>/sbin 下，而实用程序则安装在 <prefix>/bin 下，手册页将安装在 <prefix>/share 下。
:::

[comment]: # ({/d6de1968-08edd3dc})

[comment]: # ({08729b12-112b74f7})

##### 6 查看和编辑配置文件

-   编辑 Zabbix agent 配置文件 **/usr/local/etc/zabbix\_agentd.conf**

你需要为每个安装了 zabbix_agentd 的主机配置此文件。

你必须在文件中指定 Zabbix 服务器**IP 地址**，来自其他主机的连接将被拒绝。

-   编辑 Zabbix 服务器配置文件 **/usr/local/etc/zabbix\_server.conf**

你必须指定数据库名称、用户和密码（如果使用了）。

如果你是小型安装（最多十台受监控的主机），其他参数的默认值将适合你。 如果你想最大化 Zabbix server（或proxy）的性能，你应该修改默认参数。更多信息详见[性能调优](/manual/appendix/performance_tuning) 章节。

-   如果你安装了 Zabbix proxy，编辑 proxy 配置文件 **/usr/local/etc/zabbix\_proxy.conf**

你必须指定服务器 IP 地址和 proxy 的主机名（服务器必须知道），以及数据库名称、用户和密码（如果使用了）。

::: noteclassic
使用 SQLite 必须指定数据库文件的完整路径；不需要数据库用户和密码。
:::

[comment]: # ({/08729b12-112b74f7})

[comment]: # ({624acaba-bbdd82ac})

##### 7 启动守护进程

在服务器端运行 zabbix\_server。

    shell> zabbix_server

::: noteclassic
确保你的系统允许分配 36MB （或稍微多一点）的共享内存，否则 server 可能无法启动，你会在 server 的日志文件里看到“不能为<type of cache> 分配共享内存”。这可能在 FreeBSD 和 Solaris 8上发生。\
请参阅本页底部的[“另请参阅”](#see_also) 部分，了解如何配置共享内存。
:::

在所有被监控的机器上运行 zabbix\_agentd。

    shell> zabbix_agentd

::: noteclassic
确保你的系统允许分配 2MB 的共享内存，否则 agent 可能无法启动，你会在 agent 的日志文件里看到 “无法为收集器分配共享内存。” 这可能在 Solaris 8 上发生。
:::

如果你安装了 Zabbix proxy，运行zabbix\_proxy。

    shell> zabbix_proxy

[comment]: # ({/624acaba-bbdd82ac})

[comment]: # ({c9f154ca-c9f154ca})

#### - 安装 Zabbix 网页界面

[comment]: # ({/c9f154ca-c9f154ca})

[comment]: # ({fdaa1f6a-ed4e56ce})
##### 复制PHP文件

Zabbix前端是PHP编写的，所以运行它需要PHP支持的网络服务器。安装只需简单的从 ui 目录复制PHP文件到网络服务器 HTML文档目录。

Apache网络服务器的HTML文档目录的常见位置包括：

-   /usr/local/apache2/htdocs (从源代码安装Apache的默认目录)
-   /srv/www/htdocs (OpenSUSE, SLES)
-   /var/www/html (Debian, Ubuntu, Fedora, RHEL, CentOS)

建议使用子目录而非HTML根目录。要创建子目录并将Zabbix前端文件复制过去，请执行如下命令，以替换实际目录：

    mkdir <htdocs>/zabbix
    cd ui
    cp -a . <htdocs>/zabbix

如果计划用英语之外的语言，请参考[前端安装其他语言](/manual/appendix/install/locales) 。

[comment]: # ({/fdaa1f6a-ed4e56ce})

[comment]: # ({63583cbb-63583cbb})

##### 安装前端

关于 Zabbix 前端的安装，请参考[网页界面安装](/manual/installation/frontend)页面的信息。

[comment]: # ({/63583cbb-63583cbb})

[comment]: # ({new-c8da42bb})
#### - Installing Zabbix agent 2 loadable plugins

Installing Zabbix agent 2 loadable plugins is only required if you want to monitor targets not covered by built-in plugins (e.g., MongoDB servers or clusters, PostgreSQL and its forks, etc.).
See the full list of [loadable plugins](/manual/extensions/plugins#loadable-plugins) and [built-in plugins](/manual/extensions/plugins#built-in-plugins).

::: noteimportant
Before installing a plugin, please check its README file.
It may contain specific requirements and installation instructions.
:::

To install from sources, first [download](https://cdn.zabbix.com/zabbix-agent2-plugins/sources/) and extract the loadable plugin source archive.

To compile the plugin, navigate to the extracted plugin directory and run `make`:

    make

::: noteclassic
A [supported Go version](/manual/installation/requirements#agent-2) is required for building Zabbix agent 2.
:::

The plugin executable may be placed anywhere as long as it is loadable by Zabbix agent 2.
Specify the path to the plugin binary in the plugin configuration file, e.g. in postgresql.conf for the [PostgreSQL](/manual/appendix/config/zabbix_agent2_plugins/postgresql_plugin) plugin:

    Plugins.PostgreSQL.System.Path=/path/to/executable/zabbix-agent2-plugin-postgresql

The path to the plugin configuration file must be specified in the Include parameter of the Zabbix agent 2 configuration file:

    Include=/path/to/plugin/configuration/file/postgresql.conf

Proceed to [setup](/manual/extensions/plugins#configuring-plugins) for more details on configuring plugins.

Loadable plugins provided by Zabbix use simple makefiles with the following build targets:

-   `make` - build the plugin
-   `make clean` - delete all files that are created by building the plugin
-   `make check` - run self-tests (requires a real monitoring target, e.g., a PostgreSQL database)
-   `make style` - check Go code style with `golangci-lint`
-   `make format` - format Go code with `go fmt`
-   `make dist` - create a source archive including all dependencies

[comment]: # ({/new-c8da42bb})

[comment]: # ({295e6be4-1a519c06})

#### 3 安装 Java gateway

只有在你想监控 JMX 应用程序时，才需要安装Java gateway。Java gateway是轻量级的，不需要数据库。

从源代码安装，先[下载](/manual/installation/install#download_the_source_archive) 并解压源代码压缩包。

要编译Java gateway，请带 `--enable-java` 选项执行  `./configure`。建议指定 `--prefix` 选项来请求默认的 /usr/local 以外的安装路径，因为安装Java gateway 将创建一个完整的目录树，而不仅仅是一个可执行文件。

    $ ./configure --enable-java --prefix=$PREFIX

要将 Java gateway 编译并打包到一个JAR文件中，执行 `make`。 请注意，这一步你的路径中可能需 `javac` 和 `jar`  可执行文件。

    $ make

现在你在 src/zabbix\_java/bin 目录有一个 zabbix-java-gateway-$VERSION.jar 文件。如果你对从分配的目录中的 src/zabbix\_java 运行 Java gateway 感到满意，那么你可以继续按指导配置和运行 [Java gateway](/manual/concepts/java#overview_of_files_in_java_gateway_distribution)。否则，请确保您有足够的权限并执行 `make install`。

    $ make install

继续[设置](/manual/concepts/java/from_sources)以获取更多关于配置和运行 Java gateway 的详细信息 。

[comment]: # ({/295e6be4-1a519c06})

[comment]: # ({c3cc8cc3-76c01064})

#### 4 安装Zabbix web服务

只有当你想使用[定时报表](/manual/web_interface/frontend_sections/reports/scheduled)时，才需要安装Zabbix web服务。

要从源代码安装，请先[下载](/manual/installation/install#download_the_source_archive) 和解压源代码压缩包。

要编译Zabbix web服务， 请带 `--enable-webservice` 选项执行 `./configure`。

::: noteclassic
 构建 Zabbix web 服务需要配置好的 [Go](https://golang.org/doc/install) 1.13 + 版本的环境。

:::

在安装了web服务的机器上运行 zabbix\_web\_service：

    shell> zabbix_web_service

可在[web服务](/manual/appendix/install/web_service) 了解关于配置定时报表生成的更多信息。

[comment]: # ({/c3cc8cc3-76c01064})

