Centos + apache + tomcat 配置实录,httpd调用servlet

Centos + apache + tomcat 配置实录,httpd调用servlet

初学java,搭建java服务器,费了几天功夫,供自己以后查看,也供初学者参考。


总体配置Centos7,java-1.8.0,apache2.4.6,tomcat 8.5.30,tomcat-connector 1.2.43.

防火墙服务开启并开启自己需要的端口:80
systemctl start firewalld
firewall-cmd --zone=public --permanent --add-port=80/tcp
firewall-cmd --zone=public --permanent --add-port=8012/tcp //测试用的

事先把tar包都放到/usr/local 了。

1)安装java-1.8.0
tar -zxvf jdk-8u271-linux-x64.tar.gz 
//目录:/etc/java/
配置环境
export JAVA_HOME=/etc/java/jdk
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
2)安装apache-2.4.6
yum install htttpd-devel -y
yum install dnf -y
yum install httpd -y
//目录 /etc/httpd
3)安装tomcat-8.5.30
tar -zxvf apache-tomcat-8.5.30.tar.gz
把文件夹重命名tomcat
mv apache-tomcat-8.5.30 tomcat
cp tomcat /ect/   //放到/etc/tomcat目录
配置个serive:tomcat.service 内容如下:
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/etc/java/jdk
Environment=CATALINA_PID=/etc/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/etc/tomcat
Environment=CATALINA_BASE=/etc/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/etc/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=root
Group=root
[Install]
WantedBy=multi-user.target
放到/etc/systemd/system中

4)安装tomcat-connector-1.2.43
tar -zxvf tomcat-connectors-1.2.43-src.tar.gz
cd apache-tomcat-8.5.30/native
./configure --with-apxs=/usr/bin/apxs
#添加rpm通信
sudo  dnf install redhat-rpm-config
make
make install
上述步骤完成后在native/apache-2.0目录下会产生一个mod_jk.so文件,把这个文件复制到Apache目录下的modules目录下
cp mod_jk.so /etc/httpd/modules/

5)在/etc/httpd/conf目录下创建两个文件:
mod_jk.conf
内容如下:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk logs
JkLogFile /etc/httpd/logs/mod_jk.log
# Set the jk log level [debug/error/info]                      
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions indicate to send SSL KEY SIZE, 
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
JkMount /WEB-INF worker1
JkMount /*.action worker1
JkMount /*.jsp worker1
JkMount /application/* worker1
JkMount /*.do worker1
JkMount /*.class worker1
JkMount /servlet/*  worker1

workers.properties内容如下
# Defining a worker named worker1 and of type ajp13
workers.tomcat_home=/etc/tomcat
workers.java_home=$JAVA_HOME
ps=/
worker.list=worker1
# Set properties for worker1
worker.worker1.port=8009
worker.worker1.host=127.0.0.1
worker.worker1.type=ajp13
worker.worker1.lbfactor=5
结束。

6)修改/etc/httpd/conf/httpd.conf:
//把默认url修改到和tomcat一致
DocumentRoot "/etc/tomcat/webapps/test"
<Directory "/etc/tomcat/webapps/test">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
//结尾再添加内容:
Include /etc/httpd/conf/mod_jk.conf

7)修改/etc/tomcat/conf/server.xml
//8009端口和workers.properties 端口一致
<Connector port="8009" redirectPort="8443" protocol="AJP/1.3" secretRequired="false" address="localhost"/>
//重定向url默认目录
-<Host name="localhost" autoDeploy="true" unpackWARs="true" appBase="webapps">
<!--下面这句-->
<Context debug="0" reloadable="true" docBase="/etc/tomcat/webapps/test" path=""/>
<!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html -->
<!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%h %l %u %t "%r" %s %b" suffix=".txt" prefix="localhost_access_log" directory="logs"/>
</Host>

8)/etc/tomcat/webapps/test 如下文件
WEB-INF //文件夹
index.html
test.html
test.jsp  //显示时间代码如下
<html>
<body>
    <center>Now time is: <%=new java.util.Date()%></center>
</body>
</html>

WEB-INF下:
classes //文件夹里有个 hi.class内容如下:
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hi")
public class hi extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello tom!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }

    public void destroy() {
    }
}
web.xml //内容如下:
<?xml v
ersion="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
    <description> Servlet Example. </description>
    <display-name> MyServlet Example </display-name>
    <request-character-encoding>UTF-8</request-character-encoding>
    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>hi</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/servlet/hi</url-pattern>
    </servlet-mapping>
</web-app>


9)最后
cd /etc/tomcat
chmod -R o+x webapps 
给其他用户添加执行权限

10)测试
xxx.xxx.xxx.xxx/test.html
xxx.xxx.xxx.xxx/test.jsp
xxx.xxx.xxx.xxx/servlet/hi 
好像web.xml是必要的,不然 xxx.xxx.xxx.xxx/hi 是可以成功的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值