lnmp部署
目录
lnmp部署
nginx编译安装
mariadb安装
##安装mariadb
[root@vm3 ~]# yum -y install mariadb mariadb-server
##修改mariadb密码
[root@vm3 ~]# mysql
MariaDB [(none)]> set password=password("123456");
Query OK, 0 rows affected (0.002 sec)
php安装
[root@vm3 ~]# yum list all | grep php-*
php.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 @AppStream
php-bcmath.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 @AppStream
php-cli.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 @AppStream
php-common.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 @AppStream
php-dba.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 @AppStream
................................................
[root@vm3 ~]# yum -y install php-* php
lnmp配置
php配置
- /etc/php-fpm.d/www.conf
[root@vm3 ~]# vim /etc/php-fpm.d/www.conf
.................................................
user = nginx
group = nginx
listen = 127.0.0.1:9000
.................................................
- 创建php文件
[root@vm3 ~]# cd /usr/local/nginx/html/
[root@vm3 html]# ls
50x.html index.html index.php mysql.php
[root@vm3 html]# cat index.php
<?php
phpinfo();
?>
[root@vm3 html]# cat mysql.php
<?php
$link=mysqli_connect("127.0.0.1","root","123456");
if($link) echo "数据库连接成功!";
mysqli_close($link);
?>
nginx配置
[root@vm3 ~]# cd /usr/local/nginx/conf/
[root@vm3 conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@vm3 conf]# vim nginx.conf
...............................
http{
........................
server{
listen 80;
server_name localhost;
..............................
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
测试
开启服务
[root@vm3 ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@vm3 ~]# systemctl enable --now php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@vm3 ~]# nginx -s reload -c /usr/local/nginx/conf/nginx.conf
[root@vm3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
测试
- 统计logs/access.log日志文件访问最多的ip
[root@vm3 ~]# awk '{print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr -k1 | head -n 3
27 192.168.225.128
15 192.168.225.130
10 192.168.225.127
[root@vm3 ~]# awk '{print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr -k1 | head -n 4
27 192.168.225.128
15 192.168.225.130
10 192.168.225.127
10 192.168.225.1
[root@vm3 ~]# awk '{print $1}' /usr/local/nginx/logs/access.log | sort | uniq -c | sort -nr -k1 | head -n 1
27 192.168.225.128
zabbix监控nginx状态页面
zabbix安装部署
-
详情点击——>zabbix部署
-
关键配置
[root@vm3 ~]# vim /usr/local/nginx/conf/nginx.conf
......................................
location ~ \.php$ {
root html/zabbix;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
........................................
脚本文件
- 监控文件
[root@vm3 script]# pwd
/script
[root@vm3 script]# ll -d
drwxr-xr-x 2 zabbix zabbix 35 Dec 21 20:27 .
[root@vm3 script]# ll
total 4
-rwxr-xr-x 1 zabbix zabbix 796 Dec 21 18:44 nginx_status_check.sh
[root@vm3 script]# cat nginx_status_check.sh
#!/bin/bash
auth="-u admin:123456"
case $1 in
active)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==1{print $3}'
;;
accepts)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==3{print $1}'
;;
handled)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==3{print $2}'
;;
requests)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==3{print $3}'
;;
reading)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==4{print $2}'
;;
writing)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==4{print $4}'
;;
waiting)
curl -s $auth http://192.168.225.130/status | awk -F" " 'NR==4{print $6}'
;;
*)
echo "Usage:$0 active|accepts|handled|requests|reading|writing|waiting"
;;
esac
- 邮件发送脚本
[root@vm3 script]# cd /usr/local/share/zabbix/alertscripts/
[root@vm3 alertscripts]# ll -d
drwxr-xr-x 2 zabbix zabbix 25 Dec 21 19:43 .
[root@vm3 alertscripts]# ll
total 4
-rwxr-xr-x 1 zabbix zabbix 139 Dec 21 19:43 sendmail.sh
[root@vm3 alertscripts]# cat sendmail.sh
#!/bin/bash
messages=$(echo $3 | tr '\r\n' '\n')
subject=$(echo $2 | tr '\r\n' '\n')
echo "${messages}" | /usr/bin/mail -s "${subject}" $1
zabbix监控nginx配置
[root@vm3 ~]# vim /usr/local/etc/zabbix_agentd.conf
.......................................
UnsafeUserParameters=1
UserParameter=nginx_check_status[*],/bin/bash /script/nginx_status_check.sh $1
..........................................
手动测试zabbix-get
[root@vm3 alertscripts]# zabbix_get -s 127.0.0.1 -p 10050 -k nginx_check_status[writing]
1