Nginx 在CentOS下的编译安装
一、获取相关开源程序
利用CentOS Linux系统自带的yum命令安装、升级所需的程序库:
LANG=C
yum -y install gcc gcc-c++ autoconf freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers patch libtool automake telnet cmake bison bind-utils jwhois setuptool ntsysv
二、安装Nginx所需的pcre库
下载地址:http://www.pcre.org/、https://mirrors.aliyun.com/exim/pcre/
wget "https://mirrors.aliyun.com/exim/pcre/pcre2-10.37.tar.gz"
tar -zxf pcre2-10.37.tar.gz
cd pcre2-10.37
三、安装Nginx
1、编译安装Nginx
下载地址:https://nginx.org/en/download.html
wget "https://nginx.org/download/nginx-1.28.0.tar.gz"
tar -zxf nginx-1.28.0.tar.gz
cd nginx-1.28.0
./configure --user=www --group=www --prefix=/usr/local/nginx --with-stream --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_realip_module --with-http_v2_module --with-threads --with-debug --with-http_addition_module --with-http_gzip_static_module --with-pcre2=../pcre2-10.37 --http-client-body-temp-path=/var/cache/nginx/client_body --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp
gmake && gmake install
2、创建Nginx日志目录
mkdir -p /var/log/nginx
mkdir -p /var/cache/nginx
chown -R www:www /var/log/nginx
chown -R www:www /var/cache/nginx
3、创建Nginx配置文件
在/usr/local/nginx/conf/目录中创建nginx.conf文件
rm -f /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
输入以下内容:Nginx配置文件
4、启动Nginx
ln -s /usr/local/lib/libpcre.so.1 /usr/lib64/libpcre.so.1
/usr/local/nginx/sbin/nginx
三、配置开机自动启动Nginx
开机启动Nginx
vim /etc/systemd/system/nginx.service
在文件中增加以下内容
[Unit]
Description=Nginx
After=network.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.target
修改完nginx.service文件后,需要重新加载systemd的配置,以便它能够识别新的或修改过的服务文件:systemctl daemon-reload
启动Nginx
systemctl start nginx.service
在开机时启用
systemctl enable nginx.service
四、日志切割脚本
1、shell脚本/var/www/scripts/cut_nginx_log.sh
#!/bin/bash
# The Nginx logs path
src_path="/var/log/nginx/"
dst_path="/var/log/nginx/"
files=`ls ${src_path} | grep ".log"`
mkdir -p ${dst_path}$(date -d "-1 day" +"%Y")/$(date -d "-1 day" +"%m")/
for i in $files
do
if [ -f ${src_path}${i} ]
then
is=`echo $i | sed 's/\.log$//g'`
mv ${src_path}${i} ${dst_path}$(date -d "-1 day" +"%Y")/$(date -d "-1 day" +"%m")/${is}-$(date -d "-1 day" +"%Y%m%d").log
fi
done
# 删除两个月前的数据
rm -rf ${dst_path}$(date -d "-3 month" +"%Y")/$(date -d "-3 month" +"%m")
kill -USR1 `cat /usr/local/nginx/nginx.pid`
2、添加到Linux定时任务
# 定时切割Nginx日志
0 0 * * * /var/www/scripts/cut_nginx_log.sh > /dev/null 2>&1
附:Nginx错误日志级别说明
error_log file [debug|info|notice|warn|error|crit]|[{debug_core|debug_alloc|debug_mutex|debug_event|debug_http|debug_mail|debug_mysql}]
日志级别 = 错误日志级别 | 调试日志级别; 或者
日志级别 = 错误日志级别
错误日志的级别: emerg, alert, crit, error, warn, notic, info, debug,
调试日志的级别: debug_core, debug_alloc, debug_mutex, debug_event, debug_http, debug_mail, debug_mysql
error_log 指令的日志级别配置分为 错误日志级别和调试日志级别且错误日志只能设置一个级别且错误日志必须书写在调试日志级别的前面且调试日志可以设置多个级别