配置Tomcat监听80端口
修改tomcat的默认端口,将其改为80端口,在Connector port端口指定的地方修改为80端口,wq保存退出
[[email protected] ~]# vim /usr/local/tomcat/conf/server.xml
? <Connector port="80" protocol="HTTP/1.1"
? ? ? ? ? ? ? connectionTimeout="20000"
? ? ? ? ? ? ? redirectPort="8443" />
关闭其他web服务,如nginx的服务,以免出现占用端口
[[email protected] ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
重新启动tomcat服务,tomcat没有nginx类似的服务启动重启脚本,只能将服务先停止然后再启动
[[email protected] ~]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[[email protected] ~]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看tomcat的端口占用,8005端口是tomcat服务的管理端口,只有在这个端口启动监听后tomcat才能够正常提供访问
[[email protected] ~]# netstat -ntlp |grep 80
tcp6 0 0 :::80 :::* LISTEN 19270/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 19270/java ? ? ? ? ?
tcp6 0 0 :::8009 :::* LISTEN 19270/java
```? ? ?
测试通过80端口访问tomcat,curl不指定端口默认会是80端口
[[email protected] ~]# curl -I 192.168.1.234
HTTP/1.1 200
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 18 Aug 2018 22:26:12 GMT
# tomcat虚拟主机
添加tomcat虚拟主机
编辑tomcat的server.xml配置文件,添加一个虚拟主机配置
[[email protected] ~]# vim /usr/local/tomcat/conf/server.xml
<Host name="www.abc.com" appBase=""
? ? ? ? ? ?unpackWARs="true" autoDeplo="true"
? ? ? ? ? ?xmlValidation="false" xmlNamespaceAware="false">
? ? ? <Context path="" docBase="/data/wwwroot/abc/" debug="0"
? ? ? ? ? reloadable="true" crossContext="true"/>
? ? </Host>
server的虚拟主机配置说明
在<Host></Host>这个标签中定义tomcat的虚拟主机,其中docBase和appBase来定义网站文件的存放路径,如果不定义docBase,则默认会认为网站文件存放在appBase/ROOT目录下,如果定义了docBase,则就会以docBase定义的目录为主了,生效的是docBase中定义的目录,如果这步没有定义正确则会遇到404的访问问题
appBase是存放应用的目录,通常是把war包放在该目录下,tomcat会自动解压出一个程序目录出来
使用tomcat部署一个博客项目
首先下载项目的war包,拷贝war包到/usr/local/tomcat/webapps目录当中,让tomcat自动解压出文件目录,把解压的目录改为其他名字。之后删除war包,如下操作
[[email protected] webapps]# cp /usr/local/src/zrlog-2.0.2-5ebb1f3-release.war .
[[email protected] webapps]# ls
docs examples host-manager manager ROOT zrlog-2.0.2-5ebb1f3-release.war
[[email protected] webapps]# mv zrlog-2.0.2-5ebb1f3-release zrlog
[[email protected] webapps]# rm -rf zrlog-2.0.2-5ebb1f3-release.war
[[email protected] webapps]# ls
docs examples host-manager manager ROOT zrlog
之后访问192.168.1.234/zrlog的安装页面,192.168.1.234是服务器本机ip地址
安装界面如下
![](http://i2.51cto.com/images/blog/201808/30/2bd86ede47b4a7e4769939df065f6cdb.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
这里我们需要为zrlog创建名字为zrlog存储数据的数据库,并需要创建zrlog库并给予一定的权限,使用测试用户zrlog登录并验证数据库用户正确性
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on zrlog.* to ‘zrlog‘@127.0.0.1 identified by ‘[email protected]‘ ;
Query OK, 0 rows affected, 1 warning (0.01 sec)
[[email protected] webapps]# mysql -uzrlog -h127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 55
Server version: 5.7.22 Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| zrlog ? ? ? ? ? ? |
+--------------------+
2 rows in set (0.00 sec)
创建后按提示写入连接数据库的用户和密码进行下一步操作
安装后进入的管理控制页面
![](http://i2.51cto.com/images/blog/201808/30/279adb904f5238af3a6c11d7aa43335a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
将web文件目录移动位置,使用定义的域名进行访问,这里将zrlog目录下所有的文件拷贝或移动到指定的网站文件目录下,注意不是拷贝zrlog本身,这和指定的网站目录路径有关
[[email protected] webapps]# ls
docs examples host-manager manager ROOT zrlog
[[email protected] webapps]# cd /data/wwwroot/abc/
[[email protected] abc]# cp -rf /usr/local/tomcat/webapps/zrlog/* .
[[email protected] abc]# ls
admin assets error favicon.ico include META-INF WEB-INF
[[email protected] abc]# pwd
/data/wwwroot/abc
重新启动tomcat,使server.xml中的配置生效
[[email protected] abc]# /usr/local/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[[email protected] abc]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看tomcat启动的端口,tomcat只监听80、8009和8005端口,其他端口则是由网站程序监听的
[[email protected] abc]# netstat -ntlp |grep java
tcp6 0 0 127.0.0.1:45711 ::: LISTEN 20309/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:25711 ::: LISTEN 20309/java ? ? ? ? ?
tcp6 0 0 :::80 ::: LISTEN 20254/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:53080 ::: LISTEN 20276/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:55711 ::: LISTEN 20309/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:8005 ::: LISTEN 20254/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:43080 ::: LISTEN 20276/java ? ? ? ? ?
tcp6 0 0 127.0.0.1:23080 ::: LISTEN 20276/java ? ? ? ? ?
tcp6 0 0 :::8009 :::* LISTEN 20254/java ?
在定义的配置中,Host name是定义的域名,docBase定义的是网站存储的目录,appBase是存储应用的默认目录,如果设置了docBase,则appBase不需要配置
<Host name="www.aaa.com" appBase=""
? ? ? ? ? ?unpackWARs="true" autoDeplo="true"
? ? ? ? ? ?xmlValidation="false" xmlNamespaceAware="false">
? ? ? <Context path="" docBase="/data/wwwroot/abc/" debug="0"
? ? ? ? ? reloadable="true" crossContext="true"/>
? ? </Host>
在windows的host中写入测试解析的主机,在浏览器中访问定义的域名,查看是否能直接通过域名进行访问
为了防止浏览器反馈给我们缓存的页面,需要使用ctrl+F5来进行强制刷新请求,返回访问结果
![](http://i2.51cto.com/images/blog/201808/30/3c852a5aa558e1219236bf5bde6d2d7a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
原文地址:http://blog.51cto.com/8844414/2167048