文章目錄
一、安裝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、訪問測試
動靜分離,部署完成!