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