日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
CentOS6.5安裝Nginx-1.6.2及安全配置

注:以下所有操作均在centos 6.5 x86_64位系統(tǒng)下完成。

#準(zhǔn)備工作#

在安裝Nginx之前,請確保已經(jīng)使用yum安裝了pcre等基礎(chǔ)組件,具體見《CentOS安裝LNMP環(huán)境的基礎(chǔ)組件》。

#############################################

CentOS安裝LNMP環(huán)境的基礎(chǔ)組件

在安裝LNMP環(huán)境之前,請確保已經(jīng)使用yum安裝了以下各類基礎(chǔ)組件(如果系統(tǒng)已自帶,還可以考慮yum update下基礎(chǔ)組件):

  • gcc
  • cmake
  • openssl+openssl-devel
  • pcre+pcre-devel
  • bzip2+bzip2-devel
  • libcurl+curl+curl-devel
  • libjpeg+libjpeg-devel
  • libpng+libpng-devel
  • freetype+freetype-devel
  • php-mcrypt+libmcrypt+libmcrypt-devel
  • libxslt+libxslt-devel
  • gmp+gmp-devel
  • libxml2+libxml2-devel
  • mhash
  • ncurses+ncurses-devel
  • xml2

#############################################

然后創(chuàng)建www的用戶組和用戶,并且不允許登錄權(quán)限:

# id www
id: www:無此用戶
# groupadd www
# useradd -g www -s /sbin/nologin www
# id www
uid=501(www) gid=501(www) 組=501(www) 

#Nginx的安裝#

開始下載Nginx并進(jìn)行編譯安裝:

# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.6.2.tar.gz
# tar zxf nginx-1.6.2.tar.gz
# cd nginx-1.6.2
# ./configure --prefix=/usr/local/nginx-1.6.2 --group=www --user=www --with-http_ssl_module --with-pcre --with-http_stub_status_module --with-http_gzip_static_module

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx-1.6.2"
  nginx binary file: "/usr/local/nginx-1.6.2/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx-1.6.2/conf"
  nginx configuration file: "/usr/local/nginx-1.6.2/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.6.2/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-1.6.2/logs/error.log"
  nginx http access log file: "/usr/local/nginx-1.6.2/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

# make && make install
# ln -s /usr/local/nginx-1.6.2/ /usr/local/nginx
# chown -R www:www /usr/local/nginx
# chown -R www:www /usr/local/nginx-1.6.2

把Nginx的sbin目錄加入PATH:

# vim /etc/profile

export PATH=$PATH:/usr/local/mysql/bin:$JAVA_HOME/bin:/usr/local/nginx/sbin

# source /etc/profile

查看Nginx的版本信息,并且檢驗(yàn)上一步驟是否成功:

# nginx -V
nginx version: nginx/1.6.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.6.2 --group=www --user=www --with-http_ssl_module --with-pcre --with-http_stub_status_module

至此,Nginx已經(jīng)安裝完畢。

#Nginx的啟動/重啟/關(guān)閉#

給Nginx的webapp配置相關(guān)路徑(這里是為了后面運(yùn)維管理方便,可以把不同的Web項(xiàng)目放到該目錄下):

# mkdir -p /data/www

簡單修改下配置文件:

# vim /usr/local/nginx/conf/nginx.conf

user  www;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

開始啟動Nginx:

# nginx

這個(gè)時(shí)候打開瀏覽器訪問地址http://youripaddress應(yīng)該可以看到:

至此,Nginx已經(jīng)啟動成功。

一般來說,當(dāng)修改了nginx.conf配置文件后,可以直接重啟讓配置生效,重啟之前一般檢測下配置文件是否正確:

# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
# nginx -s reload

另外,重啟也可以通過發(fā)信號的方式:

# kill -HUP ${master_pid}

關(guān)閉的命令如下:

# nginx -s quit
# nginx -s stop

注:quit表示等請求結(jié)束后再關(guān)閉,stop表示立刻關(guān)閉。

也可以通過發(fā)信號的方式來關(guān)閉: 

# kill -QUIT ${nginx_master}
# kill -TERM ${nginx_master}
# kill -9 ${nginx_master}

注:-QUIT表示從容停止,等所有請求結(jié)束后再關(guān)閉進(jìn)程;TERM則表示立刻關(guān)閉進(jìn)程;-9表示強(qiáng)制關(guān)閉。

為了以后管理上的方便, 我們這里寫個(gè)啟動腳本,以后就可以用service命令來啟動,如下:

# vim /etc/init.d/nginxd

#!/bin/sh
# chkconfig: 2345 85 15
# description:Nginx Server

NGINX_HOME=/usr/local/nginx-1.6.2
NGINX_SBIN=$NGINX_HOME/sbin/nginx
NGINX_CONF=$NGINX_HOME/conf/nginx.conf
NGINX_PID=$NGINX_HOME/logs/nginx.pid

NGINX_NAME="Nginx"

. /etc/rc.d/init.d/functions

if [ ! -f $NGINX_SBIN ]
then
    echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "
    exit
fi

start() {
    $NGINX_SBIN -c $NGINX_CONF
    ret=$?
    if [ $ret -eq 0 ]; then
        action $"Starting $NGINX_NAME: " /bin/true
    else
        action $"Starting $NGINX_NAME: " /bin/false
    fi
}

stop() {
    kill `cat $NGINX_PID`
    ret=$?
    if [ $ret -eq 0 ]; then
        action $"Stopping $NGINX_NAME: " /bin/true
    else
        action $"Stopping $NGINX_NAME: " /bin/false
    fi
}

restart() {
    stop
    start
}

check() {
    $NGINX_SBIN -c $NGINX_CONF -t
}


reload() {
    kill -HUP `cat $NGINX_PID` && echo "reload success!"
}

relog() {
    kill -USR1 `cat $NGINX_PID` && echo "relog success!"
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    check|chk)
        check
        ;;
    status)
        status -p $NGINX_PID
        ;;
    reload)
        reload
        ;;
    relog)
        relog
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"
        exit 1
esac

# chmod +x /etc/init.d/nginxd
# chkconfig nginxd on

這樣子就可以通過service來啟動:

# service nginxd start

#Nginx的安全配置#

1、 首先設(shè)置不允許目錄瀏覽,默認(rèn)配置即為不允許。

autoindex off

2、開啟訪問日志,nginx中默認(rèn)已開啟,這里我們后續(xù)為了運(yùn)維管理上的方便最好把日志單獨(dú)放到/data目錄下。

access_log /data/www/logs/localhost.access.log

3、確保目錄的安全,由于Nginx使用的是www用戶啟動,黑客入侵服務(wù)器成功后將獲得www用戶的權(quán)限,所以需要確保網(wǎng)站W(wǎng)eb目錄和文件的屬主與啟動用戶不同,防止網(wǎng)站被黑客惡意篡改和刪除。網(wǎng)站W(wǎng)eb目錄和文件的屬主可以設(shè)置為root,其中Web目錄權(quán)限統(tǒng)一設(shè)置為755,Web文件權(quán)限統(tǒng)一設(shè)置為644。只有上傳目錄等可讀寫權(quán)限的目錄可以被設(shè)置為777,為了防止黑客上傳木馬到777權(quán)限目錄中,還必須保證該777權(quán)限的目錄沒有執(zhí)行腳本的權(quán)限。這里有兩種情況處理:

1)對于使用PHP的業(yè)務(wù),配置如下:

location ~* ^/data/www/logs/.*\.(php|php5)$ {
    deny all;
}

注:當(dāng)然最安全的還是給PHP的可執(zhí)行目錄采用白名單的方式,這個(gè)我們在PHP的安裝一節(jié)中再詳細(xì)介紹。

2)對于非使用PHP的業(yè)務(wù)(如python、cgi等),則需要禁止外部訪問777目錄,配置如下:

location ~ ^/data/www/logs/ {
    deny all;
}

4、對于管理目錄,需要限制訪問的IP地址,比如這里限制訪問nginx狀態(tài)的:

server {
    location /nginx-admin {
        stub_status on;
        access_log logs/nginx-admin.log;
        allow 11.12.23.0/24;
        deny all;
    }

    location /admin {
        ...
    }
}

注:上面配置的11.12.23.0/24指的就是當(dāng)前運(yùn)維客戶端的IP地址段。

在允許IP的機(jī)器上輸入地址應(yīng)該可以看到: 

而不允許的用戶訪問應(yīng)該是不可以的,會顯示403錯(cuò)誤,比如:

 

5、把Nginx默認(rèn)的首頁等頁面刪除,使用業(yè)務(wù)自己的首頁來頂替。

6、不允許IP直接訪問服務(wù)器,這樣的好處是怕當(dāng)IP地址泄漏出去之后,被人用別的域名來指向了這個(gè)IP地址,可以設(shè)置讓其返回500等錯(cuò)誤碼。比如:

server {
    listen        80 default;
    return 500;
}
server {
    listen        80;
    server_name   www.linuxidc.com tencent.com;
    root          /data/www/linuxidc;

    access_log    /data/logs/nginx/linuxidc.access.log;
    error_log     /data/logs/nginx/linuxidc.error.log;
}

分享名稱:CentOS6.5安裝Nginx-1.6.2及安全配置
標(biāo)題來源:http://m.5511xx.com/article/dpdchos.html