PHP Server Monitor 是一套用來監控服務存活的工具、操作簡單,最近開始支援以 Telegram 發送通知,十分好用!
以下紀錄 CentOS 7 從搭建 LNMP 到安裝 PHP Server Monitor 過程
安裝remi套件
$ sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
啟用 php7.3
$ sudo vim /etc/yum.repos.d/remi-php73.repo
[remi-php73]
name=Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - $basearch
mirrorlist=http://cdn.remirepo.net/enterprise/7/php73/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
安裝 nginx php-fpm mariadb 及一些工具
$ sudo yum -y install nginx php73 php-fpm php-cli php-mysqlnd php-pdo mariadb-server htop mlocate wget yum-utils yum-cron telnet net-tools setools-console policycoreutils-python unzip
因為是架在 GCP 免費方案上,所以設定個 swap。
$ sudo dd if=/dev/zero of=/swap bs=1024 count=1024k
$ sudo mkswap swap
$ sudo swapon swap
$ sudo chmod 0600 swap
$ sudo vim /etc/fstab
/swap swap swap defaults 0 0
開始設定服務
$sudo systemctl enable nginx php-fpm mariadb
$sudo systemctl start nginx php-fpm mariadb
設定mariadb
$mysql_secure_installation
除了自訂密碼輸入2次,一路Enter
建 DB、User
$mysql -uroot -p
> create database servermonitor character set utf8 collate utf8_general_ci;
> grant all on servermonitor.* to monitor@localhost identified by 'password';
> flush privileges;
> exit
設定 php.ini
$sudo vim /etc/php.ini
date.timezone = Asia/Taipei
output_buffering = Off
expose_php = off
short_open_tag = On
設定 php-fpm
$vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
取消以下註解
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
設定 nginx
$vim /etc/nginx/nginx.conf
server 是 80 443 底下都改
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
server_tokens off;
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_namwget e;
include fastcgi_params;
}
建立自簽憑證(也可以用 Let`s Encrypt)
$ sudo openssl req -new -x509 -days 3650 -nodes -out /etc/pki/nginx/server.crt -keyout /etc/pki/nginx/private/server.key
安裝php server monitor
$ cd /usr/share/nginx/html
$ sudo git clone https://github.com/phpservermon/phpservermon.git
$ sudo mv /phpservermon/* ./
$ sudo yum install php-xml
$ sudo php composer.phar install
$ sudo systemctl restart nginx php-fpm
打開 https:/server_ip/install.php 跳出如下,按「Let`s go」
下圖設定一直打不成功,所以直接手改 config.php
$vim config.php
<?php
define('PSM_DB_PREFIX', 'xxx_');
define('PSM_DB_USER', 'abc');
define('PSM_DB_PASS', '123456');
define('PSM_DB_NAME', 'xyz');
define('PSM_DB_HOST', 'localhost');
define('PSM_DB_PORT', '3306'); //3306 is the default port for MySQL. If no specfic port is used, leave it empty.
define('PSM_BASE_URL', 'https://ip_address');
輸入登入的帳號密碼
完成
設定排程每分鐘監測
$ crontab -e
*/1 * * * * /usr/bin/php /usr/share/nginx/html/cron/status.cron.php
有使用寄信服務,大致就是把 SMTP 填完就好,但要設定 selinux
$ sudo setsebool httpd_can_sendmail on
Telegram 部分因為建完才補教學,大致就是去申請 bot 再把拿到的API Token 填進來
最後欲監控的 Server 設定裡要設為 Yes 就會發通知信件及發 Telegram 通知