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

#準備工作#
在安裝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并進行編譯安裝:
# 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的版本信息,并且檢驗上一步驟是否成功:
# 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)路徑(這里是為了后面運維管理方便,可以把不同的Web項目放到該目錄下):
# 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
這個時候打開瀏覽器訪問地址http://youripaddress應該可以看到:
至此,Nginx已經(jī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)閉進程;TERM則表示立刻關(guān)閉進程;-9表示強制關(guān)閉。
為了以后管理上的方便, 我們這里寫個啟動腳本,以后就可以用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è)置不允許目錄瀏覽,默認配置即為不允許。
autoindex off
2、開啟訪問日志,nginx中默認已開啟,這里我們后續(xù)為了運維管理上的方便最好把日志單獨放到/data目錄下。
access_log /data/www/logs/localhost.access.log
3、確保目錄的安全,由于Nginx使用的是www用戶啟動,黑客入侵服務器成功后將獲得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è)務,配置如下:
location ~* ^/data/www/logs/.*\.(php|php5)$ {
deny all;
}
注:當然最安全的還是給PHP的可執(zhí)行目錄采用白名單的方式,這個我們在PHP的安裝一節(jié)中再詳細介紹。
2)對于非使用PHP的業(yè)務(如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指的就是當前運維客戶端的IP地址段。
在允許IP的機器上輸入地址應該可以看到:
而不允許的用戶訪問應該是不可以的,會顯示403錯誤,比如:
5、把Nginx默認的首頁等頁面刪除,使用業(yè)務自己的首頁來頂替。
6、不允許IP直接訪問服務器,這樣的好處是怕當IP地址泄漏出去之后,被人用別的域名來指向了這個IP地址,可以設(shè)置讓其返回500等錯誤碼。比如:
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及安全配置
標題URL:http://m.fisionsoft.com.cn/article/dpdchos.html


咨詢
建站咨詢
