1、下载PCRE, 是一个用C语言编写的正则表达式函数库
[root@localhostpcre-8.36]# cd /tmp/download/
[root@localhostdownload]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
[root@localhostdownload]# tar zxvf pcre-8.36.tar.gz
2、下载zlib库
[root@localhostpcre-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@localhostdownload]# tar -zxvf zlib-1.2.8.tar.gz cdzlib-1.2.8
4、下载SSL
[root@localhostzlib-1.2.8]# cd ..
[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz
[root@localhostdownload]# cd openssl-1.0.1c
[root@localhostopenssl-1.0.1c]# tar -zxvfopenssl-1.0.1c.tar.gz
5、下载nginx
[root@localhostdownload]# wget http://nginx.org/download/nginx-1.2.8.tar.gz
[root@localhostdownload]# 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@localhostpcre-8.36]# ./configure&&make&&make install
[root@localhost pcre-8.36]cd ../zlib-1.2.8
[root@localhostzlib-1.2.8]# ./configure && make&& make install
[root@localhost zlib-1.2.8]#cd ../openssl-1.0.1c
[root@localhostopenssl-1.0.1c]# ./config && make&& make install
[root@localhost openssl-1.0.1c]#cd ../nginx-1.2.8
[root@localhostnginx-1.2.8]# ./configure--prefix=/usr/local/nginx && make && make install
7、启动nginx
[root@localhostnginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8、测试
在确保可以连接到服务器的电脑上,浏览器输入装了nginx的机器的ip地址,会看到Welcome to nginx!的提示说明安装配配置成功了。
负载均衡配置:
现有两部服务器:
192.168.45.128 (按照以上操作安装有nginx,作为转发机,虚拟机)
192.168.45.128 (无安装nginx,运行有测试用的web工程,虚拟机)
192.168.1.10 (无安装nginx,运行有测试用的web工程,本地电脑地址),运行有测试用的web工程
127.0.0.1web_app
[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;
upstream web_app{
server 192.168.45.128:8080max_fails=2 fail_timeout=30s;
server 192.168.45.129:8080max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_next_upstream http_502http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP$remote_addr;
proxy_set_header X-Forwarded_For$proxy_add_x_forwarded_for;
proxy_pass http://web_app;
expires 3d;
}
#error_page 404 /404.html;
# redirect server error pages to thestatic page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apachelistening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGIserver listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache'sdocument root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-,name-, and port-based configuration
#
server {
listen 80;
# listen somename:8080;
server_name 127.0.0.1;
location / {
root html;
index index.html index.htm;
}
}
# 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;
# }
#}
}
设置host:
[root@aconf]# vi /etc/hosts
添加hosts:
127.0.0.1 web_app
在本地的windows机器上也添加host
192.168.45.128 web_app
启动nginx
[root@localhostnginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
在192.168.45.128和192.168.45.129上的tomcat放置一个简单的html页面。
启动虚拟机上的web应用:
在192.168.1.10上打开网址
http://web_app/hello.html
OK!
常见问题及解决办法:
1、安装PCRE时提示configure: error: Youneed a C++ compiler for C++ support.
原因是没有安装c++编译器,采用下面的命令安装:
[root@localhostpcre-8.37]# yum install -y gcc gcc-c++
2、启动nginx失败
[root@localhostnginx]# /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@localhostnginx]# cd /usr/local/
[root@localhostlocal]# ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
[root@localhost local]# ls /lib/ |grep pcre
说明缺失的包在lib64
设置软连接来解决:
[root@localhostlocal]# 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
端口被占用,关闭占用端口