环境:nginx ubuntu ip:10.12.18.112
tomcat1 ubutu ip:10.12.18.105
tomcat2 centos ip:10.12.18.127
client win7 ip:10.12.18.116
安装包:nginx-1.8.0.tar.gz
Nginx 下载:http://nginx.org/en/download.html
解压
sudo tar –zxf nginx-1.8.0.tar.gz –C /usr/src
创建用户和组
sudo groupadd –g 112 –r nginx
sudo useradd –u 112 –r –g 112 nginx
id nginx
uid=112(nginx) gid=112(nginx)groups=112(nginx)
安装nginx编译时所需包
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
编译nginx
sudo ./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--with-pcre
编译成功
sudo make && make install
启动nginx
sudo cp /usr/sbin/nginx /etc/init.d/
sudo service nginx
查看端口
sudo netstat –nulpt
访问测试
安装包:jdk-8u51-linux-x64.tar.gz apache-tomcat-8.0.24.tar.gz
Jdk 下载:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
tomcat 下载:
http://tomcat.apache.org/download-80.cgi
安装jdk
sudo tar -zxf jdk-8u51-linux-x64.tar.gz -C/usr/src
sudo mv jdk1.8.0_51 /usr/local/jdk
/usr/local/jdk/bin/java –version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build25.51-b03, mixed mode)
配置环境变量
sudo vim /etc/profile
JAVA_HOME="/usr/local/jdk"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME
不知道为何,sudo source /etc/profile之后文件并没有生效,执行java -version显示以下信息:
The program ‘java‘ can be found in thefollowing packages:
* default-jre
*gcj-4.8-jre-headless
*openjdk-7-jre-headless
*gcj-4.6-jre-headless
*openjdk-6-jre-headless
Try: sudo apt-get install <selectedpackage>
我重启系统之后/etc/profile文件才生效
我用的虚拟机装的Ubuntu,系统没有预装openjdk
验证jdk成功
java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build25.51-b03, mixed mode)
安装tomcat
sudo tar -zxf apache-tomcat-8.0.24.tar.gz-C /usr/src/
sudo mv apache-tomcat-8.0.24/usr/local/tomcat
启动tomcat
sudo /usr/local/tomcat/bin/startup.sh
错误:我用sudo startup.sh报以下错误,在网上查了很久,也没找到问题所在。。。。
Neitherthe JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variableis needed to run this program
错误:后来用普通用户执行startup.sh,只是说没有权限touch文件而已
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR:/usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
touch: cannot touch‘/usr/local/tomcat/logs/catalina.out‘: Permission denied
/usr/local/tomcat/bin/catalina.sh: 401:/usr/local/tomcat/bin/catalina.sh: cannot create/usr/local/tomcat/logs/catalina.out: Permission denied
解决:切换到root用户,tomcat启动成功
su – root
/usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR:/usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
允许访问8080端口
sudo ufw allow 8080
sudo ufw status
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
8080 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
访问测试http://10.12.18.105:8080/:8080,看到页面即jdk和tomcat安装成功
创建网站存放目录
sudo mkdir /var/www/web/ROOT
更改server.xml文件,在Host name=”localhost”处将appBase=的指向路径改为
/var/www/web
sudo vim /usr/local/tomcat/conf/server.xml
重启tomcat:/usr/local/tomcat/bin/shutdown.sh
创建index.jsp
sudo vim /var/www/web/ROOT/index.jsp
访问测试
Nginx整合tomcat
配置proxy.conf代理参数,让后端tomcat获取访问者的真实IP地址
sudo touch /etc/nginx/proxy.conf
sudo vim proxy.conf
#proxy.conf
proxy_redirect off;
#后端web服务器可以通过X-Forwarded获取访问者的真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
配置nginx.conf
sudo vim /etc/nginx/nginx.conf
#user nobody;
#启动进程,设置成和CPU的数量相等
worker_processes 1;
#全局错误日志及PID文件
error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
#epoll 是多路复用IO中的一种方式,仅限于Linux2.6以上内核,可以提高nginx性能
use epoll;
#单个后台workerprocess进程的最大并发连接数
worker_connections 1024;
}
#设定http服务器,利用反向代理功能提供负载均衡支持
http {
include mime.types;
default_type application/octet-stream;
#读取proxy.conf文件
include /etc/nginx/proxy.conf;
#设定日志格式
access_log /var/log/nginx/access.log;
#设定请求缓冲
server_names_hash_bucket_size 128;
client_header_buffer_size 32K;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
#连接超时时间
keepalive_timeout 60;
tcp_nodelay on;
server {
listen 80;
#定义用nginx.server.com来访问
server_name nginx.server.com;
#定义首页索引文件的名称
index index.html index.jsp;
#静态文件nginx自己处理
location ~ .*\.(flash|javascript|css|static|images|media)$
{
#过期30天
expires 30d;
}
#jsp页面全部转发到tomcat处理
location ~.*.jsp$ {
#网站存放目录位置
root /var/www/web/ROOT;
#转向tomcat处理
proxy_pass http://10.12.18.105:8080;
}
#查看nginx状态
location /NginxStatus {
stub_status on;
access_log on;
}
#定义错误页面提示
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
检查配置文件是否正确
sudo service nginx -t
nginx: the configuration file/etc/nginx/nginx.conf syntax is ok
nginx: configuration file/etc/nginx/nginx.conf test is successful
重新加载nginx
sudo service nginx -s reload
访问测试
这里可以在tomcat1上看到IP地址10.12.18.112(nginx)来访问,并没有显示客户端的真实的
Ip,需要更改tomcat的server.xml文件
sudo vim /usr/local/tomcat/conf/server.xml
将%h修改为%{X-Real-IP}i,下图已经修改好
查看日志,已经是客户端的真实IP地址了
安装tomcat2,和tomcat1步骤一样
Centos已经预装的jdk, 系统默认使用的是以下这个,而不是我们装的oracle的jdk
java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment(rhel-2.3.4.1.el6_3-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09,mixed mode)
安装jdk
sudo tar -zxf jdk-8u51-linux-x64.tar.gz -C/usr/src
sudo mv jdk1.8.0_51 /usr/local/jdk
配置环境变量,记得重启系统或者source /etc/profile生效
vim /etc/profile
JAVA_HOME="/usr/local/jdk"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME
通知系统使用安装的jdk
update-alternatives --install /usr/bin/java java /usr/local/jdk/bin/java 300
update-alternatives --install /usr/bin/javac javac /usr/local/jdk/bin/javac 300
update-alternatives --config java
There are 4 programs which provide ‘java‘.
Selection Command
-----------------------------------------------
*+ 1 /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
2 /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
3 /usr/lib/jvm/jre-1.5.0-gcj/bin/java
4 /usr/local/jdk/bin/java
Enter to keep the current selection[+], ortype selection number: 4
update-alternatives --config javac
There are 3 programs which provide ‘javac‘.
Selection Command
-----------------------------------------------
1 /usr/lib/jvm/java-1.6.0-openjdk.x86_64/bin/javac
* 2 /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/javac
3 /usr/local/jdk/bin/javac
Enter to keep the current selection[+], ortype selection number: 3
测试验证
java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build25.51-b03, mixed mode)
安装tomcat
tar -zxf apache-tomcat-8.0.24.tar.gz -C/usr/src/
mv apache-tomcat-8.0.24 /usr/local/tomcat
创建网页存放目录
mkdir -p /var/www/web/ROOT
vim /usr/local/tomcat/conf/server.xml
启动tomcat2
/usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR:/usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
创建访问页面
vim /var/www/web/ROOT/index.jsp
访问测试
修改nginx.conf 做负载均衡
root@nginx:/var/log/nginx# cat/etc/nginx/nginx.conf
#user nobody;
#启动进程,设置成和CPU的数量相等
worker_processes 1;
#全局错误日志及PID文件
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
#epoll 是多路复用IO中的一种方式,仅限于Linux2.6以上内核,可以提高nginx性能
use epoll;
#单个后台workerprocess进程的最大并发连接数
worker_connections 1024;
}
#设定http服务器,利用反向代理功能提供负载均衡支持
http {
include mime.types;
default_type application/octet-stream;
include /etc/nginx/proxy.conf;
#设定日志格式
access_log /var/log/nginx/access.log;
#设定请求缓冲
server_names_hash_bucket_size 128;
client_header_buffer_size 32K;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
#连接超时时间
keepalive_timeout 60;
tcp_nodelay on;
#设定负载均衡的服务器列表
upstream tomcat {
#weight参数表示权值,权值越高被分配到的几率越大
server 10.12.18.127:8080 weight=2;
server 10.12.18.105:8080 weight=1;
}
server {
listen 80;
#定义用nginx.server.com来访问
server_name nginx.server.com;
#定义首页索引文件的名称
index index.html index.jsp;
#静态文件nginx自己处理
location ~ .*\.(flash|javascript|css|static|images|media)$
{
#过期30天
expires 30d;
}
#jsp页面全部转发到tomcat处理
location ~.*.jsp$ {
root /var/www/web/ROOT;
#转向tomcat处理
proxy_pass http://tomcat;
}
#查看nginx状态
location /NginxStatus {
stub_status on;
access_log on;
}
#定义错误页面提示
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location = /favicon.iso {
log_not_found off;
}
}
}
增加客户端hosts记录来解析nginx.server.com
路径C:\Windows\System32\drivers\etc
最终测试OK
原文地址:http://766792592.blog.51cto.com/8308325/1685051