詳解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、啟動(dòng)tomcat并輸入http://localhost:8080,如果看到貓的頁(yè)面即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 #啟動(dòng)server狀態(tài)頁(yè)和https模塊
執(zhí)行完后會(huì)提示一個(gè)錯(cuò)誤,說(shuō)缺少PCRE library 這個(gè)是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; #獲取真實(shí)ip #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #獲取代理者的真實(shí)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)容
#運(yùn)行nginx所在的用戶名和用戶組 #user www www; #啟動(dòng)進(jìn)程數(shù) worker_processes 8; #全局錯(cuò)誤日志及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ù)器,利用它的反向代理功能提供負(fù)載均衡支持 http { #設(shè)定mime類型 include mime.types; default_type application/octet-stream; include /usr/local/nginx/conf/proxy.conf; #charset gb2312; #設(shè)定請(qǐng)求緩沖 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; ###禁止通過(guò)ip訪問(wèn)站點(diǎn) server{ server_name _; return 404; } server { listen 80; server_name localhost; index index.html index.htm index.jsp;#設(shè)定訪問(wèn)的默認(rèn)首頁(yè)地址 root /home/www/web/ROOT;#設(shè)定網(wǎng)站的資源存放路徑 #limit_conn crawler 20; location ~ .*.jsp$ #所有jsp的頁(yè)面均交由tomcat處理 { index index.jsp; proxy_pass http://localhost:8080;#轉(zhuǎn)向tomcat處理 } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #設(shè)定訪問(wèn)靜態(tài)文件直接讀取不經(jīng)過(guò)tomcat { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } #定義訪問(wèn)日志的寫入格式 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è)定訪問(wèn)日志的存放路徑 } }
5、修改/usr/local/nginx/conf/nginx.conf配置文件后,請(qǐng)執(zhí)行以下命令檢查配置文件是否正確:
#/usr/local/nginx/sbin/nginx -t
如果屏幕顯示以下兩行信息,說(shuō)明配置文件正確:
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ù)器沒(méi)有設(shè)置DNS服務(wù)器地址,查看/etc/resolv.conf下是否設(shè)置,若無(wú)則加上
b、防火墻攔截
6、啟動(dòng)nginx的命令
#/usr/local/nginx/sbin/nginx
這時(shí),輸入以下命令查看Nginx主進(jìn)程號(hào):
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配置文件后,請(qǐng)執(zhí)行以下命令檢查配置文件是否正確:
/usr/local/nginx/sbin/nginx -t
如果屏幕顯示以下兩行信息,說(shuō)明配置文件正確:
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、這時(shí),輸入以下命令查看Nginx主進(jìn)程號(hào):
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕顯示的即為Nginx主進(jìn)程號(hào),例如:
6302
這時(shí),執(zhí)行以下命令即可使修改過(guò)的Nginx配置文件生效:
kill -HUP 6302
或者無(wú)需這么麻煩,找到Nginx的Pid文件:
kill -HUP `cat /usr/local/nginx/nginx.pid`
9、nginx啟動(dòng)好后啟動(dòng)tomcat,此時(shí)輸入http://主機(jī)ip地址即可看到“My web!”
三、其他
stub_status
語(yǔ)法: stub_status on
默認(rèn)值: None
作用域: location
創(chuàng)建一個(gè) 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 — 對(duì)后端發(fā)起的活動(dòng)連接數(shù)
server accepts handled requests — nginx 總共處理了 16630948 個(gè)連接, 成功創(chuàng)建 16630948 次握手 (證明中間沒(méi)有失敗的), 總共處理了 31070465 個(gè)請(qǐng)求 (平均每次握手處理了 1.8個(gè)數(shù)據(jù)請(qǐng)求)
reading — nginx 讀取到客戶端的Header信息數(shù)
writing — nginx 返回給客戶端的Header信息數(shù)
waiting — 開啟 keep-alive 的情況下,這個(gè)值等于 active – (reading + writing),意思就是Nginx說(shuō)已經(jīng)處理完正在等候下一次請(qǐng)求指令的駐留連接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Linux中查找命令的執(zhí)行時(shí)間的幾種方法小結(jié)
在Linux系統(tǒng)中,了解命令的執(zhí)行時(shí)間對(duì)于優(yōu)化系統(tǒng)性能和提高效率至關(guān)重要,本文將介紹幾種方法來(lái)查找命令的執(zhí)行時(shí)間,包括內(nèi)置的time命令、GNU time工具、strace以及perf工具,需要的朋友可以參考下2024-05-05CentOS 6.6服務(wù)器編譯安裝lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)
這篇文章主要介紹了CentOS 6.6服務(wù)器編譯安裝lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3),需要的朋友可以參考下2016-10-10虛擬機(jī)中ubuntu不能聯(lián)網(wǎng)問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了虛擬機(jī)中ubuntu不能聯(lián)網(wǎng)問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03Linux平臺(tái)和Windows平臺(tái)互傳文件的實(shí)現(xiàn)方法
本文講述了在Linux主機(jī)與windows主機(jī)之間如何互傳文件的方法,因?yàn)橛袝r(shí)linux主機(jī)中的一些文件可能會(huì)在windows環(huán)境下用到,所以文章給大家介紹的非常詳細(xì),感興趣的朋友可以參考下2024-05-05Linux下動(dòng)態(tài)鏈接庫(kù)加載路徑及搜索路徑問(wèn)題
這篇文章主要介紹了Linux下動(dòng)態(tài)鏈接庫(kù)加載路徑及搜索路徑問(wèn)題,需要的朋友可以參考下2018-04-04詳解阿里云CentOS Linux服務(wù)器上用postfix搭建郵件服務(wù)器
本篇文章主要介紹了詳解阿里云CentOS Linux服務(wù)器上用postfix搭建郵件服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12