下面是详解CentOS7.0安装与配置Tomcat-7的完整攻略:
安装Java
- 使用yum安装Java:
sudo yum install java
- 验证Java是否安装成功:
java -version
下载与安装Tomcat
- 下载Tomcat7:
wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.109/bin/apache-tomcat-7.0.109.tar.gz
- 解压Tomcat7:
tar xvf apache-tomcat-7.0.109.tar.gz
- 将Tomcat目录移动至/usr/local/tomcat7:
sudo mv apache-tomcat-7.0.109 /usr/local/tomcat7
- 修改Tomcat的运行权限:
sudo chmod +x /usr/local/tomcat7/bin/*.sh
配置Tomcat
- 添加Tomcat用户:
sudo useradd -r -m -U -d /usr/local/tomcat7 -s /bin/false tomcat7
- 修改Tomcat目录的所有权:
sudo chown -R tomcat7: /usr/local/tomcat7
- 创建systemd启动服务文件:
sudo vim /etc/systemd/system/tomcat7.service
将以下内容粘贴进文件中:
[Unit]
Description=Tomcat 7
After=network.target
[Service]
Type=forking
User=tomcat7
Group=tomcat7
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
Environment=CATALINA_PID=/usr/local/tomcat7/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat7
Environment=CATALINA_BASE=/usr/local/tomcat7
ExecStart=/usr/local/tomcat7/bin/startup.sh
ExecStop=/usr/local/tomcat7/bin/shutdown.sh
Restart=on-abort
[Install]
WantedBy=multi-user.target
- 重新加载systemd:
sudo systemctl daemon-reload
- 启动Tomcat:
sudo systemctl start tomcat7.service
- 验证Tomcat是否成功安装并运行,可以在浏览器中输入以下地址进行访问:
http://localhost:8080/
示例
以下是两个示例:
示例一:在Tomcat中部署一个简单的计算器Web应用
- 下载计算器Web应用:
wget https://github.com/jbrower95/tomcatcalc/raw/master/tomcatcalc.war
- 将计算器Web应用部署至Tomcat中:
sudo cp tomcatcalc.war /usr/local/tomcat7/webapps/
- 重启Tomcat:
sudo systemctl restart tomcat7.service
- 在浏览器中输入以下地址进行访问:
http://localhost:8080/tomcatcalc/
现在就可以使用这个Web应用进行简单的计算啦!
示例二:使用Java开发一个Hello World Web应用并部署至Tomcat
-
创建一个新项目,例如:HelloWorldWeb
-
在src/main/java目录下创建一个HelloWorldServlet.java文件,内容如下:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorldServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Hello World!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- 在src/main/webapp/WEB-INF目录下创建一个web.xml文件,内容如下:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
<display-name>HelloWorldWebApp</display-name>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
- 打包应用:
mvn package
- 将打包好的应用部署至Tomcat中:
sudo cp HelloWorldWeb.war /usr/local/tomcat7/webapps/
- 重启Tomcat:
sudo systemctl restart tomcat7.service
- 在浏览器中输入以下地址进行访问:
http://localhost:8080/HelloWorldWeb/hello
现在就可以看到浏览器中输出了“Hello World!”这句话啦!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解CentOS7.0安装与配置Tomcat-7 - Python技术站