首页 > 代码库 > Linux(ubuntu12.04)下搭建Nginx+Tomcat

Linux(ubuntu12.04)下搭建Nginx+Tomcat

首先是安装包的准备nginx7.9:下载nginx  |tomcat7.0下载tomcat  |JDk下载JDK7.0

Nginx的优点太多太多,比apache好很多,更加稳定,功能更加强悍,越来越多的开发人员选择使用nginx来替代apache!!

具体查看这篇文章http://blog.csdn.net/a454211787/article/details/22494485


首先,我们配置jdk,下载好jdk之后,我的默认实在download下面,然后使用linux命令解压

$tar zxvf jdk
安装完(我是安装在/usr/java)之后,输入linux命令

sudo gedit /etc/profile
编辑profile文件,添加以下文本

export JAVA_HOME=/usr/java/jdk1.6.0_27

export JAVA_BIN=/usr/java/jdk1.6.0_27/bin

export PATH=$PATH:$JAVA_HOME/bin

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export JAVA_HOME JAVA_BIN PATH CLASSPATH
运行
java-version看是否已经安装成功了
接着安装tomcat,并且重命名为tomcat,编辑/etc/frofile加上以下语句

  
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME

进入tomcat/bin目录下面,运行
$./startup.sh
然后访问http://localhost:8080看是否访问成功.如果看到了tomcat的欢迎界面,恭喜你已经成功了一半,


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

接下来我们安装nginx,我们解压nginx  

$tar zxvf nginx~~~.tar.gz然后
$cd nginx/
$./configure
$make
$sudo make install(记住这里要用超级权限)
当执行./configure的时候会抛出说缺少PCRE library 这个是HTTP Rewrite 模块,也即是url静态化的包

所以首先我们要安装这个东西  PCRE下载pcre选择7.9版本

$tar zxvf pcre~~.tar.gz
$./configure
$make
$sudo make install

安装完之后我们在执行nginx的安装命,然后就可以了

接下来我们执行以下命令,在nginx的conf文件夹下面创建一个proxy.conf文件,配置一些代理参数

$cd /usr/local/nginx/conf
$sudo touch proxy.conf

#!nginx (-) 
# proxy.conf 
proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;  #获取真实ip
#proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

接着,我们要编辑nginx.conf文件

$sudo gedit nginx.conf(记住要是超级权限)
写上基本配置
#运行nginx所在的用户名和用户组
#user  www www; 
  
#启动进程数
worker_processes 8;
#全局错误日志及PID文件
error_log  /usr/local/nginx/logs/nginx_error.log  crit;
  
pid        /usr/local/nginx/nginx.pid;
  
#Specifies the value for maximum file descriptors that can be opened by this process.
  
worker_rlimit_nofile 65535;
#工作模式及连接数上限
events
{
  use epoll;
  worker_connections 65535;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
  #设定mime类型
  include       mime.types;
  default_type  application/octet-stream;
  include /usr/local/nginx/conf/proxy.conf;
  #charset  gb2312;
  #设定请求缓冲    
  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;
  
#  fastcgi_connect_timeout 300;
#  fastcgi_send_timeout 300;
#  fastcgi_read_timeout 300;
#  fastcgi_buffer_size 64k;
#  fastcgi_buffers 4 64k;
#  fastcgi_busy_buffers_size 128k;
#  fastcgi_temp_file_write_size 128k;
  
#  gzip on;
#  gzip_min_length  1k;
#  gzip_buffers     4 16k;
#  gzip_http_version 1.0;
#  gzip_comp_level 2;
#  gzip_types       text/plain application/x-javascript text/css application/xml;
#  gzip_vary on;
  
  #limit_zone  crawler  $binary_remote_addr  10m;
 ###禁止通过ip访问站点
  server{
        server_name _;
        return 404;
        }
  
  
  server
  {
    listen       80;
    server_name  localhost;
    index index.html index.htm index.jsp;#设定访问的默认首页地址
    root  /home/www/web/ROOT;#设定网站的资源存放路径
  
    #limit_conn   crawler  20;    
      
    location ~ .*.jsp$ #所有jsp的页面均交由tomcat处理
    {
      index index.jsp;
      proxy_pass http://localhost:8080;#转向tomcat处理
      }
        
      
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat
    {
      expires      30d;
    }
  
    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }    
  
#定义访问日志的写入格式
     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /usr/local/nginx/logs/localhost.log access;#设定访问日志的存放路径
  
      }   
}
然后我们运行linux命令
#/usr/local/nginx/sbin/nginx -t

如果屏幕显示以下两行信息,说明配置文件正确:  

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  the configuration file /usr/local/nginx/conf/nginx.conf was tested successfull
但是这个时候一般会出现一个问题说是

 error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决方法:

ln -s /usr/local/lib/libpcre.so.1 /lib64

32位系统则:

ln -s /usr/local/lib/libpcre.so.1 /lib

注:
/usr/local/lib/libpcre.so.1 为prce安装后的文件地址
低版本prce对应的libpcre.so.1 为libpcre.so.0


然后
#/usr/local/nginx/sbin/nginx
#/usr/local/nginx/sbin/nginx -s stop
检查nginx配置是否正确</span>
/usr/local/nginx/sbin/nginx -t
查看nginx进程
ps -ef |grep nginx

简单的nginx+tomcat就搭建好了,接下来要做的是搭建nginx+tomca集群,以及负载均衡

装载请指明出处

http://blog.csdn.net/a837199685








Linux(ubuntu12.04)下搭建Nginx+Tomcat