Linux——yum安装nginx 1.17+

参考:https://nginx.org/en/linux_packages.html
1、准备yum

sudo yum -y install yum-utils

2、配置nginx repos

vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

3、完成nginx-mainline配置

sudo yum-config-manager --enable nginx-mainline

4、安装nginx

sudo yum install nginx

5、相关目录
主要目录

nginx 日志文件 /var/log/nginx
nginx配置文件目录 /etc/nginx
nginx 可执行文件 /usr/sbin/nginx
nginx环境配置 /etc/sysconfig/nginx
nginx默认站点目录  /usr/share/nginx/html

其他目录

/etc/logrotate.d/nginx
/var/cache/nginx
/usr/lib64/nginx
/usr/libexec/initscripts/legacy-actions/nginx

6、常用命令
#测试配置文件正确性

nginx -t

#信号命令

nginx -s [signal]
nginx -s reload 刷新配置
nginx -s fast 快速stop
nginx -s graceful 优雅stop

#启停服务

service nginx status | stop | start

7、设置开机启动
vim /etc/init.d/nginx


#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
 
nginxd=/usr/sbin/nginx    
nginx_config=/etc/nginx/nginx.conf 
nginx_pid=/var/run/nginx.pid    
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
    if [ -e $nginx_pid ];then
       echo "nginx already running...."
   exit 1
    fi
       echo -n $"Starting $prog: "
       daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
       [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
    case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
    reload
    ;;
restart)
    stop
    start
    ;;
status)
    status $prog
    RETVAL=$?
    ;;
*)
    echo $"Usage: $prog {start|stop|restart|reload|status|help}"
    exit 1
esac
exit $RETVAL

赋权

chmod 755 /etc/init.d/nginx  

加入开机启动

chkconfig --add nginx
chkconfig nginx on

跨域设置

add_header Access-Control-Allow-Origin *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

8、日志统计

1).根据访问IP统计UV
awk '{print $1}'  access.log|sort | uniq -c |wc -l

2).统计访问URL统计PV
awk '{print $7}' access.log|wc -l

3).查询访问最频繁的URL
more access.log | awk '{print $7}' |sort | uniq -c |sort -n -k 1 -r|more
4).查询访问最频繁的IP
more  access.log | awk '{print $1}'|sort | uniq -c |sort -n -k 1 -r|more

5).根据时间段统计查看日志
 cat  access.log| sed -n '/14\/Mar\/2018:21/,/14\/Mar\/2018:22/p'|more
统计时间段内,访问最频繁的URL
cat  access.log| sed -n '/28\/May\/2020:14/,/28\/May\/2020:22/p'| awk '{print $7}' |sort | uniq -c |sort -n -k 1 -r|more
统计时间段内,访问最频繁的IP
cat  access.log| sed -n '/28\/May\/2020:14/,/28\/May\/2020:22/p'| awk '{print $1}' |sort | uniq -c |sort -n -k 1 -r|more

你可能感兴趣的:(Linux)