安裝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 -j4  # 根據cpu最大線程數決定,減少編譯時間
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 指定組

覺得麻煩直接用系統自帶的 nobody 用戶,本文后續使用nobody用戶



2.2 獲取https證書

這是可選的,如果你還沒有域名,則需先購買域名,國內😉購買域名還需要備案。

如果你有域名,那么推薦使用https,證書可以免費申請,無需備案。

以阿里雲為例,你使用域名申請后,推薦使用文件驗證,因為dns驗證的話需要等dns傳播。驗證通過一分鍾左右就會審核通過,接着下載證書文件,要下載nginx的。下載后解壓重命名為cert.pemcert.key,傳到服務器/usr/local/nginx/conf/目錄下。
后續配置中取消掉有關ssl的配置的注釋。



2.2 配置nginx.conf

vi /usr/local/nginx/conf/nginx.conf

如果不想麻煩的話可以直接備份原文件,然后使用我下面的配置

#運行用戶和組
#user  nginx-nologin nologin;
user nobody;
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;
    #    }

    #}

}



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 nobody:nobody /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" nobody

#-s reload用於運行時重載配置文件無需停止,也可以用於啟動

su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -s reload" nobody

如果要停止,可以使用

su -s /bin/bash -c "/usr/local/nginx/sbin/nginx -s quit" nobody




end

往期精彩文章推薦:

《動態svg圖片簡單制作》

《使用博客園的第一件事 - 自定義主題》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM