所有的安装包可以去以下地址下载,或者自行去官网下载,下面都有介绍.
所有安装包地址:http://download.csdn.net/detail/carboncomputer/9238037
原文地址:http://www.cnblogs.com/zhongshengzhen/p/nginx.html
nginx 学习地址:http://tengine.taobao.org/book/appendix_c.html#nginxlinux
具体步骤如下:
1、下载PCRE, 是一个用C语言编写的正则表达式函数库
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhost download]# tar zxvf pcre-8.36.tar.gz
2、下载zlib库
[root@localhost pcre-8.36]# cd /tmp/download/
[root@localhost download]# wget http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz
[root@localhost download]# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
4、下载SSL
[root@localhost zlib-1.2.8]# cd ..
[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz
[root@localhost download]# cd openssl-1.0.1c
[root@localhost openssl-1.0.1c]# tar -zxvf openssl-1.0.1c.tar.gz
5、下载nginx
[root@localhost download]# wget http://nginx.org/download/nginx-1.2.8.tar.gz
[root@localhost download]# tar -zxvf nginx-1.2.8.tar.gz
6、安装
[root@localhost download]mv pcre-8.36 /usr/local/
[root@localhost download]mv zlib-1.2.8 /usr/local/
[root@localhost download]mv openssl-1.0.1c /usr/local/
[root@localhost download]mv nginx-1.2.8 /usr/local/
[root@localhost download]cd /usr/local/
[root@localhost local]# cd pcre-8.36
[root@localhost pcre-8.36]# ./configure&&make&&make install
[root@localhost pcre-8.36] cd ../zlib-1.2.8
[root@localhost zlib-1.2.8]# ./configure && make && make install
[root@localhost zlib-1.2.8]# cd ../openssl-1.0.1c
[root@localhost openssl-1.0.1c]# ./config && make && make install
[root@localhost openssl-1.0.1c]# cd ../nginx-1.2.8
[root@localhost nginx-1.2.8]# ./configure --prefix=/usr/local/nginx && make && make install
7、启动nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8、测试
在确保可以连接到服务器的电脑上,浏览器输入装了nginx的机器的ip地址,会看到Welcome to nginx!的提示说明安装配配置成功了。
负载均衡配置:
现有两部服务器:
192.168.1.100 (按照以上操作安装有
nginx,作为转发机,虚拟机,同时运行有测试用的web工程)
192.168.1.101 (无安装nginx,运行有测试用的web工程,虚拟机)
测试用的web工程为:speechWeb
[root@a conf]# vi /usr/local/nginx/conf/nginx.conf
配置如下展示:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)";
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
"conf/nginx.conf" [readonly] 137L, 3107C 1,0-1 Top
# root html;
# index index.html index.htm;
# }
#}
# my server
upstream solrserver {
# solr1
server 192.168.1.100:9080 weight=2;
# solr2
server 192.168.1.101:8080 weight=2;
# solr3
}
server {
listen 8030;
location / {
proxy_pass http://solrserver;
#auth_basic "Restricted";
#auth_basic_user_file pwd;
}
}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
其中关键代码是:
upstream solrserver {
# solr1
server 192.168.1.100:9080 weight=2;
# solr2
server 192.168.1.101:8080 weight=2;
# solr3
}
server {
listen 8030;
location / {
proxy_pass http://solrserver;
#auth_basic "Restricted";
#auth_basic_user_file pwd;
}
}
启动每台机器的speechWeb应用,然后通过以下格式在浏览器中访问转发机:
http://192.168.1.100:8030/speechWeb/
转发机会根据配置的权重参数(weight)进行负载均衡式访问服务器(http://192.168.1.100:9080/speechWeb/ 或 http://192.168.1.100:8030/speechWeb/)
每次更改配置文件后都需要重新启动nginx才会生效。
1、当第一次启动gnix时,用以下方式:
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2、当nginx已经在运行时,则需要平滑重启,步骤如下:
先查看nginx master的pid: ps aux|grep nginx|grep master
假设nginx的pid为3979,则平滑重启命令为:kill -HUP 3979
常见问题及解决办法:
1、安装PCRE时提示configure: error: You need a C++ compiler for C++ support.
原因是没有安装c++编译器,采用下面的命令安装:
[root@localhost pcre-8.37]# apt-get install build-essential
[root@localhost pcre-8.37]# apt-get install gcc
2、启动nginx失败
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
原因是64bit的系统,但是默认取了/usr/local/lib里面的包
检查方法:
[root@localhost nginx]# cd /usr/local/
[root@localhost local]# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
[root@localhost local]# ls /lib/ |grep pcre
说明缺失的包在lib64
设置软连接来解决:
[root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
[root@localhost local]# cd nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3、启动报错:
报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决办法:sudo fuser -k 80/tcp
端口被占用,关闭占用端口