詳解Linux下Nginx+Tomcat整合的安裝與配置
一、安裝Tomcat和JDK
1、上傳apache-tomcat-6.0.18.tar.gz和jdk-6u12-linux-i586.bin至/usr/local
2、執(zhí)行如下命令安裝tomcat:
#cd /usr/local #tar zxvf apache-tomcat-6.0.18.tar.gz
解壓完成后將apache-tomcat-6.0.18重命名為tomcat
3、執(zhí)行如下命令安裝JDK:
#./jdk-6u12-linux-i586.bin
4、配置環(huán)境變量:
編輯/etc下的profile文件,加上如下內(nèi)容:
JAVA_HOME="/usr/local/jdk1.6.0_12" CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib" PATH=".:$PATH:$JAVA_HOME/bin " CATALINA_HOME="/usr/local/tomcat" export JAVA_HOME CATALINA_HOME
5、啟動tomcat并輸入http://localhost:8080,如果看到貓的頁面即tomcat和jdk安裝成功
6、新建文件目錄/home/www為網(wǎng)站存放目錄,設(shè)置server.xml文件,在Host name=”localhost”處將appBase=的指向路徑改為/home/www/web
7、創(chuàng)建index.jsp至/home/www/web/ROOT,內(nèi)容為:“My web!”
二、安裝Nginx
1、上傳nginx-0.7.63.tar.gz至/usr/local
2、執(zhí)行如下命令解壓nginx:
#cd /usr/local #tar zxvf nginx-0.7.63.tar.gz
3、編譯安裝nginx
#cd nginx-0.7.63 #./configure --with-http_stub_status_module --with-http_ssl_module #啟動server狀態(tài)頁和https模塊
執(zhí)行完后會提示一個錯誤,說缺少PCRE library 這個是HTTP Rewrite 模塊,也即是url靜態(tài)化的包
可上傳pcre-7.9.tar.gz,輸入如下命令安裝:
#tar zxvf pcre-7.9.tar.gz #cd pcre-7.9 #./configure #make #make install
安裝pcre成功后,繼續(xù)安裝nginx
#cd nginx-0.7.63 #./configure #make #make install
4、nginx安裝成功后的安裝目錄為/usr/local/nginx
在conf文件夾中新建proxy.conf,用于配置一些代理參數(shù),內(nèi)容如下:
#!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;
編輯安裝目錄下conf文件夾中的nginx.conf,輸入如下內(nèi)容
#運行nginx所在的用戶名和用戶組
#user www www;
#啟動進程數(shù)
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;
#工作模式及連接數(shù)上限
events
{
use epoll;
worker_connections 65535;
}
#設(shè)定http服務(wù)器,利用它的反向代理功能提供負載均衡支持
http
{
#設(shè)定mime類型
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx/conf/proxy.conf;
#charset gb2312;
#設(shè)定請求緩沖
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;#設(shè)定訪問的默認首頁地址
root /home/www/web/ROOT;#設(shè)定網(wǎng)站的資源存放路徑
#limit_conn crawler 20;
location ~ .*.jsp$ #所有jsp的頁面均交由tomcat處理
{
index index.jsp;
proxy_pass http://localhost:8080;#轉(zhuǎn)向tomcat處理
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #設(shè)定訪問靜態(tài)文件直接讀取不經(jīng)過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;#設(shè)定訪問日志的存放路徑
}
}
5、修改/usr/local/nginx/conf/nginx.conf配置文件后,請執(zhí)行以下命令檢查配置文件是否正確:
#/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 successfully
如果提示unknown host,則可在服務(wù)器上執(zhí)行:ping www.baidu.com如果也是同樣提示unknown host則有兩種可能:
a、服務(wù)器沒有設(shè)置DNS服務(wù)器地址,查看/etc/resolv.conf下是否設(shè)置,若無則加上
b、防火墻攔截
6、啟動nginx的命令
#/usr/local/nginx/sbin/nginx
這時,輸入以下命令查看Nginx主進程號:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
7、停止nginx的命令
#/usr/local/nginx/sbin/nginx -s stop
8、在不停止Nginx服務(wù)的情況下平滑變更Nginx配置
a、修改/usr/local/nginx/conf/nginx.conf配置文件后,請執(zhí)行以下命令檢查配置文件是否正確:
/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 successfully
b、這時,輸入以下命令查看Nginx主進程號:
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕顯示的即為Nginx主進程號,例如:
6302
這時,執(zhí)行以下命令即可使修改過的Nginx配置文件生效:
kill -HUP 6302
或者無需這么麻煩,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/nginx/nginx.pid`
9、nginx啟動好后啟動tomcat,此時輸入http://主機ip地址即可看到“My web!”
三、其他
stub_status
語法: stub_status on
默認值: None
作用域: location
創(chuàng)建一個 location 區(qū)域啟用 stub_status
“stub status” 模塊返回的狀態(tài)信息跟 mathopd's 的狀態(tài)信息很相似. 返回的狀態(tài)信息如下:
Active connections: 291 server accepts handled requests 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 106
active connections — 對后端發(fā)起的活動連接數(shù)
server accepts handled requests — nginx 總共處理了 16630948 個連接, 成功創(chuàng)建 16630948 次握手 (證明中間沒有失敗的), 總共處理了 31070465 個請求 (平均每次握手處理了 1.8個數(shù)據(jù)請求)
reading — nginx 讀取到客戶端的Header信息數(shù)
writing — nginx 返回給客戶端的Header信息數(shù)
waiting — 開啟 keep-alive 的情況下,這個值等于 active – (reading + writing),意思就是Nginx說已經(jīng)處理完正在等候下一次請求指令的駐留連接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ubuntu20.04桌面安裝及root權(quán)限開通和ssh安裝詳解
這篇文章主要介紹了Ubuntu20.04桌面安裝及root權(quán)限開通和ssh安裝詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
在Linux中列出Systemd下所有正在運行的服務(wù)的方法指南
Systemd是Linux的系統(tǒng)和服務(wù)管理器; init進程的替代品,與SysV 和LSB init腳本兼容,systemctl命令是管理systemd的主要工具,在這篇文章中,我們將演示如何 Linux中列出systemd下所有正在運行的服務(wù),感興趣的同學(xué)可以借鑒閱讀2023-06-06
Linux服務(wù)器VPS的Windows?DD包詳細的制作教程
這里就說下Windows?dd包的制作過程,雖然網(wǎng)上很多這樣的dd包,但對于一些來歷不明的,也會有點不放心,所以我們有時間的話可以自己定制一個2022-08-08
Linux內(nèi)核設(shè)備驅(qū)動之Linux內(nèi)核基礎(chǔ)筆記整理
今天小編就為大家分享一篇關(guān)于Linux內(nèi)核設(shè)備驅(qū)動之Linux內(nèi)核基礎(chǔ)筆記整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Linux在服務(wù)器多節(jié)點下面實現(xiàn)快速查找日志
在多節(jié)點分布式系統(tǒng)中,通過使用find和grep命令組合,可以實現(xiàn)高效的日志搜索,先定位到具體日期的文件夾,再執(zhí)行命令全面掃描各個服務(wù)器節(jié)點下的日志文件,從而簡化日志查詢過程2024-11-11

