nginx爲俄羅斯人開發的一款開源www服務器軟件,具有高併發佔資源少的特點。
相對於apache由於採用了最新的epoll模型(apache採用的是select模型),更適合高併發場景。
1.編譯安裝:
首先安裝nginx必須的庫:pcre-devel(支持rewrite重寫)、openssl-devel(https服務)
#yum安裝,可能需要epel源,yum install -y epel-release #pcre需要gcc-c++,yum install -y gcc-c++ yum install -y pcre pcre-devel openssl openssl-devel #使用rpm -qa檢查是否安裝 rpm -qa pcre pcre-devel openssl openssl-devel pcre-devel-7.8-7.el6.x86_64 pcre-7.8-7.el6.x86_64 openssl-1.0.1e-42.el6.x86_64 openssl-devel-1.0.1e-42.el6.x86_64
源碼安裝nginx
mkdir -p /server/{tools,scripts} cd /server/tools 將nginx-1.6.3.tar.gz下載到此目錄 useradd -s /sbin/nologin nginx tar zxvf nginx-1.6.3.tar.gz ./configure \ --prefix=/application/nginx-1.6.3 \ --user=nginx \ --group=nginx \ --with-http_stub_status_module \ --with-http_ssl_module make &&make install ln -s /application/nginx-1.6.3 /application/nginx
檢查語法啓動nginx
/application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful #語法檢查成功後啓動 /application/nginx/sbin/nginx #使用lsof,netstat檢查 lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 11071 root 9u IPv4 30817 0t0 TCP *:http (LISTEN) nginx 11072 nginx 9u IPv4 30817 0t0 TCP *:http (LISTEN) netstat -lnptu|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11071/nginx #windows瀏覽器檢查,出現welcome to nginx! #curl命令檢查 curl -I localhost 200 OK
配置文件nginx.conf
#原nginx.conf包含大量註釋,先簡化配置文件 cd /application/nginx/conf grep -vE "^$|#" nginx.conf.default>nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
配置虛擬主機www,bbs,blog以及status狀態監控
#創建虛擬主機目錄,及index文件 cd /application/nginx/html mkdir www bbs blog echo "www.etiantian.org">www/index.html echo "bbs.etiantian.org">bbs/index.html echo "blog.etiantian.org">blog/index.html #獨立的虛擬主機配置文件 cd /application/nginx/conf vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #Virtualhost include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; include extra/status.conf } #虛擬主機配置文件 mkdir extra cd extra vim