Quantcast
Channel: polygun2000的博客
Viewing all articles
Browse latest Browse all 55

centos7+nginx+php-fpm+zabbix安装文档

$
0
0
测试用的快速安装文档,纯粹流水账。

1.安装epel,nginx,zabbix,percona的官方源
# yum install epel-release
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# rpm -Uvh http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.rpm
# rpm -Uvh http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm

2.更新系统到最新状态,关闭SELinux
# yum update
# vi /etc/selinux/config 
  ===> SELINUX=disabled
# reboot

3.安装nginx
# yum install nginx
# systemctl start nginx.service
# systemctl enable nginx.service

打开浏览器,输入http://your_server_IP_address/  可以看到"welcome to nginx"页面

4.安装php、php-fpm、zendopcache
# yum install php-fpm php-cli php-pecl-zendopcache

# vi /etc/php.ini
  ===> cgi.fix_pathinfo=0

#  vi /etc/php-fpm.d/www.conf
  ===> listen = /var/run/php-fpm/php-fpm.sock
  ===> listen.owner = nobody
  ===> listen.group = nobody
  ===> user = nginx
  ===> group = nginx

创建php使用的session存储目录,并设置正确的属主和权限  
# mkdir -p /var/lib/php/session
# chown nginx:nginx /var/lib/php/session
# chmod 755 /var/lib/php/session

# systemctl start php-fpm.service
# systemctl enable php-fpm.service

5.配置nginx使用php-fpm
# /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  server_domain_name_or_IP;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

# systemctl restart nginx

6.测试php工作正常
# vi /usr/share/nginx/html/info.php



打开浏览器访问,http://your_server_IP_address/info.php
如果出现PHP版本等信息,则说明php工作正常。

# rm /usr/share/nginx/html/info.php

5.安装percona,并初始化
# yum install Percona-Server-server-56 Percona-Server-client-56
# systemctl start mysqld.service
# mysql_secure_installation
# systemctl enable mysqld.service

6.安装zabbix
# yum install php-mysql php-gd php-xml php-bcmath php-mbstring
# yum install zabbix-get zabbix-server-mysql zabbix-agent 

7.初始化zabbix数据库
# mysql -u root -p
mysql> create database zabbixdb character set utf8;
mysql> grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by 'password';
mysql> flush privileges;
mysql> exit

为了避免history等大表查询慢的问题,从开始建表就做好partition工作。
history,history_log,history_str,history_text,history_uint 这5张表,每天一个partition
trends,trends_uint 这2张表,每月一个partition

我们需要修改一下zabbix自带的建库SQL文件schema.sql,因为太长没法贴,说明一下:
history,history_log,history_str,history_text,history_uint 这5张表的建库SQL修改结尾为:

ENGINE=InnoDB
PARTITION BY RANGE(clock)
(
PARTITION p2016_02_13 VALUES LESS THAN (UNIX_TIMESTAMP("2016-02-13 00:00:00")) ENGINE = InnoDB,
PARTITION p2016_02_14 VALUES LESS THAN (UNIX_TIMESTAMP("2016-02-14 00:00:00")) ENGINE = InnoDB
);

trends,trends_uint 这2张表的建库SQL修改结尾为:
ENGINE=InnoDB
PARTITION BY RANGE (clock)
(
PARTITION p2016_01 VALUES LESS THAN (UNIX_TIMESTAMP("2016-01-01 00:00:00")) ENGINE = InnoDB,
PARTITION p2016_02 VALUES LESS THAN (UNIX_TIMESTAMP("2016-02-01 00:00:00")) ENGINE = InnoDB
);

history_log这张表的建库SQL:
删除这一行====> PRIMARY KEY (id)
增加这一行====> CREATE INDEX `history_log_0` ON `history_log` (`id`);
删除这一行====> CREATE UNIQUE INDEX `history_log_2` ON `history_log` (`itemid`,`id`);

history_text这张表的建库SQL:
删除这一行====> PRIMARY KEY (id)
增加这一行====> CREATE INDEX `history_text_0` ON `history_text` (`id`);
删除这一行====> CREATE UNIQUE INDEX `history_text_2` ON `history_text` (`itemid`,`id`);

housekeeper这张表的建库SQL:
修改====> ENGINE=BLACKHOLE;

注意: NOT NULL后边的逗号(,) , 一定要注意,多了就会报语法错误了。

# mysql -u zabbixuser -p zabbixdb < /usr/share/doc/zabbix-server-mysql-2.4.7/create/schema.sql
# mysql -u zabbixuser -p zabbixdb < /usr/share/doc/zabbix-server-mysql-2.4.7/create/images.sql
# mysql -u zabbixuser -p zabbixdb < /usr/share/doc/zabbix-server-mysql-2.4.7/create/data.sql

建立存储过程,定时自动增加新分区,删除旧分区,对zabbix官方wiki提供的SQL做了一点修改,主要点如下:
删除====> USE `zabbix`$$
删除zabbix.,修改库名为zabbixdb====> CALL drop_partitions('zabbixdb');
删除zabbix.,修改库名为zabbixdb====> CALL create_next_partitions('zabbixdb')
SQL文件详细见文末),

# mysql -u zabbixuser -p zabbixdb < auto_partition.sql

开启event_scheduler
mysql>  SHOW GLOBAL VARIABLES LIKE 'event_scheduler';
mysql>  SET GLOBAL event_scheduler = ON;

# vi /etc/my.cnf
[mysqld]
 ====> event-scheduler=ON

注意: 因为partition要求时间顺序严格递增,所以务必使用ntp确保时间准确,否则event_scheduler会执行失败。

8.修改zabbix配置文件
# vi /etc/zabbix/zabbix_server.conf
  ===> DBHost=localhost
  ===> DBName=zabbixdb
  ===> DBUser=zabbixuser
  ===> DBPassword=password
  
# vi /etc/zabbix/zabbix_agentd.conf
  ===> Server=127.0.0.1
  ===> ServerActive=127.0.0.1
  ===> Hostname=127.0.0.1

# vi /etc/php.ini
  ===> max_execution_time = 600
  ===> max_input_time = 600
  ===> memory_limit = 256M
  ===> post_max_size = 32M
  ===> upload_max_filesize = 16M
  ===> date.timezone = Asia/Shanghai

# systemctl start zabbix-server 
# systemctl start zabbix-agent
# systemctl enable zabbix-server
# systemctl enable zabbix-agent

9.安装zabbix的webGUI
从zabbix官网下载源码包,复制包中的php目录到nginx文档目录下
# mkdir /usr/share/nginx/html/zabbix
# tar xvzf zabbix-2.4.7.tar.gz
# cd frontends/php
# cp -a . /usr/share/nginx/html/zabbix
# chown -R nginx.nginx /usr/share/nginx/html/zabbix
# systemctl restart mysqld.service
# systemctl restart php-fpm.service
# systemctl restart nginx

打开浏览器,访问http://your_server_IP_address/zabbix完成安装。
默认用户名: admin
默认密码: zabbix

10.zabbix server本身的监控配置

激活zabbix server本身的监控
Configuration -> Hosts -> 勾选(zabbix server) -> 选择"Enable selected" -> Go

等几分钟以后,查看zabbix是否有收集到监控数据
Monitoring -> Latest data
Monitoring -> Screens

11.关闭Housekeeping功能
Administration-->General-->Housekeeping
取消所有的Enable internal housekeeping,然后Update

12.被监控的主机安装zabbix agent
# rpm -Uv  http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.rpm
# yum install zabbix-agent

# vi /etc/zabbix/zabbix_agentd.conf
Server=192.168.1.150
ServerActive=192.168.1.150
Hostname=server2.unixmen.local

# systemctl start zabbix-agent
# systemctl enable zabbix-agent

[安装TokuDB引擎参考如下步骤,可选]
-----------------开始-----------------------
# yum install jemalloc
# vi /etc/my.cnf
[mysqld_safe]
  ===> malloc-lib=/usr/lib64/libjemalloc.so.1

使用tokudb需要关闭transparent_hugepage支持,可以如下确认
# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never

修改grub内核参数,关闭transparent_hugepage
# vi /etc/default/grub
  ===> GRUB_CMDLINE_LINUX_DEFAULT="transparent_hugepage=never" 

重新生成grub配置文件
# grub2-mkconfig -o /boot/grub2/grub.cfg
# reboot

安装tokudb引擎
# yum install Percona-Server-tokudb-56

启用tokudb引擎
# ps_tokudb_admin --enable -uroot -p

确认tokudb引擎状态和版本
mysql> SHOW ENGINES;
...
 | TokuDB | YES | Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology | YES | YES | YES |
...
mysql> SELECT @@tokudb_version;
+------------------+
| @@tokudb_version |
+------------------+
| 5.6.28-76.1      |
+------------------+
1 row in set (0.00 sec)
----------------结束-------------------

[自动分区的SQL文件,auto_partition.sql]
这些SQL来源于zabbix官方wiki,https://zabbix.org/wiki/Docs/howto/mysql_partitioning
做了写小改动。
-----------------开始-----------------------
DROP TABLE IF EXISTS `manage_partitions`;
CREATE TABLE `manage_partitions` (
  `tablename` VARCHAR(64) NOT NULL COMMENT 'Table name',
  `period` VARCHAR(64) NOT NULL COMMENT 'Period - daily or monthly',
  `keep_history` INT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT 'For how many days or months to keep the partitions',
  `last_updated` DATETIME DEFAULT NULL COMMENT 'When a partition was added last time',
  `comments` VARCHAR(128) DEFAULT '1' COMMENT 'Comments',
  PRIMARY KEY (`tablename`)
) ENGINE=INNODB;

INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('history', 'day', 30, now(), '');
INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('history_uint', 'day', 30, now(), '');
INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('history_str', 'day', 120, now(), '');
INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('history_text', 'day', 120, now(), '');
INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('history_log', 'day', 120, now(), '');
INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('trends', 'month', 24, now(), '');
INSERT INTO manage_partitions (tablename, period, keep_history, last_updated, comments) VALUES ('trends_uint', 'month', 24, now(), '');

DELIMITER $$
DROP PROCEDURE IF EXISTS `create_next_partitions`$$
CREATE PROCEDURE `create_next_partitions`(IN_SCHEMANAME VARCHAR(64))
BEGIN
    DECLARE TABLENAME_TMP VARCHAR(64);
    DECLARE PERIOD_TMP VARCHAR(12);
    DECLARE DONE INT DEFAULT 0;
 
    DECLARE get_prt_tables CURSOR FOR
        SELECT `tablename`, `period`
            FROM manage_partitions;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
 
    OPEN get_prt_tables;
 
    loop_create_part: LOOP
        IF DONE THEN
            LEAVE loop_create_part;
        END IF;
 
        FETCH get_prt_tables INTO TABLENAME_TMP, PERIOD_TMP;
 
        CASE WHEN PERIOD_TMP = 'day' THEN
                    CALL `create_partition_by_day`(IN_SCHEMANAME, TABLENAME_TMP);
             WHEN PERIOD_TMP = 'month' THEN
                    CALL `create_partition_by_month`(IN_SCHEMANAME, TABLENAME_TMP);
             ELSE
            BEGIN
                            ITERATE loop_create_part;
            END;
        END CASE;
 
                UPDATE manage_partitions set last_updated = NOW() WHERE tablename = TABLENAME_TMP;
    END LOOP loop_create_part;
 
    CLOSE get_prt_tables;
END$$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `create_partition_by_day`$$
CREATE PROCEDURE `create_partition_by_day`(IN_SCHEMANAME VARCHAR(64), IN_TABLENAME VARCHAR(64))
BEGIN
    DECLARE ROWS_CNT INT UNSIGNED;
    DECLARE BEGINTIME TIMESTAMP;
        DECLARE ENDTIME INT UNSIGNED;
        DECLARE PARTITIONNAME VARCHAR(16);
        SET BEGINTIME = DATE(NOW()) + INTERVAL 1 DAY;
        SET PARTITIONNAME = DATE_FORMAT( BEGINTIME, 'p%Y_%m_%d' );
 
        SET ENDTIME = UNIX_TIMESTAMP(BEGINTIME + INTERVAL 1 DAY);
 
        SELECT COUNT(*) INTO ROWS_CNT
                FROM information_schema.partitions
                WHERE table_schema = IN_SCHEMANAME AND table_name = IN_TABLENAME AND partition_name = PARTITIONNAME;
 
    IF ROWS_CNT = 0 THEN
                     SET @SQL = CONCAT( 'ALTER TABLE `', IN_SCHEMANAME, '`.`', IN_TABLENAME, '`',
                                ' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', ENDTIME, '));' );
                PREPARE STMT FROM @SQL;
                EXECUTE STMT;
                DEALLOCATE PREPARE STMT;
        ELSE
        SELECT CONCAT("partition `", PARTITIONNAME, "` for table `",IN_SCHEMANAME, ".", IN_TABLENAME, "` already exists") AS result;
        END IF;
END$$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `create_partition_by_month`$$
CREATE PROCEDURE `create_partition_by_month`(IN_SCHEMANAME VARCHAR(64), IN_TABLENAME VARCHAR(64))
BEGIN
    DECLARE ROWS_CNT INT UNSIGNED;
    DECLARE BEGINTIME TIMESTAMP;
        DECLARE ENDTIME INT UNSIGNED;
        DECLARE PARTITIONNAME VARCHAR(16);
        SET BEGINTIME = DATE(NOW() - INTERVAL DAY(NOW()) DAY + INTERVAL 1 DAY + INTERVAL 1 MONTH);
        SET PARTITIONNAME = DATE_FORMAT( BEGINTIME, 'p%Y_%m' );
 
        SET ENDTIME = UNIX_TIMESTAMP(BEGINTIME + INTERVAL 1 MONTH);
 
        SELECT COUNT(*) INTO ROWS_CNT
                FROM information_schema.partitions
                WHERE table_schema = IN_SCHEMANAME AND table_name = IN_TABLENAME AND partition_name = PARTITIONNAME;
 
    IF ROWS_CNT = 0 THEN
                     SET @SQL = CONCAT( 'ALTER TABLE `', IN_SCHEMANAME, '`.`', IN_TABLENAME, '`',
                                ' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', ENDTIME, '));' );
                PREPARE STMT FROM @SQL;
                EXECUTE STMT;
                DEALLOCATE PREPARE STMT;
        ELSE
        SELECT CONCAT("partition `", PARTITIONNAME, "` for table `",IN_SCHEMANAME, ".", IN_TABLENAME, "` already exists") AS result;
        END IF;
END$$
DELIMITER ;


DELIMITER $$
DROP PROCEDURE IF EXISTS `drop_partitions`$$
CREATE PROCEDURE `drop_partitions`(IN_SCHEMANAME VARCHAR(64))
BEGIN
    DECLARE TABLENAME_TMP VARCHAR(64);
    DECLARE PARTITIONNAME_TMP VARCHAR(64);
    DECLARE VALUES_LESS_TMP INT;
    DECLARE PERIOD_TMP VARCHAR(12);
    DECLARE KEEP_HISTORY_TMP INT;
    DECLARE KEEP_HISTORY_BEFORE INT;
    DECLARE DONE INT DEFAULT 0;
    DECLARE get_partitions CURSOR FOR
        SELECT p.`table_name`, p.`partition_name`, LTRIM(RTRIM(p.`partition_description`)), mp.`period`, mp.`keep_history`
            FROM information_schema.partitions p
            JOIN manage_partitions mp ON mp.tablename = p.table_name
            WHERE p.table_schema = IN_SCHEMANAME
            ORDER BY p.table_name, p.subpartition_ordinal_position;
 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
 
    OPEN get_partitions;
 
    loop_check_prt: LOOP
        IF DONE THEN
            LEAVE loop_check_prt;
        END IF;
 
        FETCH get_partitions INTO TABLENAME_TMP, PARTITIONNAME_TMP, VALUES_LESS_TMP, PERIOD_TMP, KEEP_HISTORY_TMP;
        CASE WHEN PERIOD_TMP = 'day' THEN
                SET KEEP_HISTORY_BEFORE = UNIX_TIMESTAMP(DATE(NOW() - INTERVAL KEEP_HISTORY_TMP DAY));
             WHEN PERIOD_TMP = 'month' THEN
                SET KEEP_HISTORY_BEFORE = UNIX_TIMESTAMP(DATE(NOW() - INTERVAL KEEP_HISTORY_TMP MONTH - INTERVAL DAY(NOW())-1 DAY));
             ELSE
            BEGIN
                ITERATE loop_check_prt;
            END;
        END CASE;
 
        IF KEEP_HISTORY_BEFORE >= VALUES_LESS_TMP THEN
                CALL drop_old_partition(IN_SCHEMANAME, TABLENAME_TMP, PARTITIONNAME_TMP);
        END IF;
        END LOOP loop_check_prt;
 
        CLOSE get_partitions;
END$$
DELIMITER ;

DELIMITER $$
DROP PROCEDURE IF EXISTS `drop_old_partition`$$
CREATE PROCEDURE `drop_old_partition`(IN_SCHEMANAME VARCHAR(64), IN_TABLENAME VARCHAR(64), IN_PARTITIONNAME VARCHAR(64))
BEGIN
    DECLARE ROWS_CNT INT UNSIGNED;
 
        SELECT COUNT(*) INTO ROWS_CNT
                FROM information_schema.partitions
                WHERE table_schema = IN_SCHEMANAME AND table_name = IN_TABLENAME AND partition_name = IN_PARTITIONNAME;
 
    IF ROWS_CNT = 1 THEN
                     SET @SQL = CONCAT( 'ALTER TABLE `', IN_SCHEMANAME, '`.`', IN_TABLENAME, '`',
                                ' DROP PARTITION ', IN_PARTITIONNAME, ';' );
                PREPARE STMT FROM @SQL;
                EXECUTE STMT;
                DEALLOCATE PREPARE STMT;
        ELSE
        SELECT CONCAT("partition `", IN_PARTITIONNAME, "` for table `", IN_SCHEMANAME, ".", IN_TABLENAME, "` not exists") AS result;
        END IF;
END$$
DELIMITER ;


DELIMITER $$
CREATE EVENT IF NOT EXISTS `e_part_manage`
       ON SCHEDULE EVERY 1 DAY
       STARTS '2016-01-01 21:30:00'
       ON COMPLETION PRESERVE
       ENABLE
       COMMENT 'Creating and dropping partitions'
       DO BEGIN
            CALL drop_partitions('zabbixdb');
            CALL create_next_partitions('zabbixdb');
       END$$
DELIMITER ; 
----------------结束-------------------

【确认分区自动添加成功】
-----------------开始-----------------------
mysql> select * from manage_partitions;   
+--------------+--------+--------------+---------------------+----------+
| tablename    | period | keep_history | last_updated        | comments |
+--------------+--------+--------------+---------------------+----------+
| history      | day    |           30 | 2016-02-15 21:30:00 |          |
| history_log  | day    |          120 | 2016-02-15 21:30:00 |          |
| history_str  | day    |          120 | 2016-02-15 21:30:00 |          |
| history_text | day    |          120 | 2016-02-15 21:30:00 |          |
| history_uint | day    |           30 | 2016-02-15 21:30:00 |          |
| trends       | month  |           24 | 2016-02-15 21:30:00 |          |
| trends_uint  | month  |           24 | 2016-02-15 21:30:00 |          |
+--------------+--------+--------------+---------------------+----------+
7 rows in set (0.00 sec)

mysql> show create table history;
......
(PARTITION p2016_02_13 VALUES LESS THAN (1455339600) ENGINE = InnoDB,
 PARTITION p2016_02_14 VALUES LESS THAN (1455426000) ENGINE = InnoDB,
 PARTITION p2016_02_16 VALUES LESS THAN (1455685200) ENGINE = InnoDB)
----------------结束-------------------

参考文档:
http://www.unixmen.com/how-to-install-zabbix-server-on-centos-7/
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7
http://www.itzgeek.com/how-tos/linux/centos-how-tos/nginx-php-fpm-mariadb-on-centos-7-rhel-7.html
http://www.unixmen.com/install-lamp-server-apache-mariadb-php-centosrhelscientific-linux-7/
https://www.zabbix.org/wiki/Docs/howto/high_availability_Zabbix_Frontend
https://www.scalescale.com/tips/nginx/install-php-zend-opcache-cache-web-stats-nginx/
http://eddmann.com/posts/storing-php-sessions-file-caches-in-memory-using-tmpfs/
https://www.webfoobar.com/node/31
http://www.server-world.info/en/note?os=CentOS_7&p=zabbix
http://8838848.blog.51cto.com/8828848/1583960
https://bignosekates.wordpress.com/2013/05/07/zabbix-server-is-not-running/
http://www.jincon.com/archives/169/
http://www.cnblogs.com/yyhh/p/4792830.html
https://support.zabbix.com/browse/ZBX-9063
http://php.net/manual/en/opcache.installation.php
https://zabbix.org/wiki/Docs/howto/mysql_partitioning
http://zabbixzone.com/zabbix/history-tables-housekeeper/

 

Viewing all articles
Browse latest Browse all 55

Trending Articles