一、Nginx
--优点:高性能web服务器,反响代理服务器(http server db server 邮件服务器……),缓存服务器。支持更多的并发链接(静态小文见),占用资源更少。
--支持kqueue,epoll等网络IO时间模型,由此来支持高并发。
--主要应用场合
---------使用Nginx结合FastCUI运行PHP、JSP、Perl程序。
---------使用Nginx作为反响代理,负载均衡,规则过滤。
---------使用Nginx运行静态HTML页,小图片。
---------使用Nginx加cache插件实现对web服务器缓存功能。
---Nginx使用最新的eqoll和kqueue网络IO模型
---apache使用传统的select模型
--1.安装nginx http://nginx.org/en/download.html (先要安装pcre库)
[root@moshan lnmp]# rpm -qa|grep pcre
pcre-7.8-6.el6.i686
[root@moshan lnmp]#
[root@moshan lnmp]# tar zxf nginx-1.8.0.tar.gz
[root@moshan lnmp]# useradd nginx -s /sbin/nologin -M
[root@moshan lnmp]# cd nginx-1.8.0
[root@moshan nginx-1.8.0]# ./configure \
--user=nginx \
--group=nginx \
--with-pcre=/usr/local/pcre
--prefix=/usr/local/nginx-1.8.0 \
--with-http_stub-status_module \ #加载状态模块
--with-http_ssl_module #激活ssl模块
--configure通过了,但是make的时候报如下错误
make -f objs/Makefile make[1]: Entering directory
`/root/Downloads/lnmp/nginx-1.8.0' cd /usr/local/pcre \ && if [ -f
Makefile ]; then make distclean; fi \ && CC="cc" CFLAGS="-O2
-fomit-frame-pointer -pipe " \ ./configure --disable-shared /bin/sh: line
--通过查找configure帮助分析
[root@moshan nginx-1.8.0]# ./configure --help | grep '\--with-pcre'
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation
support
[root@moshan nginx-1.8.0]#
--可以看到pcre的路径是源码包所在的路经,不是安装路径,所以将pcre的路径改成源码包的路径再重新configure就ok了
---------------with-pcre=/root/Downloads/lamp/apach/pcre-8.10
[root@moshan nginx-1.8.0]# make && make install
---安装完成
-----------------------------------------------------------------------------
---跟apache的安装一样做一个软链接
[root@moshan nginx-1.8.0]# ln -s /application/nginx-1.8.0/ /application/nginx
[root@moshan nginx-1.8.0]# /application/nginx/sbin/nginx -t
--如果此处报一个libpcre.so.1找不到的错误
/application/nginx/sbin/nginx:error while loading shared
libraries:libpcre.so.1:cannot shared object file: Nosuch file or directory
------原因分析----pcre版本太新--解决如下
[root@moshan pcre-8.10]# find / -name libpcre.so*
/usr/local/pcre/lib/libpcre.so.0.0.1
/usr/local/pcre/lib/libpcre.so
/usr/local/pcre/lib/libpcre.so.0
/lib/libpcre.so.0.0.1
/lib/libpcre.so.0
将找到的/usr/local/pcre/lib 添加到 /etc/ld.so.conf文件下
[root@moshan pcre-8.10]# ldconfig -----使配置生效
--安装工作完毕
--2.启动nginx
[root@moshan nginx-1.8.0]# /application/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
-------我们发现启动失败---通过lsof反查80端口我们发现httpd正在监听80端口---所以我们可以先关闭apache服务--再重启nginx
[root@moshan nginx-1.8.0]# lsof -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1877 root 4u IPv6 12771 0t0 TCP *:http (LISTEN)
clock-app 3966 root 22u IPv4 2501259 0t0 TCP
blog.moshan.com:34569->124.40.53.10:http (CLOSE_WAIT)
httpd 19635 moshan 4u IPv6 12771 0t0 TCP *:http (LISTEN)
httpd 19636 moshan 4u IPv6 12771 0t0 TCP *:http (LISTEN)
httpd 19637 moshan 4u IPv6 12771 0t0 TCP *:http (LISTEN)
httpd 19744 moshan 4u IPv6 12771 0t0 TCP *:http (LISTEN)
------如下显示即表示启动nginx服务了
ot@moshan nginx-1.8.0]# /application/apache/bin/apachectl stop
[root@moshan nginx-1.8.0]# /application/nginx/sbin/nginx
[root@moshan nginx-1.8.0]# ps -ef|grep nginx
root 30631 1 0 16:41 ? 00:00:00 nginx: master process
/application/nginx/sbin/nginx
nginx 30632 30631 0 16:41 ? 00:00:00 nginx: worker process
root 30641 28278 0 16:45 pts/0 00:00:00 grep nginx
[root@moshan nginx-1.8.0]#
[root@moshan nginx-1.8.0]# netstat -lnt|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:*
LISTEN
[root@moshan nginx-1.8.0]# lsof -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
clock-app 3966 root 22u IPv4 2698678 0t0 TCP
blog.moshan.com:34687->124.40.53.10:http (CLOSE_WAIT)
firefox 22183 root 101u IPv4 2669889 0t0 TCP
blog.moshan.com:39750->180.149.132.248:http (ESTABLISHED)
nginx 30631 root 6u IPv4 2683918 0t0 TCP *:http (LISTEN)
nginx 30632 nginx 6u IPv4 2683918 0t0 TCP *:http (LISTEN)
[root@moshan nginx-1.8.0]#
--3.测试-----------------如下结果表示nginx搭建成功----------
--默认站点目录/application/nginx/html
[root@moshan nginx-1.8.0]# wget http://192.168.24.88
--2015-05-23 16:49:55-- http://192.168.24.88/
Connecting to 192.168.24.88:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: “index.html”
100%[======================================>] 612 --.-K/s in 0s
2015-05-23 16:49:55 (38.7 MB/s) - “index.html” saved [612/612]
[root@moshan nginx-1.8.0]#
------------如果通过浏览器访问不了--------可以关闭防火墙和selinux--------------查看错误日志
--目录分析
[root@moshan nginx]# tree
.
├── client_body_temp-----------------------临时目录
├── conf-----------------------------------所有配置文件的目录
│ ├── fastcgi.conf-----------------------fastcgi的配置文件
│ ├── fastcgi.conf.default
│ ├── fastcgi_params---------------------fastsgi的参数文件
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf--------------------------nginx的主配置文件
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_tem-----------------------------临时目录
├── html------------------------------------默认站点目录
│ ├── 50x.html----------------------------错误页面优雅代替显示文件
│ └── index.html--------------------------主页文件
├── logs------------------------------------默认日志文件存放的目录,包括错误日志
│ ├── access.log--------------------------访问日志
│ ├── error.log---------------------------错误日志
│ └──nginx.pid----------------------------nginx的pid文件,nginx进程启动以后会把所有进程的id写道这个文件
├── proxy_temp
├── sbin------------------------------------命令目录
│ └── nginx-------------------------------nginx的启动命令
├── scgi_temp-------------------------------临时目录
└── uwsgi_temp------------------------------临时目录
9 directories, 21 files
[root@moshan nginx]#
------------基于域名的虚拟主机配置
-------主配置文件nginx.conf
user nginx nginx;
worker_processes 8; #cup的个数*2
error_log /app/logs/nginx_error.log crit; #crit 严重级别
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#将如下内容取消注释(日志文件)
log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.moshan.com moshan.com;
location / {
root html;
index index.html index.htm;
access_log /app/logs/blog_access.log commonlog;#添加日志文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
----重启nginx服务
[root@moshan nginx]# ./sbin/nginx -s reload
----通过浏览器测试
----如果需要添加多个虚拟主机,只需要增加server标签即可
server {
listen 80;
server_name bbs.moshan.com;
location / {
root html/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log commonlog;
}
}
server {
listen 80;
server_name www.moshan.com;
location / {
root html/www;
index index.html index.htm;
access_log /app/logs/www_access.log commonlog
}
}
----也可以新建一个extra/nginx_vhosts.conf
--然后在主配置文件中include该文件--并将所有的server段内容放在nginx_vhosts.conf中
--(可以为每个虚拟主机都建立一个nginx_vhosts.conf),并在主配置文件中都进行include。
-->配置状态信息虚拟主机etra/status_vhosts.conf
server {
listen 80;
server_name status.moshan.com;
location / {
stub_status on;#开启状态
access_log off; #关闭日志文件功能
}
}
-->基于端口的虚拟主机配置
server {
listen 8080;
server_name bbs.moshan.com;
location / {
root html/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log commonlog;
}
}
server {
listen 8090;
server_name www.moshan.com;
location / {
root html/www;
index index.html index.htm;
access_log /app/logs/www_access.log commonlog
}
}
--测试----域名后面带端口哦!!!
-->基于ip的虚拟主机配置
--1.添加两个别名ip
[root@moshan htdocs]# ifconfig eth0:89 ipaddr 192.168.24.89 up
[root@moshan htdocs]# ifconfig eth0:90 ipaddr 192.168.24.90 up
server {
listen 192.168.24.89:8080;
server_name 192.168.24.89;
location / {
root html/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log commonlog;
}
}
server {
listen 192.168.24.90:8090;
server_name 192.168.24.90;
location / {
root html/www;
index index.html index.htm;
access_log /app/logs/www_access.log commonlog
}
}
--2.使用浏览器测试即可。
-->虚拟主机配置别名。
server {
listen 80;
server_name bbs.moshan.com moshan.com;
location / {
root html/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log commonlog;
}
}
server {
listen 80;
server_name www.moshan.com moshan.cn;
location / {
root html/www;
index index.html index.htm;
access_log /app/logs/www_access.log commonlog
}
}