ubuntu基于apache+postgresql编译安装zabbix

一、安装环境

  ubuntu 14.04LTS Postgresql-9.4.3 zabbix-2.4.5 httpd-2.4.12 php-5.6.10

二、选择原因

  选择postgresql数据库的原因,除了是因为 zabbix 的数据业务相对复杂,比使用 mysql 更加优势,而且mysql的数据表容易坏,时常需要修复。现在虽然nginx比较流行,但对于高稳定,且不需要高并发的应用,推荐还是用apache,且使用 prefork 模式。

三、安装准备

  创建zbx系统用户用于管理与zabbix运行相关的软件

$ useradd -M -r -d /zbx zbx
$ apt-get install build-essential # 安装编译环境

  接下来为系统添加语系,只有这样zabbix的多语言选项才会生效:

$ vim /var/lib/locales/supported.d/zabbix #建立一个新文件,内容如下:
en_US.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8
zh_TW.UTF-8 UTF-8
bg_BG.UTF-8 UTF-8
fr_FR.UTF-8 UTF-8
de_DE.UTF-8 UTF-8
id_ID.UTF-8 UTF-8
it_IT.UTF-8 UTF-8
ja_JP.UTF-8 UTF-8
pt_BR.UTF-8 UTF-8
pt_PT.UTF-8 UTF-8
ru_RU.UTF-8 UTF-8
sk_SK.UTF-8 UTF-8
uk_UA.UTF-8 UTF-8
$ locale-gen #立即更新系统的语系

四、安装Postgresql

  下载并安装

$ apt-get install libreadline-dev zlib1g-dev libssl-dev libxml2-dev libxslt-dev python-dev # 安装依赖
$ wget http://ftp.postgresql.org/pub/source/v9.4.3/postgresql-9.4.3.tar.bz2
$ tar –jxvf postgresql -C /usr/local/src   
$ cd postgresql-9.4.3
$ ./configure
--prefix=/usr/local/postgresql 
--with-segsize=8 
--with-wal-segsize=64 
--with-wal-blocksize=64 
--with-python 
--with-openssl 
--with-libxml 
--with-libxslt 
--enable-thread-safety
$ make
$ make install

  设置环境变量

$ vim /etc/profile.d/pgsql.sh
export PGHOME=/usr/local/postgresql
export PGDATA=$PGHOME/data
export PATH=$PATH:$PGHOME/bin
export LD_LIBRARY_PATH=$PGHOME/lib/ 
alias pg_stop='su - fmc -c "pg_ctl -D $PGDATA stop"'
alias pg_start='su - fmc -c "pg_ctl -D $PGDATA -l $PGHOME/pgsql.log start"'
$ source /etc/profile.d/pgsql.sh

  初始化数据库

$ su - zbx
No directory, logging in with HOME=/
$ initdb -D /usr/local/postgresql/data -E UTF8 --locale=C -U zbx -W

  将postgresql的动态链接库为系统共享,编译zabbix时需要:

$ vim /etc/ld.so.conf.d/pgsql.conf
/zabbix/postgresql/lib
$ ldconfig # 让其立即生效

  启动 postgresql,并为 zabbix 建立数据库及用户:

$ pg_start
$ psql -U zbx --dbname=postgres
create database zabbix;
create user zbx;
grant all on database zabbix to zbx;
\q

五、安装apache

  安装依赖

$ apt-get install libpcre3-dev
$ wgethttp://mirror.bit.edu.cn/apache/apr/apr-1.5.2.tar.gz
$ tar -zxvf apr/apr-1.5.2.tar.gz
$ cd apr-1.5.2/
$ ./configure && make
$ make install
$ wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.5.4.tar.gz
$ tar -zxvf apr-util-1.5.4.tar.gz
$ cd apr-util-1.5.4/
$ ./configure --with-apr=/usr/local/apr && make
$ make install

  编译安装 apache:

$ wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.12.tar.gz
$ tar -zxvf /httpd-2.4.12.tar.gz
$ cd httpd*
$ ./configure --prefix=/usr/local/httpd --enable-so --enable-mods-shared=most --with-mpm=prefork 
$ make  && make install

  配置vim /usr/local/httpd/conf/httpd.conf,请根据默认配置添加替换或注释以下关键字段:

#ServerAdmin you@example.com
ServerName 192.168.45.127:80
User zbx
Group zbx
Include conf/extra/httpd-mpm.conf
<IfModule mime_module>
        AddHandler application/x-httpd-php .php
</IfModule>
DocumentRoot "/usr/share/html/zabbix"
<Directory "/usr/share/html/zabbix">
</Directory>
<IfModule dir_module>
        DirectoryIndex index.php
</IfModule>    

  配置/usr/local/httpd/conf/extra/httpd-mpm.conf,请根据默认配置添加替换或注释以下关键字段:

<IfModule mpm_prefork_module>
StartServers 1
MinSpareServers 2
MaxSpareServers 5
MaxRequestWorkers 30
MaxConnectionsPerChild 100
</IfModule>

  启动apache:

$ cd /usr/local/httpd/ 
$ ./bin/apachectl start

六、安装PHP

  安装依赖

$ apt-get install libbison-dev re2c libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev

  安装php,将其作为apache的模块运行:

$ wget http://tw1.php.net/distributions/php-5.6.10.tar.bz2
$ tar -jxvf php-5.6.10.tar.bz2
$ cd php*
$ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/httpd/bin/apxs --with-pdo-pgsql=/usr/local/postgresql --with-pgsql=/usr/local/postgresql --with-gd --with-png-dir --with-jpeg-dir --with-freetype-dir --enable-bcmath --enable-ctype --enable-sockets --enable-mbstring --with-gettext --disable-debug
$ make
$ make install
$ cp php.ini-production /fmc/php/etc/php.ini #复制标准配置文件

  配置php.ini,请根据默认配置添加替换以下关键字段:

$ vim /usr/local/php/etc/php.ini
...
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Shanghai
...

七、安装zabbix

  安装依赖

$ apt-get install gettext libopenipmi-dev ipmitool libssh-dev fping libcurl4-openssl-dev libiksemel-dev libsnmp-dev

  安装zabbix

$ wget http://repo.zabbix.com/zabbix/2.4/ubuntu/pool/main/z/zabbix/zabbix_2.4.5.orig.tar.gz
$ tar -zxvf zabbix* && cd zabbix*
$ ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-postgresql=/usr/local/postgresql/bin/pg_config --with-net-snmp --with-jabber --with-libxml2 --with-libcurl --with-openipmi --enable-ipv6
$ make install

  初始化zabbix数据库:

$ cd database/postgresql # 源目录下
$ psql --username=fmc --dbname=zabbix --file=schema.sql #注意数据导入的顺序
$ psql --username=fmc --dbname=zabbix --file=images.sql
$ psql --username=fmc --dbname=zabbix --file=data.sql

  配置zabbix的文件:

$ vim /usr/local/zabbix/etc/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zbx
DBPassword=zbx

  启动zabbix-server、zabbix-agentd:

$ cd /usr/local/zabbix 
$./sbin/zabbix_agentd start #启动代理,用于监视服务器自身
$./sbin/zabbix_server start #启动服务守护进程

  如果启动server时报错:error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory,是在设置postgreSQL环境变量时乜有配置LD_LIBRARY_PATH。

  配置zabbix的前端WEB文件:

$ cd zabbix-2.4.5/frontends/ # 源目录下
$ mkdir -p /usr/share/html/zabbix/
$ cp -ar php/* /usr/share/html/zabbix/ 
$ chown -R zbx:zbx /usr/share/html/zabbix/

  用浏览器访问 zabbix 吧:http://192.168.45.127t, 前端的安装都是图形化操作的。

  安装完成后,你就可以使用了。默认的超级用户是(注意大小写) Admin,密码是 zabbix。

八、设置开机启动

$ vim /etc/rc.local
su - zbx -c "pg_ctl -D $PGDATA -l $PGHOME/pgsql.log start"
cd /usr/local/zabbix && ./sbin/zabbix_agentd start
cd /usr/local/zabbix && ./sbin/zabbix_server start
cd /usr/local/httpd && ./bin/apachectl start
exit 0 #这句在ubuntu中不能删除,否则不会生效。