準備:
- linux版本:CentOS7.4 64位
- 以下操作全部在root賬號下進行
- 已經(jīng)在騰訊云上進行備案的域名一枚亿蒸,如何解析,請參看騰訊云 .cn域名解析配置講解 域名綁定 澤0715 新浪博客
一边锁、環(huán)境
jdk-1.8
nginx-1.12.1
tomcat-7.0.79
1姑食、防火墻開通80和443端口,參看CentOS7.x之防火墻
2茅坛、騰訊云安全組開放80和443端口
二音半、JDK1.8安裝
參見騰訊云服務器:CentOS安裝MySQL、JDK贡蓖、Tomcat及web項目發(fā)布外網(wǎng)實戰(zhàn)中jdk安裝部分
三曹鸠、Tomcat安裝
#如果沒有此目錄,則創(chuàng)建
cd /usr/local/tomcat
#下載tomcat-7.0.79
wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.79/bin/apache-tomcat-7.0.79.tar.gz
#解壓并重命名分別為tomcat-7.0.79_01斥铺、tomcat-7.0.79_02(server.conf中的端口下文有詳細配置)
tar -xzvf apache-tomcat-7.0.79.tar.gz
四彻桃、Web項目
1、tomcat-7.0.79_01中的WebProject項目index.jsp內(nèi)容
tomcat-7.0.79_01中的server.conf配置如下
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_01/webapps/WebProject" reloadable="true" crossContext="true" />
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
2晾蜘、tomcat-7.0.79_02中的WebProject項目index.jsp內(nèi)容
tomcat-7.0.79_02中的server.conf中的配置如下
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8081" protocol="HTTP/1.1" URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8444" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_02/webapps/WebProject" reloadable="true" crossContext="true" />
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
3邻眷、server.conf配置不同之處有四
#1_01
<Server port="8005" shutdown="SHUTDOWN">
#1_02
<Server port="8006" shutdown="SHUTDOWN">
#2_01
<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8443" />
#2_02
<Connector port="8081" protocol="HTTP/1.1" URIEncoding="UTF-8"
connectionTimeout="20000"
redirectPort="8444" />
#3_01
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
#3_02
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
#4_01
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_01/webapps/WebProject" reloadable="true" crossContext="true" />
#4_02
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/usr/local/tomcat/tomcat7/tomcat-7.0.79_02/webapps/WebProject" reloadable="true" crossContext="true" />
說明:
- 將WebProject項目在Eclipse或者MyEclipse中打成war包上傳至tomcat啟動目錄中眠屎,tomcat啟動一次后,會自動解壓war包肆饶,將tomcat關閉后改衩,即可刪除war包
- 將兩個tomcat分別啟動
五、Nginx安裝
1驯镊、下載葫督、解壓
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz #穩(wěn)定版
tar nginx-1.12.1.tar.gz
cd nginx-1.12.1
2、安裝準備
yum install gcc gcc-c++ -y #安裝gcc和c++包
yum -y install pcre pcre-devel zlib-devel openssl-devel
- SSL功能需要openssl庫板惑,官網(wǎng)地址
- gzip模塊需要zlib庫候衍,官網(wǎng)地址
- rewrite模塊需要pcre庫(實際nginx中也可以使用return進行301跳轉)官網(wǎng)地址
3、安裝Nginx
cd /usr/local/src/nginx-1.12.1
#安裝到指定目錄
./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
#編譯
make && make install
- --prefix=/usr/local/nginx/ #nginx安裝的根目錄
- --with-http_stub_status_module #監(jiān)控nginx運行狀態(tài)
- --with-http_ssl_module #添加ssl模塊
有關nginx參數(shù)詳解洒放,請參考nginx安裝及編譯參數(shù)詳解
4蛉鹿、 nginx.conf參數(shù)配置(圖文)
xxx.cn全部以你申請的域名替換
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream tomcat7_cluster{
server localhost:8080 weight=5;
server localhost:8081 weight=5;
}
server {
listen 80;
server_name www.xxx.cn;
access_log logs/jmszwzr.access.log main;
error_log logs/jmszwzr.error.log;
#rewrite ^(.*) https://$server_name$1 permanent;
#棄用rewrite,使用return效率更高
return 301 https://$server_name$request_uri;
location / {
proxy_pass http://tomcat7_cluster;
}
}
# HTTPS server
server {
listen 443 ssl;
#server_name www.xxx.cn;
server_name localhost;
#ssl on;
access_log logs/ssl.access.log main;
error_log logs/ssl.error.log;
#為虛擬主機指定pem格式的證書文件
ssl_certificate 1_www.xxx.cn_bundle.crt;
#為虛擬主機指定私鑰文件
ssl_certificate_key 2_www.xxx.cn.key;
#ssl_session_cache shared:SSL:1m;
#客戶端能夠重復使用存儲在緩存中的會話參數(shù)時間
ssl_session_timeout 10m;
#指定使用的ssl協(xié)議
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#指定許可的密碼描述
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
#SSLv3和TLSv1協(xié)議的服務器密碼需求優(yōu)先級高于客戶端密碼
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://tomcat7_cluster;
}
}
}
配置好nginx.conf文件之后往湿,可以進行驗證配置是否正確
#在nginx根目錄下
./sbin/nginx #啟動nginx
./sbin/nginx -s reload #軟重啟
六妖异、瀏覽器訪問
訪問兩個項目的幾率可在nginx中配置
結尾:以上只是簡單的將nginx作為反向代理服務器,配以https加密訪問领追,實際情況中他膳,還有很多需要添加的模塊和優(yōu)化的地方。如tomcat集群下的session共享绒窑、nginx優(yōu)化等問題棕孙。
小白初入,如有錯誤些膨,歡迎各位簡友指正蟀俊,在此感謝!