LoginSignup
45

More than 5 years have passed since last update.

CentOS 6.5でzabbix+nginx+php-fpm環境を構築してみる

Last updated at Posted at 2014-06-29

zabbix インストール

参考:Zabbix サーバ 2.2.3のインストール, Zabbix-2.2をインストールする手順(using PostgreSQL/Nginx)

yumリポジトリ設定

例によって公式リポジトリから最新版を入手します。

yumリポジトリ追加&インストール
$ sudo rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zabbix-release-2.2-1.el6.noarch.rpm
$ sudo yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-web-japanese zabbix-agent
$ zabbix_server -V
Zabbix server v2.2.4 (revision 46772) (23 June 2014)
Compilation time: Jun 26 2014 02:27:30
$ zabbix_agent -V
Zabbix agent v2.2.4 (revision 46772) (23 June 2014)
Compilation time: Jun 26 2014 02:31:36

公式の最新版からインストールします。

インストールが終わったら/etc/yum.repos.d/zabbix.repoenable=0にしておいてもいいかもしれません。

MySQL設定

まずはDB設定からで、MySQLを使用します。

mysqlインストール
$ sudo yum -y install mysql-server
$ sudo /usr/bin/mysql_secure_installation
$ sudo service mysqld start
$ sudo chkconfig mysqld on

ここではmysql-serverが未インストールだったので、yumからのインストールと初期設定をやっていますが、既に使っているDBがあるなら飛ばしてもいいかもしれません。

mysql初期設定
$ mysql -u root -p
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '任意のパスワード';
mysql> exit
$ cd /usr/share/doc/zabbix-server-mysql-2.2.4/create/
$ mysql -u root -p zabbix < schema.sql
$ mysql -u root -p zabbix < images.sql
$ mysql -u root -p zabbix < data.sql

必要なスキーマなどはSQLファイルが用意されているので、読み込ませるだけになります。

zabbix-server設定

概ね必要な設定は初期設定で入っているので、DBPasswordだけ設定します。

/etc/zabbix/zabbix_server.conf
@@ -114,6 +114,8 @@
 # Default:
 # DBPassword=

+DBPassword=MySQLに設定した任意のパスワード
+
 ### Option: DBSocket
 #      Path to MySQL socket.
 #

その他にDBHost/DBName/DBUser辺りは必要に応じて設定変更が必要らしいです。

zabbixサービス起動

zabbix-server起動
$ sudo service zabbix-server start
Starting Zabbix server:                                    [  OK  ]
$ chkconfig --list | grep zabbix
zabbix-agent    0:off   1:off   2:off   3:off   4:off   5:off   6:off
zabbix-server   0:off   1:off   2:off   3:off   4:off   5:off   6:off
$ sudo chkconfig zabbix-server on
$ chkconfig --list zabbix-server
zabbix-server   0:off   1:off   2:on    3:on    4:on    5:on    6:off

まずは、サーバ側のサービスを起動しておきます。

zabbix-agent起動
$ sudo service zabbix-agent start
Starting Zabbix agent:                                     [  OK  ]
$ sudo chkconfig zabbix-agent on
$ chkconfig --list zabbix-agent
zabbix-agent    0:off   1:off   2:on    3:on    4:on    5:on    6:off

ついでに、サーバ本体もzabbix-agentとして登録するために、エージェントも起動しておきました。

zabbix-web設定

nginx側の設定前に、zabbix-web関連のファイルを適当なディレクトリでnginx側から見れるように設定しておきます。

nginxでzabbixを見れるように
$ cd /etc/zabbix
$ ls -ld web
drwxr-x--- 2 apache apache  4096  6月 27 20:34 2014 web
$ sudo chown nginx.nginx web
drwxr-x--- 2 nginx  nginx   4096  6月 27 20:34 2014 web
$ cd /var/www
$ sudo ln -s /usr/share/zabbix .

デフォルト(rpmで入れたばかりの)状態ではapache権限になっていたりするので、その辺りを変更したり、ベースとして/var/www配下でファイルを管理したいので、シンボリックリンクを設定したりします。

zabbix側の設定としてはここまででほぼ完了です。

nginx+php-fpmで監視画面を起動

参考:CentOSにてnginxでPHPを動かす,

nginx設定

nginx側にサーバ設定を丸ごと追加します。もしかしたら単にlocation単位で増やした方が簡単なのかもしれませんが、サーバ設定ファイル丸ごと別管理にしたかったので(^_^;)

ここで追加しているserver_nameについて、別途DNS設定などを追加する必要がありますが、現状はローカルPCの/etc/hostsに追記する方向で動作確認しています。

別途認証とかの設定を追加したりしたら、DNS設定入れようかな…。

/etc/nginx/conf.d/zabbix.conf
server {
        server_name zabbix.example.org;

        root        /var/www/zabbix;
        index       index.html index.php;
        access_log  /var/log/nginx/access_zabbix.log main;
        error_log   /var/log/nginx/error_zabbix.log error;

        location ~ \.php {
                fastcgi_pass    127.0.0.1:9000;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

access/errorログについては、/etc/logrotate.d/nginxで適当にローテートして貰うためにファイルパスを合わせて(/var/log/nginx/*.logにして)います。error_log辺りはデフォルトのerror.logに纏めてしまっても良かったのだろうか…。

サーバ設定が出来たらsudo service nginx restartするだけですが、必要に応じて/etc/nginx/nginx.confserver_names_hash_bucket_sizeを変更する必要があるかもしれません。

php-fpm設定

次にphp関連の設定を変更します。

/etc/php.ini
@@ -437,7 +437,8 @@

 ; Maximum execution time of each script, in seconds
 ; http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time
-max_execution_time = 30
+;max_execution_time = 30
+max_execution_time = 300

 ; Maximum amount of time each script may spend parsing request data. It's a good
 ; idea to limit this time on productions servers in order to eliminate unexpectedly
@@ -446,7 +447,8 @@
 ; Development Value: 60 (60 seconds)
 ; Production Value: 60 (60 seconds)
 ; http://www.php.net/manual/en/info.configuration.php#ini.max-input-time
-max_input_time = 60
+;max_input_time = 60
+max_input_time = 300

 ; Maximum input variable nesting level
 ; http://www.php.net/manual/en/info.configuration.php#ini.max-input-nesting-level
@@ -726,7 +728,8 @@

 ; Maximum size of POST data that PHP will accept.
 ; http://www.php.net/manual/en/ini.core.php#ini.post-max-size
-post_max_size = 8M
+;post_max_size = 8M
+post_max_size = 32M

 ; Magic quotes are a preprocessing feature of PHP where PHP will attempt to
 ; escape any character sequences in GET, POST, COOKIE and ENV data which might
@@ -944,6 +947,7 @@
 ; Defines the default timezone used by the date functions
 ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
 ;date.timezone =
+date.timezone = Asia/Tokyo

 ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.default-latitude
 ;date.default_latitude = 31.7667

php-fpmもデフォルトだとapache権限で動作しようとする為、nginx権限で動作するように、設定を変更しておきます。

/etc/php-fpm.d/www.conf
@@ -36,9 +36,9 @@
 ; Note: The user is mandatory. If the group is not set, the default user's group
 ;       will be used.
 ; RPM: apache Choosed to be able to access some dir as httpd
-user = apache
+user = nginx
 ; RPM: Keep a group allowed to write in log dir.
-group = apache
+group = nginx

 ; Choose how the process manager will control the number of child processes.
 ; Possible Values:

ここまでで最低限の設定は終わったので、php-fpmを自動起動するようにしておきます。

php-fpm起動
$ chkconfig php-fpm --list
php-fpm         0:off   1:off   2:off   3:off   4:off   5:off   6:off
$ sudo service php-fpm start
php-fpm を起動中:                                          [  OK  ]
$ chkconfig php-fpm --list
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

ブラウザでの設定

後はブラウザ側で操作します。

http://zabbix.example.orgにアクセスすることで、初期画面が表示されるので、基本はそのままNextしていけば完了します。

その後、適時設定(監視対象の追加など)が出来るはずです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45