【基础服务-Linux开发平台】Nginx的安装和配置

简介

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

方法 / 步骤

一:下载并解压

wget -c http://nginx.org/download/nginx-1.20.2.tar.gz

二:安装并启动

2.1 安装相关依赖

  • 一键安装依赖插件
  • 编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装 ( gcc-c++ )
  • zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ( zlib zlib-devel)
  • nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
 yum -y install  make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

在这里插入图片描述

2.2 到安装包的目录进行配置检测

./configure

2.3 安装并启动

make && make install

在这里插入图片描述

  • 启动

安装完毕后进入 默认文件夹 /usr/local/nginx/sbin
进行启动

cd /usr/local/nginx/sbin
# 启动
./nginx
  • 启动成功
    在这里插入图片描述

2.4 开机启动

# 添加并编辑 /etc/rc.d/init.d/nginx 文件
vi /etc/rc.d/init.d/nginx
  • 启动脚本内容
    注意:内容中的路径(系统默认安装路径) 如果自定义路径请及时修改:
#!/bin/sh
#
#nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse 
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

# 设置文件的执行权限
chmod a+x /etc/init.d/nginx

# 将nginx服务加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx

# 系统重新刷新/加载一下
systemctl daemon-reload

# 测试 service 系统服务命令
service nginx start | stop | restart | status

# 设置开机启动
chkconfig nginx on

在这里插入图片描述

三:日志定时整理设置

实际应用: shell+定时任务+nginx信号管理,完成日志按日期存储
分析思路:
凌晨00:00:01,把昨天的日志重命名,放在相应的目录下
再重新生成新的日志文件

vi /usr/local/nginx/logjob.sh
#!/bin/bash
base_path='/usr/local/nginx/logs'
#日志保存的路径
log_path=$(date -d yesterday +"%Y%m")
 
day=$(date -d yesterday +"%d")
 
mkdir -p $base_path/$log_path
 
mv $base_path/access.log $base_path/$log_path/access_$day.log
 
#echo $base_path/$log_path/access_$day.log
 
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
  • 设置执行权限
chmod a+x /usr/local/nginx/logjob.sh
  • 编辑计划任务表 crontab
crontab -e
# 增加内容 (每天0时1分(建议在02-04点之间,系统负载小))
01 00 * * * /usr/local/nginx/logjob.sh

日志分割后效果

在这里插入图片描述

参考资料 & 致谢

[1] Linux Crontab 定时任务

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OxYGC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值