找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 58|回复: 0

linux安装Tomcat、Nginx

[复制链接]

26

主题

0

回帖

92

积分

管理员

积分
92
注册时间
2025-10-3
最后登录
2026-5-12
发表于 2026-5-5 12:29:21 | 显示全部楼层 |阅读模式
操作系统使用CentOS

Tomcat的安装

1.更新yum及系统
# 升级所有包和和系统内核
[root@iZbp17oxotxuhf5g2zr3brZ ~]# yum -y update

2.安装JDK
# OpenJDK install
[root@iZbp17oxotxuhf5g2zr3brZ ~]# sudo yum install java-1.8.0-openjdk -y

3.Tomcat下载安装
首先切换到/usr/src目录下
[root@iZbp17oxotxuhf5g2zr3brZ ~]# cd /usr/src

下载
# download
[root@iZbp17oxotxuhf5g2zr3brZ src]# wget https://mirror.bit.edu.cn/apache ... omcat-8.5.57.tar.gz

解压
# tar 命令解压
[root@iZbp17oxotxuhf5g2zr3brZ src]# tar -xvf apache-tomcat-8.5.57.tar.gz

启动
进入到`/usr/src/apache-tomcat-8.5.57/bin`目录中
# 进入到tomcat目录下的bin目录中
[root@iZbp17oxotxuhf5g2zr3brZ bin]# cd /usr/src/apache-tomcat-8.5.57/bin

启动tomcat
[root@iZbp17oxotxuhf5g2zr3brZ bin]# ./startup.sh


查看日志
日志文件存放在tomcat目录下的logs目录中
# 回到logs目录
[root@iZbp17oxotxuhf5g2zr3brZ bin]# cd ..

进入logs目录
[root@iZbp17oxotxuhf5g2zr3brZ apache-tomcat-8.5.57]# cd logs

查看日志
[root@iZbp17oxotxuhf5g2zr3brZ logs]# tail -f catalina.out

关闭
# 关闭 ./shutdon.sh
[root@iZbp17oxotxuhf5g2zr3brZ bin]# ./shutdown.sh

访问
浏览器地址输入:`IP:PORT` 即可访问。如:218.244.158.200:8080

注意:PORT必须在服务器安全组中开放,否则无法访问。


配置域名访问
本文使用Nginx配置域名访问,首先安装Nginx及其Nginx安装所需依赖

1.pcre依赖安装
进入到`/usr/src`目录下
[root@iZbp17oxotxuhf5g2zr3brZ bin]# cd /usr/src

下载pcre依赖
[root@iZbp17oxotxuhf5g2zr3brZ src]# wget http://downloads.sourceforge.net ... 37/pcre-8.37.tar.gz

解压pcre依赖
[root@iZbp17oxotxuhf5g2zr3brZ src]# tar -xvf pcre-8.37.tar.gz

进入解压之后的pcre目录
[root@iZbp17oxotxuhf5g2zr3brZ src]# cd pcre-8.37

编译安装,如果没有安装C++会出现错误`configure: error: You need a C++ compiler for C++ support.`需要先安装C++

安装gcc ,c++
[root@iZbp17oxotxuhf5g2zr3brZ pcre-8.37]# yum install -y gcc gcc-c++

# check
[root@iZbp17oxotxuhf5g2zr3brZ pcre-8.37]# ./configure

# 编译安装
[root@iZbp17oxotxuhf5g2zr3brZ pcre-8.37]# make && make install

# 查看版本
[root@iZbp17oxotxuhf5g2zr3brZ pcre-8.37]# pcre-config --version
8.37

其他依赖安装

通过yum方式安装其他依赖,包括zlib以及openssl,不再使用下载压缩包,编译安装的方式,且自动下载依赖。执行下面一条命令即可。


# yum 一键安装依赖
[root@localhost pcre-8.37]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

安装Nginx
进入到`/usr/src`。通过wget方式下载nginx压缩文件包。

下载nginx
# wget下载nginx压缩包
[root@iZbp17oxotxuhf5g2zr3brZ src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

# 解压
[root@iZbp17oxotxuhf5g2zr3brZ src]# tar -xvf nginx-1.12.2.tar.gz

编译安装
进入到解压之后的nginx目录下,进行编译安装。
# 切换目录
[root@iZbp17oxotxuhf5g2zr3brZ src]# cd nginx-1.12.2

# check
[root@iZbp17oxotxuhf5g2zr3brZ nginx-1.12.2]# ./configure

# 编译安装
[root@iZbp17oxotxuhf5g2zr3brZ nginx-1.12.2]# make && make install

至此,Nginx就安装完成了。安装成功之后,在 usr 目录中 多出来一个目录 local/nginx,在 nginx 目录下 有 sbin 目录,有启动脚本。如下:
[root@iZbp17oxotxuhf5g2zr3brZ nginx-1.12.2]# cd /usr/local
[root@iZbp17oxotxuhf5g2zr3brZ local]# ls
aegis  bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src
[root@iZbp17oxotxuhf5g2zr3brZ local]#

查看nginx版本
在`usr/local/nginx/sbin`目录下执行`./nginx -v`
# 查看版本
[root@iZbp17oxotxuhf5g2zr3brZ sbin]# ./nginx -v
nginx version: nginx/1.12.2

启动nginx
同样在`usr/local/nginx/sbin`目录下操作。

[root@iZbp17oxotxuhf5g2zr3brZ sbin]# ./nginx
[root@iZbp17oxotxuhf5g2zr3brZ sbin]#

查看nginx进程
# 进程查看
[root@iZbp17oxotxuhf5g2zr3brZ sbin]# ps -ef | grep nginx
root      7701     1  0 13:52 ?        00:00:00 nginx: master process ./nginx
nobody    7702  7701  0 13:52 ?        00:00:00 nginx: worker process
root      7710 31503  0 13:54 pts/0    00:00:00 grep --color=auto nginx

由于nginx中默认的端口是80端口,但是出于linux系统安全方面考虑,所以系统中默认没有开放80端口,需要在服务器安全组中开放80端口。否则无法访问。

访问nginx,浏览器地址输入IP即可。

停止nginx
[root@iZbp17oxotxuhf5g2zr3brZ sbin]# ./nginx -s stop

重载Nginx
[root@iZbp17oxotxuhf5g2zr3brZ sbin]# ./nginx -s reload


配置反向代理

在Nginx的配置文件(在/usr/local/nginx/conf路径的nginx.conf)中配置。
[root@localhost /]# cd /usr/local/nginx/conf

使用vi命令打开`nginx.conf`
[root@localhost conf]# vi nginx.conf

将server节点下server_name后的 `localhost`改成`192.168.232.129`

另外在`location`节点中添加一条用于转发的信息:proxy_pass http://127.0.0.1:8080;

重载nginx
# 重启nginx
[root@localhost sbin]# ./nginx -s reload



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Microsoft产品知识分享交流论坛

GMT+8, 2026-5-31 22:14 , Processed in 0.020289 second(s), 23 queries .

Powered by Discuz! X5.0 Licensed

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表