Centos Apache and Tomcat integrated configuration

The tomcat and apache httpd services have been deployed and installed on Centos, and tomcat can access the external network through port 8080, and apache httpd can access the external network through port 80. All that needs to be done now is that the service is accessed through the domain name. It is to proxy the tomcat service through apache httpd.

Therefore, Baidu's various articles and blogs are all in the fog.

 

Here is the official official reason why Apache and Tomcat should be connected:

1. Improve the processing ability of static files;

2. Use web servers for load balancing and fault tolerance;

3. Seamlessly upgrade the app

These three points are very important for a web website. We hope that our website is not only fast, but also stable. It cannot be accessed by users because of a Tomcat downtime or an upgrade program. The only functional and best HTTP server is apache's http server, which is the closest and most reliable in combination with tomcat.

 

There are three ways to integrate Apache and Tomcat:

1. JK

This is the most common way. Most of the ones found on the Internet are also about configuring JK. Of course, the most complete document is its official documentation. JK itself has two versions, 1 and 2, respectively. The latest version of 1 is 1.2.19, and version 2 has long been abandoned, and no new version will be launched in the future, so version 1 is recommended.

JK communicates with the Tomcat server through the AJP protocol. The default port of Tomcat's AJP Connector is 8009. JK itself provides a monitoring and management page jkstatus. Through jkstatus, you can monitor the current working status of JK and set the connection to tomcat, as shown in the following figure:

Figure 1: The monitoring and management page jkstatus


In this figure, we can see that the current JK is equipped with two connections to ports 8109 and 8209 respectively. At present, the connection of s2 is in a stopped state, and the connection of s1 has processed more than 470,000 requests since the last restart. Up to 6.2 G, the maximum concurrent number is 13 and so on. We can also use the management function of jkstatus to switch JK to different Tomcats, such as enabling s2 and disabling s1, which is very useful when updating applications, and the entire switching process is transparent to users. It achieves the purpose of seamless upgrade.

 

There are three most critical files in the configuration of JK, namely:

httpd.conf
Apache server configuration file, used to load JK module and specify JK configuration file information

workers.properties
connection definition file to Tomcat server

uriworkermap.properties
URI mapping file, used to specify which URLs are handled by Tomcat, you can also configure these URIs directly in httpd.conf, but the advantage of these independent configurations is that the JK module will regularly update the contents of the file, allowing us to modify the configuration There is no need to restart the Apache server.

The second and third configuration file names can be customized. The following is a typical httpd.conf configuration for JK

# (httpd.conf)
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so

#
# Configure mod_jk
#
JkWorkersFile conf/workers.properties
JkMountFile conf/uriworkermap.properties
JkLogFile logs/mod_jk.log
JkLogLevel warn

Next, we create two new files in Apache's conf directory: workers.properties and uriworkermap.properties.

The contents of the workers.properties file are roughly as follows:

#
# workers.properties
#
# list the workers by name
worker.list=DLOG4J, status

# localhost server 1
# ------------------------
worker.s1.port=8109
worker.s1.host=localhost
worker.s1.type=ajp13

# localhost server 2
# ------------------------
worker.s2.port=8209
worker.s2.host=localhost
worker.s2.type=ajp13
worker.s2.stopped=1

worker.DLOG4J.type=lb
worker.retries=3
worker.DLOG4J.balanced_workers=s1, s2
worker.DLOG4J.sticky_session=1

worker.status.type=status

The above workers.properties configuration is the configuration used for the page we screen shot earlier. First, we configured two workers of type ajp13, s1 and s2, which point to Tomcat running on two different ports 8109 and 8209 on the same server. Next, we configured a worker of type lb (that is, the meaning of load balancing), its name is DLOG4J, which is a logical worker, which is used to manage the two physical connections s1 and s2 configured earlier. Finally, a worker of type status is configured, which is a module used to monitor JK itself. Having these three workers is not enough, we also need to tell JK which workers are available, so there is the line configuration of worker.list = DLOG4J, status .

 

The next step is the URI mapping configuration. We need to specify which links are processed by Tomcat and which are directly processed by Apache. You can understand the meaning of the configuration by looking at the following file.

/ * = DLOG4J
/jkstatus=status

!/*.gif=DLOG4J
!/*.jpg=DLOG4J
!/*.png=DLOG4J
!/*.css=DLOG4J
!/*.js=DLOG4J
!/*.htm=DLOG4J
!/*.html=DLOG4J

All requests are handled by the DLOG4J worker, with a few exceptions, /jkstatus requests are handled by the status worker. In addition, what does the exclamation mark in front of each line of data in this configuration mean? The exclamation mark indicates that the next URI should not be processed by JK, that is, Apache directly processes all images, css files, js files and static html text files.

 

二、http_proxy

这是利用 Apache 自带的 mod_proxy 模块使用代理技术来连接 Tomcat。在配置之前请确保是否使用的是 2.2.x 版本的 Apache 服务器。因为 2.2.x 版本对这个模块进行了重写,大大的增强了其功能和稳定性。

http_proxy 模式是基于 HTTP 协议的代理,因此它要求 Tomcat 必须提供 HTTP 服务,也就是说必须启用 Tomcat 的 HTTP Connector。一个最简单的配置如下

ProxyPass /images !
ProxyPass /css !
ProxyPass /js !
ProxyPass / http://localhost:8080/

在这个配置中,我们把所有 http://localhost 的请求代理到 http://localhost:8080/ ,这也就是 Tomcat 的访问地址,除了 images、css、js 几个目录除外。我们同样可以利用 mod_proxy 来做负载均衡。

再看看下面这个配置:

ProxyPass /images !
ProxyPass /css ! 
ProxyPass /js !

ProxyPass / balancer://example/
<Proxy balancer://example/>
BalancerMember http://server1:8080/
BalancerMember http://server2:8080/
BalancerMember http://server3:8080/
</Proxy>

配置比 JK 简单多了,而且它也可以通过一个页面来监控集群运行的状态,并做一些简单的维护设置。

图 2:监控集群运行状态


 

三、ajp_proxy

ajp_proxy 连接方式其实跟 http_proxy 方式一样,都是由 mod_proxy 所提供的功能。配置也是一样,只需要把 http:// 换成 ajp:// ,同时连接的是 Tomcat 的 AJP Connector 所在的端口。上面例子的配置可以改为:

ProxyPass /images !
ProxyPass /css ! 
ProxyPass /js !

ProxyPass / balancer://example/
<Proxy balancer://example/>
BalancerMember ajp://server1:8080/
BalancerMember ajp://server2:8080/
BalancerMember ajp://server3:8080/
</Proxy>

采用 proxy 的连接方式,需要在 Apache 上加载所需的模块,mod_proxy 相关的模块有 mod_proxy.so、mod_proxy_connect.so、mod_proxy_http.so、mod_proxy_ftp.so、mod_proxy_ajp.so, 其中 mod_proxy_ajp.so 只在 Apache 2.2.x 中才有。如果是采用 http_proxy 方式则需要加载 mod_proxy.so 和 mod_proxy_http.so;如果是 ajp_proxy 则需要加载 mod_proxy.so 和 mod_proxy_ajp.so这两个模块。

 

三者比较

相对于 JK 的连接方式,后两种在配置上是比较简单的,灵活性方面也一点都不逊色。但就稳定性而言就不像 JK 这样久经考验,毕竟 Apache 2.2.3 推出的时间并不长,采用这种连接方式的网站还不多,因此,如果是应用于关键的互联网网站,还是建议采用 JK 的连接方式。

 

以上三种方式,这里采用ajp_proxy的代理方式来进行。

1.配置Apache配置文件httpd.conf

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf

在末尾添加:

NameVirtualHost *:80
<VirtualHost *:80>  
    ServerName www.linyi.cn
    ErrorLog logs/linyi-error_log  
    CustomLog logs/linyi-access_log common  
    <Directory "/var/www/html/usr/local/apache-tomcat-8.0.43/webapps/">  
        DirectoryIndex index.htm index.jsp index.html  
	Options Indexes FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride All
   </Directory>  
    DocumentRoot /var/www/html/usr/local/apache-tomcat-8.0.43/webapps/
    <IfModule mod_proxy.c>  
        ProxyPass /  ajp://127.0.0.1:8009/  
        ProxyPassReverse / ajp://127.0.0.1:8009/  
    </IfModule>  
</VirtualHost>

centos7下,apache httpd操作指令:

systemctl start httpd.service #启动
systemctl stop httpd.service #停止
systemctl restart httpd.service #重启

systemctl enable httpd.service #开机启动
systemctl disable httpd.service #开机不启动

systemctl status httpd.service  #检查httpd状态

killall httped  #杀掉所有httpd进程

 

2.配置Tomcat配置文件server.xml

[root@localhost ~]# vi /usr/local/apache-tomcat-8.0.43/conf/server.xml

Host节点:

<Host name="www.linyi.cn" appBase="webapps"  
    unpackWARs="true" autoDeploy="true"  
    xmlValidation="false" xmlNamespaceAware="false">  
</Host> 

 

Engine节点:

<Engine name="Catalina" defaultHost="www.linyi.cn">

 

重启tomcat

 

最后,在浏览器中输入:http://www.linyi.cn/就可以得到需要的结果。

 

参考:http://blog.csdn.net/jackome/article/details/36418727

https://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326470360&siteId=291194637