目录
一、准备工作
二、系统初始化设置
三、安装Nginx
1、安装依赖环境
2、编译安装
2.1、解压安装包
2.2、安装组件
2.3、make && make install
3、安装优化
4、 修改/usr/local/nginx/conf/nginx.conf的配置文件
四、验证
五、Nginx服务监控模块版一键安装shell脚本
一、准备工作
准备nginx-module-vts-master.zip、nginx-1.15.9.tar.gz源码包放到/opt目录下
二、系统初始化设置
#!/bin/bash
#################################关闭防火墙#######################################
systemctl stop firewalld.service &> /dev/null
if [ $? -eq 0 ];then
echo "防火墙已关闭"
else
echo "防火墙关闭失败,请重新操作"
break
fi
systemctl disable firewalld.service &> /dev/null
if [ $? -eq 0 ];then
echo "防火墙已设置为开机不启动"
else
echo "防火墙开机不启动设置失败,请重新开始"
break
fi
###################################关闭安全中心#####################################
setenforce 0 ####
echo "selinux已关闭"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
grep -w "SELINUX=disabled" /etc/selinux/config &> /dev/null
if [ $? -eq 0 ];then
echo "selinux已设置为永久关闭"
else
echo "selinux设置永久关闭失败"
break
fi
################################检测调试网卡网络状态##################################
ping -c1 -w1 -i0.2 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then
echo "网络正常"
else
sed -i 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/' /etc/sysconfig/network-scripts/ifcfg-ens33
read -p "请输入IP地址: " ip
read -p "请输入子网掩码: " mask
read -p "请输入网关地址: " way
read -p "请输入DNS: " dns
cat<<EOF>>/etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=$ip
NETMASK=$mask
GATEWAY=$way
DNS=$dns
EOF
systemctl restart network &>/dev/null
ping -c2 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then
echo "网络正常"
else
echo "网络设置失败"
fi
fi
################################# 修改主机名 ##########################################
read -p "修改主机名:需要输入1;不需要输入2: " xuanze
case $xuanze in
1)
read -p "输入新的主机名: " xin
hostnamectl set-hostname $xin
su
;;
2)
echo "nice"
;;
*)
echo "输入错误!"
esac
执行结果
防火墙已关闭
防火墙已设置为开机不启动
selinux已关闭
selinux已设置为永久关闭
网络正常
修改主机名:需要输入1;不需要输入2: 1
输入新的主机名: zwb
三、安装Nginx
1、安装依赖环境
yum -y install gcc gcc-c++ pcre-devel zlib-devel make pcre zlib openssl openssl-devel
2、编译安装
2.1、解压安装包
cd /opt ##进入压缩包位置
tar -xzvf nginx-1.15.9.tar.gz ##解压nginx-1.15.9压缩包
unzip nginx-module-vts-master.zip ## 解压压缩包
mv nginx-module-vts-master /usr/local/ ##复制nginx-module-vts-master到/usr/local/
2.2、安装组件
cd /opt/nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--add-module=/usr/local/nginx-module-vts-master
2.3、make && make install
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ ##建立软连接到/usr/local/sbin/方便执行
##时不需要以绝对路径方式执行
nginx -V ### 查看安装的模块情况
useradd -M -s /sbin/nologin nginx ###添加用户
nginx -t ##检查配置文件语法是否有问题
nginx ##启动
3、安装优化
配置nginx.service文件,并赋予权限
[root@zwb nginx-1.15.9]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service ##赋予权限
systemctl enable nginx --now ##立即启动并设置为开机启动
4、 修改/usr/local/nginx/conf/nginx.conf的配置文件
vim /usr/local/nginx/conf/nginx.conf
1、取消65~71行的注释符#号
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi_params;
2、75 行增加index.php
45 index index.html index.htm index.php;
3、第20行增加
vhost_traffic_status_zone;
log_format main '{ "@timestamp": "$time_local", '
'"@fields": { '
'"uri":"$request_uri",'
'"url":"$uri",'
'"upstream_addr":"$upstream_addr",'
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"host":"$host",'
'"server_addr":"$server_addr",'
'"request_time": "$request_time", '
'"request_time":"$request_time",'
'"status":"$status",'
'"request": "$request", '
'"request_method": "$request_method", '
'"size":$body_bytes_sent,'
'"upstream_time":"$upstream_response_time"'
'"http_referrer": "$http_referer", '
'"body_bytes_sent":"$body_bytes_sent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_user_agent": "$http_user_agent" } }';
4、在
location / {
root html;
index index.html index.htm;
} 下增加内容
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
systemctl restart nginx.service ###更改配置文件后需要重启服务
四、验证
登录查看参数(这个IP是我虚拟机的ens33的地址)还请自行查看自己的ip地址。
Server main
host:主机名
version:版本
version:存活时间
connections :连接数
①active :当前活动的客户端连接数
②reading:读取客户端连接的总数
③writing:写入客户端连接的总数
④waiting:正在等待的客户端连接总数
requests:请求的客户端连接总数
①accepted:接受的客户端连接总数
②handled:已处理的客户端连接总数
③total:总计客户端连接数
shared memory:共享内存
①name:指定的共享内存名
②maxsize:配置中指定的共享内存的最大大小限制
③usedsize:共享内存当前大小
④usednode:共享内存中当前使用的节点数
server zones
requests:从客户端接收的客户端请求总数
①total:总数
②req/s:单位时间数
③time:时间
responses:状态代码分别为1xx、2xx、3xx、4xx、5xx的响应数
cache:
①miss:未命中的缓存数
②bypass:绕过缓存旁路数
③expired:过期缓存数
④stale:失效缓存数
⑤updating:缓存更新次数
⑥revalidated:重新验证的缓存数
⑦hit:命中缓存数
⑧scarce:未达缓存要求的请求总数
⑨total:缓存总数
五、Nginx服务监控模块版一键安装shell脚本
#!/bin/bash
rm -f /var/run/yum.pid
##################系统初始化操作################
systemctl stop firewalld.service &> /dev/null
if [ $? -eq 0 ];then
echo "防火墙关闭成功"
else
echo "防火墙关闭失败"
exit
fi
systemctl disable firewalld.service &> /dev/null
if [ $? -eq 0 ];then
echo "防火墙关闭开机启动成功"
else
echo "防火墙关闭开机启动失败"
exit
fi
setenforce 0
if [ $? -eq 0 ];then
echo "selinux关闭成功"
else
echo "selinux关闭失败"
exit
fi
##安装依赖包
yum -y install gcc gcc-c++ pcre-devel zlib-devel make pcre zlib openssl openssl-devel
##编译安装
cd /opt
if [ -f nginx-1.15.9.tar.gz ];then
tar -xzvf nginx-1.15.9.tar.gz
else
echo "nginx-1.15.9.tar.gz不存在"
exit
fi
if [ -f nginx-module-vts-master.zip ];then
unzip nginx-module-vts-master.zip
mv nginx-module-vts-master /usr/local/
else
echo "nginx-module-vts-master不存在"
exit
fi
cd /opt/nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--add-module=/usr/local/nginx-module-vts-master \
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
id nginx &> /dev/null
if [ $? -eq 0 ];then
echo "nginx用户已存在"
else
useradd -M -s /sbin/nologin nginx
fi
cat<<EOF>/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chmod 754 /lib/systemd/system/nginx.service
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
cat<<EOF>/usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
vhost_traffic_status_zone;
log_format main '{ "@timestamp": "$time_local", '
'"@fields": { '
'"uri":"$request_uri",'
'"url":"$uri",'
'"upstream_addr":"$upstream_addr",'
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"host":"$host",'
'"server_addr":"$server_addr",'
'"request_time": "$request_time", '
'"request_time":"$request_time",'
'"status":"$status",'
'"request": "$request", '
'"request_method": "$request_method", '
'"size":$body_bytes_sent,'
'"upstream_time":"$upstream_response_time"'
'"http_referrer": "$http_referer", '
'"body_bytes_sent":"$body_bytes_sent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_user_agent": "$http_user_agent" } }';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
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;
}
}
}
EOF
systemctl restart nginx &> /dev/null
if [ $? -eq 0 ];then
echo "nginx重启成功"
else
echo "nginx重启失败"
exit
fi