1. 程式人生 > >01-centos7.2上原始碼編譯部署LNMP+zabbix3.2(2017-03-07)

01-centos7.2上原始碼編譯部署LNMP+zabbix3.2(2017-03-07)

環境準備

centos7.2
nginx-1.9.15
mysql5.7
zabbix-3.2.3
php-5.5.35

配置基礎服務

[root@zbserver
~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@zbserver ~]#

[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777728
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS
=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=eno16777728 UUID=30f74f98-8f20-4e25-aff0-4a23cf7e911d DEVICE=eno16777728 ONBOOT=yes IPADDR=192.168.181.179 GATEWAY=192.168.181.2 NETMASK=255.255.255.0 DNS1=192.168.181.2 [[email protected] ~]#
[root@localhost ~]# yum install vim
[[email protected]
~]# vim /etc/hostname 新增zbserver.create80.com
[[email protected] ~]# hostname
zbserver.create80.com

修改/etc/hosts檔案

[root@localhost ~]# vim /etc/hosts

新增:
192.168.181.179 zbserver.create80.com zabbix1

檢視:

[[email protected] ~]# hostnamectl status
   Static hostname: zbserver.create80.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b05666a8061b46f39567ae72d1cd8157
           Boot ID: 1e97e7bf0412441fa5b3015accee0263
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[[email protected] ~]#

配置時鐘同步:

[root@localhost ~]# timedatectl
      Local time: 四 2017-03-09 02:55:33 CST
  Universal time: 三 2017-03-08 18:55:33 UTC
        RTC time: 三 2017-03-08 18:55:33
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@localhost ~]#

安裝ntp

[root@zbserver ~]# yum install ntp

[root@zbserver ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@zbserver ~]# systemctl start ntpd

設定開機自啟動

[root@zbserver ~]# systemctl enable ntpd.service

參考筆記:
http://www.centoscn.com/CentOS/config/2015/1105/6385.html

關閉防火牆

[root@zbserver ~]# systemctl status firewalld
[root@zbserver ~]# systemctl stop firewalld

關閉selinux

[[email protected] ~]# 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=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@zbserver ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@zbserver ~]# grep -n ^[A-Z] /etc/selinux/config
7:SELINUX=disabled
12:SELINUXTYPE=targeted
[root@zbserver ~]#

安裝mysql資料庫

CentOS7預設安裝了和MySQL有相容性的MariaDB資料庫,在我們安裝MySQL5.7之前為了避免發生衝突首先刪除MariaDB

[root@zbserver ~]# rpm -qa | grep maria
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@zbserver ~]# yum remove mariadb-libs -y

新增mysql的yum源
在CentOS7上yum安裝MySQL需使用MySQL的yum源。執行以下命令首先新增MySQL的yum源。

[root@zbserver soft]# yum install wget
[root@zbserver soft]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm


[root@zbserver soft]# yum localinstall mysql57-community-release-el7-8.noarch.rpm

安裝完後得到兩個檔案

[[email protected] yum.repos.d]# ll   /etc/yum.repos.d/
總用量 36
-rw-r--r--. 1 root root 1664 12月  9 2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 12月  9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 12月  9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 12月  9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 12月  9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 12月  9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 12月  9 2015 CentOS-Vault.repo
-rw-r--r--  1 root root 1222 3月  23 2016 mysql-community.repo
-rw-r--r--  1 root root 1236 3月  23 2016 mysql-community-source.repo
[[email protected] yum.repos.d]#

安裝完成之後驗證MySQL YUM repository

[[email protected] yum.repos.d]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64        MySQL Connectors Community           30
mysql-tools-community/x86_64             MySQL Tools Community                43
mysql57-community/x86_64                 MySQL 5.7 Community Server          166
[[email protected] yum.repos.d]#

這裡寫圖片描述

開始安裝

[[email protected] yum.repos.d]# yum install mysql-community-server

這裡寫圖片描述

[root@zbserver yum.repos.d]# mysqld --version
mysqld  Ver 5.7.17 for Linux on x86_64 (MySQL Community Server (GPL))

配置mysql開機自啟動

[[email protected] yum.repos.d]# systemctl enable mysqld.service
[[email protected] yum.repos.d]# systemctl start mysqld.service

安裝MySQL 5.7之後,MySQL會生成一個隨機密碼在/var/log/mysqld.log檔案中,使用如下命令檢視密碼:

[root@zbserver log]# grep 'temporary password' /var/log/mysqld.log
2017-03-08T05:39:40.970412Z 1 [Note] A temporary password is generated for root@localhost: +hyQTJ(K6wnf
[root@zbserver log]#

[root@zbserver log]# grep "password" /var/log/mysqld.log >> /tmp/mysql.password
[root@zbserver log]# cat /tmp/mysql.password
2017-03-08T05:39:40.970412Z 1 [Note] A temporary password is generated for root@localhost: +hyQTJ(K6wnf
[root@zbserver log]#

記錄密碼之後,執行 mysql_secure_installation初始化MySQL,初始化過程中會提示輸入隨機密碼

[root@zbserver log]# mysql_secure_installation
更新密碼為:xxxxxxx

登入:

[root@zbserver log]# mysql -u root -p

設定使用者 root 可以在任意 IP 下被訪問:

mysql>grant all privileges on *.* to [email protected]"%" identified by 'Zabbix_Long01';

設定使用者 root 可以在本地被訪問:

mysql>grant all privileges on *.* to [email protected]"localhost" identified by " Zabbix_Long01";

重新整理許可權使之生效:

mysql> flush privileges;

改變MySQL 的字符集:

mysql> show variables like '%character%';

檢視指定的資料庫中指定資料表的字符集,如檢視 mysql 資料庫中 servers 表的字符集:
show table status from mysql like ‘%servers%’;
檢視指定資料庫中指定表的全部列的字符集,如檢視 mysql 資料庫中 servers 表的全部的列的字符集:
show full columns from servers;

這裡寫圖片描述

設定 MySQL 的字符集為 UTF-8:

開啟 /etc 目錄下的 my.cnf 檔案(此檔案是 MySQL 的主配置檔案):

vi /etc/my.cnf

在 [mysqld] 前新增如下程式碼:

[client]
default-character-set=utf8

在 [mysqld] 後新增如下程式碼:

character_set_server=utf8

這裡寫圖片描述

[root@zbserver ~]# systemctl restart mysqld
[root@zbserver ~]# mysql -u root -p

這裡寫圖片描述

[[email protected] ~]# systemctl stop mysqld

mysql安裝完成,做一個快照。

安裝nginx

先安裝依賴包

[root@zbserver ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel

這裡寫圖片描述

建立使用者名稱:

[root@zbserver ~]# useradd nginx -s /sbin/nologin -M

下載nginx軟體包並進入到目錄中:

wget http://nginx.org/download/nginx-1.9.15.tar.gz && tar xvf nginx-1.9.15.tar.gz && cd nginx-1.9.15

編譯:

[[email protected] nginx-1.9.15]# ./configure --prefix=/usr/local/product/nginx1.9.15 --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre

這裡寫圖片描述

安裝:

[root@zbserver nginx-1.9.15]# make && make install

這裡寫圖片描述

建立軟連結並將Nginx服務加入作業系統啟動組

[root@zbserver nginx-1.9.15]# ln -s /usr/local/product/nginx1.9.15 /usr/local/nginx
[root@zbserver nginx-1.9.15]# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
[root@zbserver nginx-1.9.15]#

安裝php

下載php安裝包:

[[email protected] soft]# wget http://cn2.php.net/get/php-5.5.35.tar.gz/from/this/mirror
[[email protected] soft]# mv mirror php-5.5.35.tar.gz
tar -xvf php-5.5.35.tar.gz
cd php-5.5.35
./configure --prefix=/usr/local/product/php-5.5.35 --with-config-file-path=/usr/local/product/php-5.5.35/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql --with-mysqli --with-mysql --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath

這裡寫圖片描述

安裝:

make(時間比較長,耐心等待結果)

這裡寫圖片描述

make test(時間比較長,耐心等待結果)

這裡寫圖片描述

make install

這裡寫圖片描述

建立超連結:

[root@zbserver php-5.5.35]# ln -s /usr/local/product/php-5.5.35 /usr/local/php

配置php

[root@zbserver php-5.5.35]# cp php.ini-production /usr/local/php/etc/php.ini
[root@zbserver php-5.5.35]# cd /usr/local/php/etc/
[root@zbserver etc]# cp php-fpm.conf.default php-fpm.conf
[root@zbserver etc]#

修改php.ini配置引數
修改php.ini引數:(zabbix環境需要修改的引數)

max_execution_time = 300Default is 30)
memory_limit = 128M (no change, default is 128M)
post_max_size = 16M  (default is 8M)
upload_max_filesize = 2M (no change, default is 2M)
max_input_time = 300   (default is 60)
date.timezone = PRC    (default is empty)

將PHP服務加入作業系統啟動組

[root@zbserver etc]# echo "/usr/local/php/sbin/php-fpm" >>/etc/rc.local

安裝zbabix

[root@zbserver ~]# groupadd zabbix
[root@zbserver ~]# useradd -g zabbix zabbix

在mysql或PostgreSQL中建立Zabbix資料庫(本文以mysql為例)

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.03 sec)

mysql> grant all privileges on zabbix.* to [email protected] identified by 'Zabbix_Long01';
Query OK, 0 rows affected, 1 warning (0.02 sec)

設定使用者 zabbix可以在任意 IP 下被訪問
mysql> grant all privileges on *.* to [email protected]"%" identified by 'Zabbix_Long01'
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to [email protected]"localhost" identified by 'Zabbix_Long01';
Query OK, 0 rows affected, 1 warning (0.00 sec)


[[email protected] mysql]# mysql -u zabbix -pZabbix_Long01 zabbix<schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -u zabbix -pZabbix_Long01 zabbix<images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -u zabbix -pZabbix_Long01 zabbix<data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]#

解壓zabbix

tar -zxvf zabbix-3.2.1.tar.gz
[[email protected] mysql]# mysql -u zabbix -pZabbix_Long01 zabbix<schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -u zabbix -pZabbix_Long01 zabbix<images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -u zabbix -pZabbix_Long01 zabbix<data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]#
[[email protected] soft]# rpm -ivh mysql-community-devel-5.7.16-1.el6.x86_64.rpm
準備中...                          ################################# [100%]
正在升級/安裝...
   1:mysql-community-devel-5.7.16-1.el################################# [100%]
[[email protected] soft]#

yum install libcurl-devel

yum install net-snmp-devel
yum install libxml2-devel
yum install gcc

跳轉到Zabbix解壓目錄:

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

[[email protected] zabbix-3.2.3]# make install

這裡寫圖片描述

Zabbix Server基本配置

[[email protected] zabbix-3.2.3]# cd /usr/local/etc/
[[email protected] etc]# vim zabbix_server.conf

[[email protected] conf]# grep ^[A-Z] /usr/local/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log
DBName=zabbix
DBUser=zabbix
DBPassword=Zabbix_Long01
DBPort=3306
StartVMwareCollectors=5
VMwarePerfFrequency=60
VMwareCacheSize=8M
Timeout=4
LogSlowQueries=3000
[[email protected] conf]#

修改zabbix_agentd.conf檔案

[[email protected] etc]# vim zabbix_agentd.conf
[[email protected] etc]# grep ^[A-Z] zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=192.168.181.179
ServerActive=zbserver.create80.com
Hostname=zbserver.create80.com
[[email protected] etc]#

將zabbix服務加入作業系統啟動服務:

echo "/usr/local/sbin/zabbix_server" >>/etc/rc.local

echo "/usr/local/sbin/zabbix_agentd" >>/etc/rc.local

建立Zabbix Server服務:

[root@zbserver etc]# cp /opt/soft/zabbix-3.2.3/misc/init.d/fedora/core/zabbix_server /usr/lib/systemd/system/
[root@zbserver etc]# cd  /usr/lib/systemd/system/
[root@zbserver init.d]# chmod +x zabbix_server
[root@zbserver init.d]#

建立Zabbix agentd服務:

[root@zbserver init.d]# cp /opt/soft/zabbix-3.2.3/misc/init.d/fedora/core/zabbix_agentd /usr/lib/systemd/system/
[root@zbserver init.d]# cd  /usr/lib/systemd/system/
[root@zbserver init.d]# chmod +x zabbix_agentd
[root@zbserver init.d]#

Nginx和Zabbix Server Front Web配置

[root@zbserver html]# mkdir -p zabbix
[root@zbserver html]# cd zabbix/
[root@zbserver zabbix]# mkdir -p logs
[root@zbserver zabbix]# cp -rp /opt/soft/zabbix-3.2.3/frontends/php/* /usr/local/product/nginx1.9.15/html/zabbix/
[root@zbserver zabbix]#

修改NGINX配置檔案

cd /usr/local/product/nginx1.9.15/conf
mkdir extra
cd extra

[[email protected] extra]# vim zabbix.conf
server {
listen 80;
server_name zbserver.create80.com;
access_log /usr/local/product/nginx1.9.15/html/zabbix/logs/zbserver.create80.com.access.log main;
index index.html index.php index.html;
root /usr/local/product/nginx1.9.15/html/zabbix;

location /{
               try_files $uri $uri/ /index.php?$args;
}

location ~ ^(.+.php)(.*)$ {
               fastcgi_split_path_info ^(.+.php)(.*)$;
               include fastcgi.conf;
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_index index.php;
               fastcgi_param PATH_INFO $fastcgi_path_info;
}

}

修改nginx.conf

[root@zbserver extra]# cd /usr/local/product/nginx1.9.15/conf/
vim nginx.conf
[[email protected] extra]# cat nginx.conf

user  nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    include extra/*.conf;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;