第一步 關閉selinux以及防火牆,被這兩個東西坑過很多次了

[root@localhost ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled ###關閉selinux # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 #臨時關閉 [root@localhost ~]# getenforce Permissive

[root@localhost ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 六 2019-10-12 16:53:25 CST; 3 days ago Docs: man:firewalld(1) Main PID: 8309 (firewalld) CGroup: /system.slice/firewalld.service └─8309 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 10月 12 16:53:24 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 10月 12 16:53:25 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
第二步 部署lamp或者lnmp環境,這里以部署lamp環境做演示。
首先apache的安裝: 01 [root@localhost ~]# yum -y install httpd
接着mysql/mariadb的安裝: yum install mysql mysql-server -y (yum install mariadb mariadb-server -y ,啟動服務器后,輸入mysql_secure_installation回車進行修改命令)
這里安裝mariadb 02 [root@localhost ~]# yum install mariadb mariadb-server -y
然后php的安裝:03 [root@localhost ~]# yum install php php-mysql php-devel -y
接着啟動對應服務和設置開機自啟

[root@localhost ~]# systemctl start httpd [root@localhost ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@localhost ~]# systemctl start mariadb [root@localhost ~]# systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. [root@localhost ~]#
設置數據庫賬號密碼
mysql_secure_installation #在Set root password? [Y/n]后面輸入 y,設置密碼123456,在Disallow root login remotely? [Y/n] 后面輸入n,允許root用戶遠程登錄,其他選項直接回車默認就可以了

[root@localhost ~]# 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] n ... skipping. 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] n ... skipping. 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] n ... skipping. 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! [root@localhost ~]#
寫一個頁面測試php和mariadb是否已經在Apache里正常運行,以下內容是用來檢測用php連接mariadb的,連接成功顯示success,失敗顯示fail(說明哪里沒配置正確,需要檢查)
直接復制一下內容到終端中執行就可以了(注意,$link之類的變量前面必須加個斜桿轉義,否則系統會把他當做一個變量,不能將這個變量名打印到文本中)
cat > /var/www/html/test.php <<EOF <?php \$link=mysql_connect('localhost','root','123456'); if(!\$link) echo "fail"; else echo "success"; mysql_close(); ?> EOF
訪問服務器ip/test.php,如果返回success則說明可以了
第三步 編譯安裝zabbix
3.1 下載源碼包就行,我下載的是4.0版本 LTS 的 https://www.zabbix.com/download
3.2 解壓
[root@localhost ~]# cd /opt/ [root@localhost opt]# ls /opt/ zabbix-4.0.13.tar.gz [root@localhost opt]# tar -zxvf zabbix-4.0.13.tar.gz [root@localhost opt]# ls zabbix-4.0.13 zabbix-4.0.13.tar.gz [root@localhost opt]#
3.3 開始編譯,執行sql文本時注意導入順序
設置zabbix用戶與數據庫
[root@localhost opt]# cd zabbix-4.0.13 [root@localhost zabbix-4.0.13]# groupadd --system zabbix [root@localhost zabbix-4.0.13]# useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix [root@localhost zabbix-4.0.13]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 5.5.64-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)]> create database zabbix character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> quit Bye
導入zabbix的數據
[root@localhost ~]# cd /opt/zabbix-4.0.13/database/mysql/
[root@localhost mysql]# mysql -uzabbix -pzabbix
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 5.5.64-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)]> use zabbix Database changed MariaDB [zabbix]> source schema.sql;
MariaDB [zabbix]> source images.sql;
MariaDB [zabbix]> source data.sql;
MariaDB [zabbix]> quit
Bye
[root@localhost mysql]#
[root@localhost ~]# yum install net-snmp gcc mysql-devel libxml2-devel net-snmp-devel libevent-devel curl-devel -y
[root@localhost ~]# cd /opt/zabbix-4.0.13
[root@localhost zabbix-4.0.13]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
[root@localhost zabbix-4.0.13]# make install

[root@localhost zabbix-4.0.13]# cd /usr/local/zabbix/ [root@localhost zabbix]# tree -L 3 . ├── bin │ ├── zabbix_get │ └── zabbix_sender ├── etc │ ├── zabbix_agentd.conf │ ├── zabbix_agentd.conf.d │ ├── zabbix_server.conf │ └── zabbix_server.conf.d ├── lib │ └── modules ├── sbin │ ├── zabbix_agentd │ └── zabbix_server └── share ├── man │ ├── man1 │ └── man8 └── zabbix ├── alertscripts └── externalscripts
接下來還需要修改zabbix的配置文件,[root@localhost zabbix]# vim /usr/local/zabbix/etc/zabbix_server.conf 取消DBPassword的注釋並將其值改為zabbix。這個不修改回導致zabbix_server起不來
現在可以啟動zabbix_server了
[root@localhost zabbix]# /usr/local/zabbix/sbin/zabbix_server
[root@localhost zabbix]#
自動啟動systemd 參考:https://www.cnblogs.com/linux985/p/11691774.html
[root@localhost ~]# netstat -anput | grep zabbix #查看服務是否起來了
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 9427/zabbix_server
tcp6 0 0 :::10051 :::* LISTEN 9427/zabbix_server
[root@localhost ~]#
注意:zabbix_server默認日志文件位於/tmp/zabbix_server.log,有問題可以查看這個日志文件
[root@localhost zabbix-4.0.13]# cp -r frontends/php/ /var/www/html/zabbix #先返回到解壓后的目錄再執行這條命令,這個遷移的是zabbix前端文件
現在我們先來嘗試訪問zabbix ,訪問地址服務器ip/zabbix
點擊下一步
這個需要修改php配置文件和添加php擴展就可以了
我們先修改/etc/php.ini文件,將對應參數做如下修改
[root@localhost zabbix-4.0.13]# vim /etc/php.ini [root@localhost zabbix-4.0.13]# sed -i s/'^max_execution_time.*'/'max_execution_time = 300'/g /etc/php.ini [root@localhost zabbix-4.0.13]# sed -i s/'^max_input_time.*'/'max_input_time = 300'/g /etc/php.ini [root@localhost zabbix-4.0.13]# sed -i s/'^post_max_size.*'/'post_max_size = 16M'/g /etc/php.ini [root@localhost zabbix-4.0.13]# sed -i s/'^;date.timezone.*'/'date.timezone = Asia\/Shanghai'/g /etc/php.ini
修改后的參數
max_execution_time = 300
max_input_time = 300
post_max_size = 16M
date.timezone =Asia/Shanghai
添加擴展
[root@localhost zabbix-4.0.13]# yum install php-gd php-xml php-bcmath php-mbstring -y
PHP ldap Warning警告問題解決
[root@localhost ~]# scp /usr/lib64/php/modules/ldap.so root@10.192.27.112:/usr/lib64/php/modules/
root@10.192.27.112's password:
ldap.so
[root@localhost ~]# vim /etc/php.ini
; If you wish to have an extension loaded automatically, use the following ; syntax: ; extension=modulename.extension extension=ldap.so #增加一行
找到這兩個rpm包安裝,注意一下版本,剛開始,我使用的是php-bcmath-5.4.16-45.el7.x86_64.rpm這個版本的,后面發現會有依賴問題,所以后面下了另一個版本的php-bcmath-5.4.16-42.el7.x86_64.rpm,安裝方式rpm -ivh rpm包名
systemctl restart httpd 重啟一下httpd,刷新頁面就發現不會報錯了
那個警告可以忽略,點擊下一步。第二次安裝時我遇到了另一個問題,那就是提示The frontend does not match Zabbix database. Current database version (mandatory/optional): 3050047/3050047. Required mandatory version: 4000000. Contact your system administrator.之類的問題,具體解決方法是
mysql> use zabbix;
mysql> update dbversion set mandatory=4000000; 將后面的4000000根據情況改為對應的值
mysql> flush privileges;
隨便起個名
下一步
再下一步
按操作提示,下載配置文件,放到指定位置就可以了
點擊下一步
點擊完成,默認賬號Admin密碼zabbix
登錄效果
###參考以下文檔
zabbix4.0離線快速編譯安裝(編譯安裝方法)
本博客已整理更新至第三版。更新於2019.5.28
其實這個主要是想試一下離線編譯安裝的具體步驟,記得要配置好本地yum雲,因為我們需要使用yum,yum能幫我們自動解決很多依賴問題。發現最主要的問題是,當編譯安裝時,會提示沒有對應包,所以有一個網站我們需要到上面去找依賴包,網址http://rpm.pbone.net/,當我們找不到某些要用到的包時,可以考慮到這個網址里下載。值得注意的是php缺少的某些擴展,可以通過yum安裝,php會自動識別這些擴展的
一般來說,直接執行以下腳本也可以了。腳本統一放在root目錄下
腳本1
systemctl stop firewalld systemctl disable firewalld sed -i 's/=enforcing/=disabled/g' /etc/selinux/config setenforce 0 yum install httpd mariadb mariadb-server php php-mysql php-devel php-xml php-bcmath php-mbstring php-gd wget expect net-snmp gcc mysql-devel libxml2-devel net-snmp-devel libevent-devel curl-devel -y wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.2.1/zabbix-4.2.1.tar.gz systemctl start httpd systemctl enable httpd systemctl enable mariadb systemctl start mariadb chmod +x initmysqlpassword.sh /root/initmysqlpassword.sh tar -zxvf zabbix-4.2.1.tar.gz cd zabbix-4.2.1 groupadd --system zabbix useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix cd database/mysql/ mysql -uroot -p123456 -e "create database zabbix character set utf8 collate utf8_bin;" mysql -uroot -p123456 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';" mysql -uroot -p123456 -e "flush privileges;" mysql -uzabbix -pzabbix -e "use zabbix;source schema.sql;source images.sql;source data.sql;" cd ../.. ./configure --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 make install sed -i s/'# DBPassword='/'DBPassword=zabbix'/g /usr/local/etc/zabbix_server.conf cp -r frontends/php/ /var/www/html/zabbix sed -i s/'^max_execution_time.*'/'max_execution_time = 300'/g /etc/php.ini sed -i s/'^max_input_time.*'/'max_input_time = 300'/g /etc/php.ini sed -i s/'^post_max_size.*'/'post_max_size = 16M'/g /etc/php.ini sed -i s/'^;date.timezone.*'/'date.timezone = Asia\/Shanghai'/g /etc/php.ini systemctl restart httpd systemctl restart mariadb zabbix_server
腳本2,無需手動自行,只需將該腳本命名為initmysqlpassword.sh
#!/usr/bin/expect spawn mysql_secure_installation expect "Enter current password for root (enter for none):" send "\r" expect "Set root password? " send "Y\r" expect "New password:" send "123456\r" expect "Re-enter new password:" send "123456\r" expect "Remove anonymous users? " send "y\r" expect "Disallow root login remotely? " send "n\r" expect "Remove test database and access to it? " send "y\r" expect "Reload privilege tables now? " send "y\r" expect eof
常見錯誤
源碼安裝zabbix3.2.7時PHP ldap Warning
問題如下
解決方法:
1、首先查看源碼安裝的php模塊中是否有ldap.so
[root@nms ldap]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/
total 1576
-rwxr-xr-x 1 root root 129484 Aug 22 10:18 ldap.so # 出現告警是因為此模塊不存在,須要編譯生成此模塊並重新加載
-rwxr-xr-x 1 root root 1430846 Aug 21 19:38 opcache.so
[root@nms ldap]#
2、如何本機中已經有ldap.so,只須要在php.ini文件中添加extension=ldap.so
864 ; Dynamic Extensions ; 865 ;;;;;;;;;;;;;;;;;;;;;; 866 867 ; If you wish to have an extension loaded automatically, use the following 868 ; syntax: 869 extension=ldap.so #新添加內容 870 ; 871 ; extension=modulename.extension 872 ; 873 ; For example, on Windows: 874 ; 875 ; extension=msql.dll 876 ; 877 ; ... or under UNIX: 878 ; 879 ; extension=msql.so 880 ; 881 ; ... or with a path: 882 ; 883 ; extension=/path/to/extension/msql.so 884 ;
3、如果相應的模塊不存在
3.1下載相應的php版本源碼到主目錄,進行源碼編譯ldap模塊(重點,其他模塊如果缺失編譯方法一樣)
[root@nms ~]# tar jxvf php-7.1.7.tar.bz2 [root@nms ~]# ls anaconda-ks.cfg install.log lnmp-install.log php-7.1.7.tar.bz2 databases_backup_20170821115223 install.log.syslog php-7.1.7
[root@nms ldap]# cd /root/php-7.1.7/ext/ldap/ #進入模塊所在的源碼位置
[root@nms ldap]# ll
total 144
-rw-rw-r-- 1 1000 1000 7374 Jul 6 17:30 config.m4
-rw-rw-r-- 1 1000 1000 1008 Jul 6 17:30 config.w32
-rw-rw-r-- 1 1000 1000 77 Jul 6 17:30 CREDITS
-rw-rw-r-- 1 1000 1000 102189 Jul 6 17:30 ldap.c
-rw-rw-r-- 1 1000 1000 4464 Jul 6 17:30 ldap.mak
-rw-rw-r-- 1 1000 1000 1546 Jul 6 17:30 LDAP_Win32_HOWTO.txt
-rw-rw-r-- 1 1000 1000 2315 Jul 6 17:30 php_ldap.h
drwxrwxr-x 2 1000 1000 12288 Jul 6 17:30 tests
[root@nms ldap]# /usr/local/php/bin/phpize #編譯PHP擴展的工具,主要是根據系統信息生成對應的configure文件
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend Extension Api No: 320160303
[root@nms ldap]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-ldap #編譯
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
........
[root@nms ldap]# make && make install #安裝
/bin/sh /root/php-7.1.7/ext/ldap/libtool --mode=compile cc -DLDAP_DEPRECATED=1 -I. -I/root/php-7.1.7/ext/ldap -DPHP_ATOM_INC -I/root/php-7.1.7/ext/ldap/include -I/root/php-7.1.7/ext/ldap/main -I/root/php-7.1.7/ext/ldap -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/php-7.1.7/ext/ldap/ldap.c -o ldap.lo
mkdir .libs
cc -DLDAP_DEPRECATED=1 -I. -I/root/php-7.1.7/ext/ldap -DPHP_ATOM_INC -I/root/php-7.1.7/ext/ldap/include -I/root/php-7.1.7/ext/ldap/main -I/root/php-7.1.7/ext/ldap -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/php-7.1.7/ext/ldap/ldap.c -fPIC -DPIC -o .libs/ldap.o
/bin/sh /root/php-7.1.7/ext/ldap/libtool --mode=link cc -DPHP_ATOM_INC -I/root/php-7.1.7/ext/ldap/include -I/root/php-7.1.7/ext/ldap/main -I/root/php-7.1.7/ext/ldap -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -o ldap.la -export-dynamic -avoid-version -prefer-pic -module -rpath /root/php-7.1.7/ext/ldap/modules ldap.lo -lldap -llber
cc -shared .libs/ldap.o -lldap -llber -Wl,-soname -Wl,ldap.so -o .libs/ldap.so
creating ldap.la
(cd .libs && rm -f ldap.la && ln -s ../ldap.la ldap.la)
/bin/sh /root/php-7.1.7/ext/ldap/libtool --mode=install cp ./ldap.la /root/php-7.1.7/ext/ldap/modules
cp ./.libs/ldap.so /root/php-7.1.7/ext/ldap/modules/ldap.so
cp ./.libs/ldap.lai /root/php-7.1.7/ext/ldap/modules/ldap.la
PATH="$PATH:/sbin" ldconfig -n /root/php-7.1.7/ext/ldap/modules
----------------------------------------------------------------------
Libraries have been installed in:
/root/php-7.1.7/ext/ldap/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR‘
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH‘ environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH‘ environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR‘ linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf‘
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don‘t forget to run ‘make test‘.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/ #make 生成模塊安裝位置
[root@nms ldap]#
[root@nms php-7.1.7]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/ #確認ldap.so模塊存在
total 1576
-rwxr-xr-x 1 root root 129484 Aug 22 10:18 ldap.so
-rwxr-xr-x 1 root root 1430846 Aug 21 19:38 opcache.so
[root@nms php-7.1.7]# vi /usr/local/php/etc/php.ini #編輯php.ini,添加新生成的ldap.so模塊
853 default_socket_timeout = 60
854
855 ; If your scripts have to deal with files from Macintosh systems,
856 ; or you are running on a Mac and need to deal with files from
857 ; unix or win32 systems, setting this flag will cause PHP to
858 ; automatically detect the EOL character in those files so that
859 ; fgets() and file() will work regardless of the source of the file.
860 ; http://php.net/auto-detect-line-endings
861 ;auto_detect_line_endings = Off
862
863 ;;;;;;;;;;;;;;;;;;;;;;
864 ; Dynamic Extensions ;
865 ;;;;;;;;;;;;;;;;;;;;;;
866
867 ; If you wish to have an extension loaded automatically, use the following
868 ; syntax:
869 extension=ldap.so #新添加模塊
870 ;
871 ; extension=modulename.extension
872 ;
873 ; For example, on Windows:
874 ;
875 ; extension=msql.dll
876 ;
877 ; ... or under UNIX:
878 ;
879 ; extension=msql.so
880 ;
[root@nms php-7.1.7]# service httpd restart #重啟httpd
restart apache... done
4、http://192.168.0.198/zabbix/setup.php再次進入前端安裝
發現PHP LDAP模塊 OK!!
5、至此zabbix 前面安裝 “Check of pre-requisites”全部OK