基于CentOS7的系统:
一、下载并上传包到linux上
1、使用wget
命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。
# 下载
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
# 解压
tar -zxvf nginx-1.12.0.tar.gz
# 重命名
mv nginx-1.12.0 nginx
2、可以从官网下载,然后使用rz命令上传压缩包
如果rz使用不了,执行命令yum install lrzsz 安装rzsz后,再执行rz
二、在 nginx.conf 中加上对于日志的json格式化
vim /usr/local/nginx/conf/nginx.conf
#在http下加上:
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":1,'
'"host":"$server_addr",'
'"client":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"domain":"$host",'
'"url":"$uri",'
'"status":"$status"}';
access_log /opt/elk-data/logs/access.log json;
三、测试配置是否可以正常运行,并解决相应问题
/usr/local/nginx/sbin/nginx -t
# 显示如下结果就是正常运行
Password:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
出现以下问题则对应解决:
1. nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
原因:当前用户对该位置没有写入权限
解决办法:
l 使用命令:sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 以root权限启动
l 使用命令:sudo chmod -R a+rw /usr/local/nginx 给所有用户赋权限(个人学习,不考虑安全问题)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 启动Nginx
注:以非root权限启动时,会出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 错误
原因:Linux只有root用户可以使用1024一下的端口
解决办法:
l 已root权限启动
l 将 /usr/local/nginx/conf/nginx.conf 文件中的80端口改为1024以上
server {
# listen 80
listen 8080
……
}
2. nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
在 /usr/local/nginx/logs/ 下创建一个error.log 的文件就可以了
cd /usr/local/nginx/logs/
mkdir error.log
3. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
参照这个:https://blog.csdn.net/ownfire/article/details/7966645
4. nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
解决方法:找到你的nginx.conf的文件夹目录,然后运行类似如下命令
nginx -c etc/nginx/nginx.conf
再运行nginx -s reload,就可以了。
5、yum install gcc gcc-c++ //执行命令后出现的问题,一直循环执行其中的三句,如下图所示:
解决办法就是先ctrl+z一下,接着输入rm –f /var/run/yum.pid
注意:一定要有网
6、unbuntu下安装安装pcre-8.37 configure: error: You need a C++ compiler for C++ support
解决办法链接是:http://www.ithao123.cncontent-9518048.html
7、安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)
参考这个链接: https://blog.csdn.net/qq_40673345/article/details/103695894
8、curl: (7) Failed connect to 192.168.194.6:80; Connection refused
[root@localhost ~]# curl 192.168.194.6:80
curl: (7) Failed connect to 192.168.194.6:80; Connection refused
检查 nginx.conf 的配置
server 的listen 改成0.0.0.0:80,用0.0.0.0来代替自己的ip
9、启动 nginx 后 access.log 收集不到日志
重启一下 nginx 就可以了
/usr/local/nginx/sbin/nginx -s reload