實踐環境:
系統版本:CentOS6.4 X86_64
Nginx版本:Nginx-1.4.5
Tomcat版本:Tomcat-7.0
虛擬機10.10.54.61
############################
源碼編譯nginx1.4.5
1.下載
[root@gyf soft]#wget http://nginx.org/download/nginx-1.4.5.tar.gz
[root@gyf soft]# tar xvf nginx-1.4.5.tar.gz
2.編譯
yum install openssl-devel.X86_64 make pcre
[root@gyf nginx-1.4.5]# ./configure --prefix=/usr/local/nginx --user=apache --group=apache --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
///
--with-http_stub_status_module enable ngx_http_stub_status_module ---支持監控
--with-http_gzip_static_module enable ngx_http_gzip_static_module ---支持壓縮
3.安裝
[root@gyf nginx-1.4.5]# make && make install
4.啓動
[root@gyf conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] getpwnam("apache") failed
[root@gyf conf]# useradd apache
[root@gyf conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@gyf conf]# netstat -ntlp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1572/httpd
[root@gyf conf]# apachectl stop
[root@gyf conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5.關閉:給父進程發送一個TERM信號,試圖殺死它和它的子進程。
[root@s01 logs]# cat /usr/local/nginx/logs/nginx.pid | xargs kill -TERM
6.重啓
[root@s01 logs]# cat /usr/local/nginx/logs/nginx.pid | xargs kill -HUP
HUP 重啓
TERM,INT 快速中止
USR1 從新打開日誌文件,用於日誌切割
USR2 平滑升級可執行程序
QUIT 從容關閉
WINCH 從容關閉工做進程
//測試配置文件
[root@s01 html]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
//nginx配置文件格式:
user apache apache;
worker_processes 2; ---cpu核-1
....
event{}
http{
....
upstream {}
server{ location{} }
}
#############################
安裝tomcat
1.下載jdk-7u51-linux-x64.rpm
rpm -ivh jdk-7u51-linux-x64.rpm
2.添加環境變量,配置jdk
[root@gyf soft]# find / -name '*jdk*'
/usr/java/jdk1.7.0_51
[root@gyf profile.d]# vim /etc/profile
JAVA_HOME=/usr/java/jdk1.7.0_51
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME
[root@gyf soft]# source /etc/profile
[root@gyf soft]#java -version
3.安裝tomcat
[root@gyf soft]# tar xvf apache-tomcat-7.0.52.tar.gz
[root@gyf soft]# mv apache-tomcat-7.0.52 /usr/local/tomcat
4.作tomcat自動啓動腳本
[root@gyf soft]# cd /usr/local/tomcat/bin/
[root@gyf bin]# cp catalina.sh /etc/init.d/tomcat
[root@gyf bin]# chmod +x /etc/init.d/tomcat
[root@gyf init.d]# vim tomcat
#chkconfig: 2345 88 99
#description: tomcat
//jdk目錄
:96 JAVA_HOME=/usr/java/jdk1.7.0_51
//tomcat目錄
CATALINA_HOME=/usr/local/tomcat
[root@gyf bin]# chkconfig --add tomcat
5.啓動tomcat
[root@gyf soft]# /etc/init.d/tomcat start
在瀏覽器中搜索10.10.54.61:8080出現tomcat原始頁面即安裝成功
#################
一臺機器同時運行兩個tomcat
上面前兩步無需在作
1.安裝tomcat到新目錄
[root@gyf soft]# tar xvf apache-tomcat-7.0.52.tar.gz
[root@gyf soft]# mv apache-tomcat-7.0.52 /usr/local/tomcat2
2.編輯tomcat/conf/server.xml,將全部端口號8:開頭都改成9開頭(其餘端口只要不衝突便可,目的是不讓多個的Tomcat的端口號衝突)
修改3個地方
第一:
<Server port="9005" shutdown="SHUTDOWN"> ----原來是8005,關閉端口號
第二
<Connector port="9080" maxHttpHeaderSize="8192" ----原來是8080,http連接端口號:1:
第三<Connector port="9009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" /> ---原來是8009,jvm即java虛擬機訪問端口
3.啓動
[root@gyf tomcat2]# ./bin/startup.sh
4.查看進程兩個
[root@gyf tomcat2]# ps -ef |grep tomcat
7.作tomcat自動啓動腳本
[root@gyf soft]# cd /usr/local/tomcat2/bin/
[root@gyf bin]# cp catalina.sh /etc/init.d/tomcat2
[root@gyf bin]# chmod +x /etc/init.d/tomcat2
[root@gyf init.d]# vim tomcat2
#chkconfig: 2345 85 96
#description: tomcat
:96 JAVA_HOME=/usr/java/jdk1.7.0_51
CATALINA_HOME=/usr/local/tomcat2
[root@gyf bin]# chkconfig --add tomcat2
8.重啓tomcat2
[root@gyf soft]# /etc/init.d/tomcat2 start
####tomcat配置用戶管理
[root@gyf conf]# vim /usr/local/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat" roles="admin-gui,manager-gui"/>
##################
配置nginx
1.配置代理文件
[root@gyf htdocs]# cd /usr/local/nginx/conf/
[root@gyf conf]#mkdir virtual
[root@gyf conf]# vim virtual/bbs.ssr.com.conf
upstream bbs_ssr_com {
server localhost:9080 max_fails=3 weight=1 fail_timeout=60s;
server localhost:8080 max_fails=3 weight=3 fail_timeout=60s;
}
server {
listen 80;
server_name bbs.ssr.com;
location / {
#jsp網站程序根目錄,通常nginx與tomcat在同一個目錄
# root /usr/local/tomcat/webapps/ROOT;
root /var/www/bbs
index index.html index.jsp index.html;
}
location ~ .*\.(gif|jpg|png|bmp|swf)$ #由nginx處理靜態頁面
{
expires 30d; #使用expires緩存模塊,緩存到客戶端30天
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
location ~ (\.jsp)|(\.do)$ {
index index.jsp ;
proxy_pass http://bbs_ssr_com; #來自jsp請求交給tomcat處理
proxy_set_header Host $host;
proxy_set_header X-Real-IP$remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;#後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
proxy_redirect off;
client_max_body_size 10m; #容許客戶端請求的最大單文件字節數
client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數,
proxy_connect_timeout 90; #nginx跟後端服務器鏈接超時時間(代理鏈接超時)
proxy_read_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時)
proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffers 6 32k; #proxy_buffers緩衝區,網頁平均在32k如下的話,這樣設置
proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
}
}
2..主配置文件配置
[root@gyf ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
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;
keepalive_timeout 65;
#gzip壓縮功能設置
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-javascripttext/css application/xml;
gzip_vary on;
include virtual/bbs.ssr.com.conf;
}
3.配置tomcat
[root@gyf conf]# vim /usr/local/tomcat/conf/server.xml
#確認8080端口
:70 <Connector port="8080" protocol="HTTP/1.1"
71 connectionTimeout="20000"
72 redirectPort="8443" />
123 <Host name="localhost" appBase="webapps"
124 unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/bbs" />
125 若是網站沒有放在tomcat默認根目錄/usr/local/tomcat/webapps/ROOT/
時添加<Context path="" docBase="/var/www/bbs" />
[root@gyf conf]# vim /usr/local/tomcat2/conf/server.xml
#確認9080端口
:70 <Connector port="8080" protocol="HTTP/1.1"
71 connectionTimeout="20000"
72 redirectPort="8443" />
123 <Host name="localhost" appBase="webapps"
124 unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/bbs" />
4.建網頁
[root@gyf www]#mkdir -p /var/www/bbs
[root@gyf bbs]# vim index.html
html
[root@gyf bbs]# vim index.jsp
jsp
[root@gyf bbs]# vim index.do
do
5.重啓服務
[root@gyf ~]# /etc/init.d/nginx restart
[root@gyf ~]# /etc/init.d/tomcat restart
[root@gyf ~]# /etc/init.d/tomcat2 restart
6.測試
在ubuntu10.10.54.60
gyf@gyf :~$ vim /etc/hosts
10.10.54.60 bbs.ssr.com
在搜索應請輸入bbs.ssr.com
顯示html
輸入bbs.ssr.com/index.jsp
http://10.10.54.61/index.jsp
顯示jsp
輸入bbs.ssr.com/index.do
http://10.10.54.61/index.do
顯示do
javascript