文章目录
一、安装Nginx
1、安装语言包
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz -C /opt
2、配置编译安装
[root@localhost ~]# cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module //开启stub_ status状态统计模块//
[root@localhost ~]# make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //让系统识别命令
3、检查、启动、重启、停止
[root@localhost ~]# nginx- t //检查
[root@localhost ~]# nginx //启动
[root@localhost ~]# killall -1 nginx //重启
[root@localhost ~]# killall -3 nginx //停止
yum install elinks -y //安装软件
elinks http://localhost //验证访问,会提示welcome语句
4、制作管理角本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start |stop|restart |reload}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig --level 35 nginx on
#访问验证
二、安装tomcat
1、设置环境
#先关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
#首先需要先安装jdk
[root@localhost opt]# tar zxvf jdk-8u91-linux-x64.tar.gz
[root@localhost opt]# mv jdk1.8.0_91/ /usr/local/
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
bin etc games include jdk1.8.0_91 lib lib64 libexec sbin share src
#设置环境变量
[root@localhost local]# vim /etc/profile //编辑系统变量文件
#末行添加三条
export JAVA_HOME=/usr/local/jdk1.8.0_91
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@localhost local]# source /etc/profile //使环境变量生效
#验证
[root@localhost local]# java -version //查看jdk版本(为安装的版本)
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
2、安装并启动Tomcat
#安装包已下载,挂载并解压
[root@localhost opt]# ls
apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz rh
[root@localhost opt]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local
#切换至安装目录并进行优化
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-8.5.16 etc include lib libexec share bin games jdk1.8.0_91 lib64 sbin src
[root@localhost local]# mv apache-tomcat-8.5.16/ tomcat //解压后名字太长,重命名
[root@localhost local]# ls
bin etc games include lib lib64 libexec nginx sbin share src tomcat
[root@localhost local]# cd tomcat/
[root@localhost tomcat]# ls
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
[root@localhost tomcat]# cd bin/
[root@localhost bin]# ls
[root@localhost bin]# ln -s /usr/local/tomcat//bin/startup.sh /usr/bin/ //将开启脚本添加到/usr/bin中,自动识别
[root@localhost bin]# ln -s /usr/local/tomcat//bin/shutdown.sh /usr/bin/ //将关闭脚本添加到/usr/bin中,自动识别
#执行开启脚本
[root@localhost bin]# startup.sh
[root@localhost bin]# netstat -ntap | grep 8080
#访问验证
三、配置动静分离
实现网站动静分离,jsp页面的信息直接由tomcat进行解析处理。
1、nginx配置
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
……省略内容
server {
listen 80;
server_name localhost;
location ~.*.jsp$ { //添加以下内容
proxy_pass http://192.168.100.41:8080; //jsp页面交由Tomcat处理
proxy_set_header Host $host;
}
……省略内容
#创建静态页面,便于验证识别
[root@localhost ~]# vim /usr/local/nginx/html/index.html
[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start
#静态页面测试
2、Tomcat设置
#设置被访问的动态网页内容
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/test
[root@localhost ~]# vim /usr/local/tomcat/webapps/test/index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
</body>
</html>
#动态页面测试
四、nginx处理静态图片,Tomcat处理动态页面配置
- Tomcat指路径,nginx放图片
- 目录名称需要和Java项目名称相同
1、Tomcat配置
[root@localhost ~]# vim /usr/local/tomcat/webapps/test/index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
<img src="1.png"> //添加图片
</body>
</html>
2、nginx配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
……省略内容
server {
listen 80;
server_name localhost;
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { //添加此段内容,匹配这些类型的文件
root html; //html站点目录
expires 30d; //缓存时间30天
}
charset utf-8;
location ~.*.jsp$ {
proxy_pass http://192.168.100.42:8080;
proxy_set_header Host $host;
}
……省略内容
[root@localhost ~]# mkdir /usr/local/nginx/html/test/
[root@localhost ~]# cp /opt/1.png /usr/local/nginx/html/test/
[root@localhost ~]# cd /usr/local/nginx/html/test/
[root@localhost ~]# ls
1.png
3、访问测试
动静分离,部署完成!