linux CentOS YUM 安装 nginx+tomcat+java+mysql运行环境

Java环境配置

1 安装JDK

查看CentOS自带JDK是否已安装

1
[root@test ~]# yum list installed |grep java

若有自带安装的JDK,应如下操作进行卸载CentOS系统自带Java环境

1
2
3
4
5
6
7
8
9
10
[root@test ~]# java -version
java version "1.7.0_181"
OpenJDK Runtime Environment (rhel-2.6.14.8.el6_9-x86_64 u181-b00)
OpenJDK 64-Bit Server VM (build 24.181-b00, mixed mode)
 
[root@test ~]# yum -y remove java-1.7.0-openjdk*
[root@test ~]# yum -y remove tzdata-java.noarch
 
[root@test ~]# java --version
-bash/usr/bin/java: No such file or directory

查看yum库中的Java安装包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@test ~]# yum -y list java*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: centos.ustc.edu.cn
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
Available Packages
java-1.5.0-gcj.x86_64 1.5.0.0-29.1.el6 base
java-1.5.0-gcj-devel.x86_64 1.5.0.0-29.1.el6 base
java-1.5.0-gcj-javadoc.x86_64 1.5.0.0-29.1.el6 base
java-1.5.0-gcj-src.x86_64 1.5.0.0-29.1.el6 base
java-1.6.0-openjdk.x86_64 1:1.6.0.40-1.13.12.6.el6_8 updates
java-1.6.0-openjdk-demo.x86_64 1:1.6.0.40-1.13.12.6.el6_8 updates
java-1.6.0-openjdk-devel.x86_64 1:1.6.0.40-1.13.12.6.el6_8 updates
java-1.6.0-openjdk-javadoc.x86_64 1:1.6.0.40-1.13.12.6.el6_8 updates
java-1.6.0-openjdk-src.x86_64 1:1.6.0.40-1.13.12.6.el6_8 updates
java-1.7.0-openjdk.x86_64 1:1.7.0.111-2.6.7.2.el6_8 updates
java-1.7.0-openjdk-demo.x86_64 1:1.7.0.111-2.6.7.2.el6_8 updates
java-1.7.0-openjdk-devel.x86_64 1:1.7.0.111-2.6.7.2.el6_8 updates
java-1.7.0-openjdk-javadoc.noarch 1:1.7.0.111-2.6.7.2.el6_8 updates
java-1.7.0-openjdk-src.x86_64 1:1.7.0.111-2.6.7.2.el6_8 updates
java-1.8.0-openjdk.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-debug.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-demo.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-demo-debug.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-devel.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-devel-debug.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-headless-debug.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-javadoc-debug.noarch 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-src.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java-1.8.0-openjdk-src-debug.x86_64 1:1.8.0.101-3.b13.el6_8 updates
java_cup.x86_64 1:0.10k-5.el6 base
java_cup-javadoc.x86_64 1:0.10k-5.el6 base
java_cup-manual.x86_64 1:0.10k-5.el6 base
javacc.x86_64 4.1-0.5.el6 base
javacc-demo.x86_64 4.1-0.5.el6 base
javacc-manual.x86_64 4.1-0.5.el6 base
javassist.noarch 3.9.0-6.el6 base
javassist-javadoc.noarch 3.9.0-6.el6 base

以yum库中java-1.8.0为例, "*"表示将java-1.8.0的所有相关Java程序都安装上

1
[root@test ~]# yum -y install java-1.8.0-openjdk*

查看刚安装的Java版本信息。
输入:"java -version" 可查看Java版本
输入:"javac" 可查看Java的编译器命令用法

2 yum 安装 tomcat

一、环境搭建准备
1、先到tomcat官网https://tomcat.apache.org/查看下载路径

我下载的tomcat-9 如:

http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0.16.tar.gz

linux CentOS YUM 安装  nginx+tomcat+java+mysql运行环境

进入目录:

cd /usr/local/

  

wget下载安装包

wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0.16.tar.gz

解压:

tar -zxvf apache-tomcat-9.0.16.tar.gz

目录说明:

bin             --启动命令目录
conf --配置文件目录 *重点
lib --库文件目录
logs --日志文件目录 *重点
temp --临时缓存文件
webapps --web应用家目录 *重点
work --工作缓存目录

5. 启动Tomcat:

./bin/startup.sh

6 关闭Tomcat:

./bin/shutdown.sh

  

7 开机启动:

在/usr/lib/systemd/system目录下增加tomcat9.service文件,内容如下:

[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target [Service]
Type=oneshot
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes [Install]
WantedBy=multi-user.target

  

退出并保存,执行systemctl enable tomcat使tomcat9随开机启动

systemctl enable tomcat9.service

另外命令

systemctl start tomcat9.service    启动tomcat

systemctl stop tomcat9.service    关闭tomcat

systemctl restart tomcat9.service    重启tomcat

当然启动和结束tomcat也可以到tomcat的安装目录的bin目录下执行shutdown.sh  startup.sh来完成

4、查看tomcat默认端口8080是否开启

linux CentOS YUM 安装  nginx+tomcat+java+mysql运行环境
[root@localhost ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 3832/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 3739/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3378/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 3380/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3655/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 9873/sshd: root@pts
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 6137/sshd: root@pts
tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 10234/sshd: root@pt
tcp6 0 0 :::8009 :::* LISTEN 9804/java
tcp6 0 0 :::3306 :::* LISTEN 7196/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::8080 :::* LISTEN 9804/java
tcp6 0 0 :::6000 :::* LISTEN 3832/X
tcp6 0 0 :::22 :::* LISTEN 3378/sshd
tcp6 0 0 ::1:631 :::* LISTEN 3380/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 3655/master
tcp6 0 0 ::1:6010 :::* LISTEN 9873/sshd: root@pts
tcp6 0 0 ::1:6011 :::* LISTEN 6137/sshd: root@pts
tcp6 0 0 ::1:6012 :::* LISTEN 10234/sshd: root@pt
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 9804/java
linux CentOS YUM 安装  nginx+tomcat+java+mysql运行环境

三、浏览器打开:http://192.168.0.8:8080  出现以下界面说明成功

linux CentOS YUM 安装  nginx+tomcat+java+mysql运行环境

centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库

1.)使用yum安装nginx需要包括Nginx的库,安装Nginx的库

#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.使用下面命令安装nginx

#yum install nginx

3.启动nginx

#service nginx start
或者
#systemctl start nginx.service

4.停止nginx

nginx -s stop

5.重新加载nginx配置文件

nginx -s reload

6.nginx指定项目路径 注:修改配置文件默认用户名为root

linux CentOS YUM 安装  nginx+tomcat+java+mysql运行环境

一、安装Nginx

下载源:wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装源:yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm -y(注意-y参数)

安装Nginx:yum install nginx

启动Nginx服务:service nginx start

停止Nginx服务:service nginx stop

查看Nginx运行状态:service nginx status

检查Nginx配置文件:nginx -t

服务运行中重新加载配置:nginx -s reload

添加Nginx服务自启动:chkconfig nginx on

二、修改防火墙规则

修改Nginx所在主机的防火墙配置:vi /etc/sysconfig/iptables,将nginx使用的端口添加到允许列表中。

例如:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (表示允许80端口通过)

修改Tomcat所在主机的防火墙配置:vi /etc/sysconfig/iptables,将tomcat使用的端口添加到允许列表中。

例如:-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT (表示允许8080端口通过)

如果主机上有多个tomcat的话,则按此规则添加多条,修改对应的端口号即可。

保存后重启防火墙:service iptables restart

三、Tomcat负载均衡配置

Nginx启动时默认加载配置文件/etc/nginx/nginx.conf,而nginx.conf里会引用/etc/nginx/conf.d目录里的所有.conf文件。

因此可以将自己定制的一些配置写到单独.conf文件里,只要文件放在/etc/nginx/conf.d这个目录里即可,方便维护。

创建tomcats.conf:vi /etc/nginx/conf.d/tomcats.conf,内容如下:

upstream tomcats {
ip_hash;
server 192.168.0.251:8080;
server 192.168.0.251:8081;
server 192.168.0.251:8082;
}

修改default.conf:vi /etc/nginx/conf.d/default.conf,修改如下:

#注释原有的配置
#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#} #新增配置默认将请求转发到tomcats.conf配置的upstream进行处理
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcats; #与tomcats.conf里配置的upstream同名
}

保存后重新加载配置:nginx -s reload

四、静态资源分离配置

修改default.conf:vi /etc/nginx/conf.d/default.conf,添加如下配置:

#所有js,css相关的静态资源文件的请求由Nginx处理
location ~.*\.(js|css)$ {
root /opt/static-resources; #指定文件路径
expires 12h; #过期时间为12小时
}
#所有图片等多媒体相关静态资源文件的请求由Nginx处理
location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
root /opt/static-resources; #指定文件路径
expires 7d; #过期时间为7天
}

五、修改SELinux安全规则

如果访问Nginx时出现502 Bad Gateway错误,则可能是Nginx主机上的SELinux限制了其使用http访问权限引起的,输入命令setsebool -P httpd_can_network_connect 1 开启权限即可。

文件/etc/nginx/nginx.conf完整配置如下:

user  nginx;
worker_processes auto; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 100000; events {
use epoll;
multi_accept on;
worker_connections 1024;
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log /var/log/nginx/access.log main; sendfile on;
server_tokens off;
#tcp_nopush on; keepalive_timeout 65; gzip on;
gzip_disable "msie6";
gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; include /etc/nginx/conf.d/*.conf;
}

文件/etc/nginx/conf.d/default.conf完整配置如下:

server {
listen 80;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main; #location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#} location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_servers;
} location ~.*\.(js|css)$ {
root /opt/static-resources;
expires 12h;
} location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
root /opt/static-resources;
expires 7d;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

注意:如果执行命令时没有root权限,请在命令前面加上 sudo 执行。

bin             --启动命令目录
conf --配置文件目录 *重点
lib --库文件目录
logs --日志文件目录 *重点
temp --临时缓存文件
webapps --web应用家目录 *重点
work --工作缓存目录
上一篇:python3csv与xlsx文件操作模块(csv、xlsxwriter)


下一篇:java基础篇 -- 导出excel表格数据