[工具] Shell LNMP 没运行则重启系统 (systemctl 版)

介绍:

作者:朱明宇
名称:LNMP 没运行则重启系统
作用:LNMP 没运行则重启系统

使用方法:
1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本:

#!/bin/bash
  
systemctl status nginx | grep 'active (running)'
if [ $? -ne 0 ];then
        /usr/sbin/reboot
fi

systemctl status mariadb | grep 'active (running)'
if [ $? -ne 0 ];then
        /usr/sbin/reboot
fi

systemctl status php-fpm | grep 'active (running)'
if [ $? -ne 0 ];then
        /usr/sbin/reboot
fi

[命令] Linux 命令 curl (访问并测试网页速度)

案例一:

# curl -o /dev/null -s -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} eternalcenter.com

(补充:这里以访问并测试 eternalcenter.com ,并显示测试过程为例)

案例二:

# curl -o /dev/null -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} https://api.weixin.qq.com/sns/user

(补充:这里以访问并测试 eternalcenter.com ,并不显示测试过程为例)

[步骤] Linux Nginx 源码安装包的管理 (通过 systemd 实现)

注意:在通过 systemd 管理源码安装的软件或自制 rpm 包安装的软件(以 Nginx 为例)之前,先要源码安装 Nginx 或者自制 rpm 包安装 Nginx

正文:

步骤目录:

步骤一:创建 Nginx 的 systemd 文件

步骤二:导入新创建的 Nginx systemd 配置文件

步骤三:使用 systemd 管理 Ngixn
3.1 使用 systemd 启动 Nginx
3.2 使用 systemd 开机自启 Nginx
3.3 使用 systemd 查看 Nginx 的状态

具体的操作步骤:

步骤一:创建 Nginx 的 systemd 文件

# vim /etc/systemd/system/nginx.service 

创建以下内容:

[Unit]

Description=nginx server daemon

Documentation=man:nginx(8)

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

步骤二:导入新创建的 Nginx systemd 配置文件

# systemctl daemon-reload

步骤三:使用 systemd 管理 Ngixn
3.1 使用 systemd 启动 Nginx

# systemctl start nginx.service

3.2 使用 systemd 开机自启 Nginx

# systemctl enable nginx.service

3.3 使用 systemd 查看 Nginx 的状态

# systemctl status nginx.service

[步骤] Nginx 日志的切割

注意:

在设置 Nginx 自动化日志切割并保存之前要先安装 Nginx

正文:

内容一:切割 Nginx 日志的原理

# mv access.log access2.log
# kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

内容二:切割 Nginx 日志的 Shell 脚本

# mkdir -p /usr/local/nginx/
# vim /usr/local/nginx/nginxlog.sh

创建以下内容:

#!/bin/bash
date=$(date +%Y%m%d)
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log   $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)

内容三:设置 Nginx 日志切割的自动化任务

# crontab -e
03 03 * * 5 /usr/local/nginx/nginxlog.sh

(补充:这里以每周五的 3 点 3 分执行 /usr/local/nginx/nginxlog.sh 命令为例)

[实验] Nginx 模块的设置 (监控模块)

软件准备:

在 Nginx 官网上下载搭建集群所需软件 Nginx:

http://nginx.org/en/download.html

正文:

步骤一:系统环境要求

1) 所有服务器的系统都需要是 CentOS 7 版本
2) 所有服务器系统都需要有 yum 源

步骤二:安装带有状态信息监控模块的 Nginx

# yum -y install gcc pcre-devel openssl-devel
# tar -xvf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# ./configure \
>--with-http_stub_status_module
# make && make install

(补充:这里以安装 nginx-1.16.1 为例)

步骤三:修改 Nginx 的配置文件

# vi /usr/local/nginx/conf/nginx.conf

将部分内容修改如下:

......
location /status {
stub_status on;
#allow IP address;
#deny IP address;
}
......

步骤四:启动 Nginx

# /usr/local/nginx/bin/nginx

步骤五:查看监控模块

通过浏览器访问以下网址:

http://127.0.0.1/status