19

安装nginx并安全地配置和启动
摘要:centos中的nginx安装教程,以低权限用户启动nginx,使用防火墙转发流量解决非root用户无法使用1024以下端口问题,nologin用户执行命令的方法。
文章目录见右侧

一、安装nginx#

参考文章<<

安装教程&命令

如果centos服务器是最小体量安装,则先安装weget

yum install -y wget

#下载nginx,截至写稿最新是1.18.0

cd /home

wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -zxvf nginx-1.18.0.tar.gz

cd nginx-1.18.0
#安装依赖

yum install -y pcre pcre-devel openssl openssl-devel gcc gcc gcc-c++ ncurses-devel perl
#可选安装步骤

#vim auto/cc/gcc

#在179行这样注释掉 CFLAGS="$CFLAGS -g" 使用:set nu显示行号

#支持ssl

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
#编译和安装

make
make install
#安装成功

#安装目录

cd /usr/local/nginx

二、配置教程#
安全地配置nginx,主要是以低权限用户运行nginx,这样即使出现漏洞,攻击者也难以反弹shell

2.1 创建用户和组#
groupadd nologin

useradd nginx-nologin -M -s /sbin/nologin -g nologin
#-M 不指定家目录

#-s 指定shell ,nologin代表该用户无法用于登录,但是可以指定以此用户执行命令,详情在后面

#-g 指定组

2.2 获取https证书#
这是可选的,如果你还没有域名,则需先购买域名,国内😉购买域名还需要备案。

如果你有域名,那么推荐使用https,证书可以免费申请,无需备案。

以阿里云为例,你使用域名申请后,推荐使用文件验证,因为dns验证的话需要等dns传播。验证通过一分钟左右就会审核通过,接着下载证书文件,要下载nginx的。下载后解压重命名为cert.pem和cert.key,传到服务器/usr/local/nginx/conf/目录下。
后续配置中取消掉有关ssl的配置的注释。

2.2 配置nginx.conf#
vi /usr/local/nginx/conf/nginx.conf
如果不想麻烦的话可以直接备份原文件,然后使用我下面的配置

#运行用户和组
user nginx-nologin nologin;
worker_processes 1;

#去掉注释
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#去掉注释
pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    #使用8800,非root用户无法使用1024以下端口,后续使用防火墙转发流量
    listen       8800;
    server_name  localhost;

    #charset koi8-r;

    ###强制http转https
    #return 301 https://blog.yunmuq.xyz:4433;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

###流量控制,没有https也可以用在http
#开辟10m内存存储相关信息,访问频率限制为一秒3次,访问频率超过会返回500状态码
#limit_req_zone $binary_remote_addr zone=mylimit:10m rate=3r/s;

# HTTPS server
#
#server {
#    listen      4433  ssl;
#    server_name  localhost;

    #流量控制
    #继承上面的配置,并且新增一个缓冲区能缓存2次请求
    #limit_req zone=mylimit burst=2;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#    error_page  404              /404.html;

### 497错误即400状态码中的,错误地使用http协议请求https端口
#    error_page  497              /400.html;

    # redirect server error pages to the static page /50x.html
    #
#    error_page   500 502 503 504  /50x.html;
#    location = /50x.html {
#        root   html;
#    }

#}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118

}

2.3 配置防火墙#
如果是最小安装,没有防火墙的话,还需要安装防火墙

可以运行命令检测是否安装

iptables
firewalld #centos7默认
如果两个都报错找不到命令的话需要安装,推荐firewalld

yum install -y firewalld

systemctl enable firewalld #开机启动

systemctl start firewalld #启动
接下来是防火墙配置

#开放端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --zone=public --add-port=443/tcp --permanent
#流量转发

firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8800 --permanent

firewall-cmd --add-forward-port=port=443:proto=tcp:toport=4433–permanent
#配置立即生效

firewall-cmd --reload

2.4 配置文件权限#
这是最后一步,推荐把这些命令存为sh文件,并设置权限755,因为改动文件时可能需要重复执行。

chown -R nginx-nologin:nologin /usr/local/nginx

chmod -R 755 /usr/local/nginx

三、启动nginx#
一切准备就绪,现在可以启动,推荐把启动命令保存为sh文件,并设置权限755

这里演示了nologin用户如何执行命令

首次启动:

#su即使用另一个用户执行命令,-s指定了shell,-c是命令内容

#nginx -c是检查配置是否正确

su -s /bin/bash -c “/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf” nginx-nologin

#-s reload用于运行时重载配置文件无需停止,也可以用于启动

su -s /bin/bash -c “/usr/local/nginx/sbin/nginx -s reload” nginx-nologin
如果要停止,可以使用

su -s /bin/bash -c “/usr/local/nginx/sbin/nginx -s quit” nginx-nologin

posted @ 2020-12-31 11:54  远道a  阅读(64)  评论(0编辑  收藏  举报