天天看點

nas部署java_nginx+tomcat+nas

第一步:部署nas節點

[[email protected] ~]# df -Th

[[email protected] ~]# fdisk /dev/vda---------------------------------------------->1.檢視磁盤2.建立擴充分區四3.添加邏輯分區五六七各500M

[[email protected] ~]# yum install mdadm

[[email protected] ~]# mdadm -C /dev/md0 -l 5 -n 3 /dev/vda{5,6,7}----------------->建立raid5

[[email protected] ~]# mdadm -Ds

[[email protected] ~]# mdadm -Ds >> /etc/mdadm.conf

[[email protected] ~]# vim /etc/mdadm.conf----------------------------------------->軟raid永久生效

DEVICE /dev/vda5 /dev/vda6 /dev/vda7

[[email protected] ~]# rpm -qf `which pvcreate`

[[email protected] ~]# yum install lvm2-------------------------------------------->安裝邏輯卷管理(Logical Volume Manager,LVM)

[[email protected] ~]# pvcreate /dev/md0------------------------------------------->添加實體卷(Physical Volume,PV)

[[email protected] ~]# vgcreate vg0 /dev/md0--------------------------------------->添加卷組(Volume Group,VG)

[[email protected] ~]# lvcreate -L 800M -n /dev/vg0/lv0 vg0------------------------>添加邏輯卷(Logical Volume,LV)

[[email protected] ~]# mkfs.xfs /dev/vg0/lv0--------------------------------------->格式化

[[email protected] ~]# mkdir /webdir

[[email protected] ~]# mount /dev/vg0/lv0 /webdir---------------------------------->挂載檔案

[[email protected] ~]# vim /etc/rc.local------------------------------------------->開機自起

mount /dev/vg0/lv0 /webdir

[[email protected] ~]# ll /etc/rc.local

[[email protected] ~]# chmod +x /etc/rc.local-------------------------------------->加執行權限

[[email protected] ~]# yum install nfs-utils--------------------------------------->安裝網絡檔案系統(Network File System,NFS)

[[email protected] ~]# vim /etc/exports

#/webdir *(ro)

#把webdir檔案以隻讀方式共享給所有人

/webdir 192.168.122.0/24(rw,no_root_squash,sync)-------------------------->讀寫權限共享給這個網段

[[email protected] ~]# ll /webdir

[[email protected] ~]# ll /webdir -d----------------------------------------------->顯示目錄自身屬性

drwxr-xr-x 2 root root 6 10月 27 12:52 /webdir/

[[email protected] ~]# chown -R nfsnobody.nfsnobody /webdir/

[[email protected] ~]# ls -ld /webdir/

drwxr-xr-x 2 nfsnobody nfsnobody 6 10月 27 12:52 /webdir/

[[email protected] ~]# systemctl restart nfs

#去tomcat節點安裝nfs,并檢視是否能連通

[[email protected] ~]# yum search showmount

[[email protected] ~]# yum install nfs-utils

[[email protected] ~]# yum install nfs-utils

[[email protected] ~]# showmount -e 192.168.122.200

[[email protected] ~]# mount -t nfs 192.168.122.200:/webdir /mnt

[[email protected] ~]# umount /mnt--------------------------------------------->測試遠端挂載是否成功

第二步:部署資料庫

[[email protected] ~]# yum install mariadb-server mariadb-------------------------->安裝資料庫服務端

[[email protected] ~]# yum install mariadb----------------------------------------->安裝資料庫用戶端

[[email protected] ~]# yum install mariadb

[[email protected] ~]# systemctl restart mariadb

[[email protected] ~]# mysql

[[email protected] ~]# /usr/bin/mysqladmin -u root password '123'------------------>設定密碼

[[email protected] ~]# mysql -u root -p'123'

MariaDB [(none)]> create database sns;

#建立庫sns

MariaDB [(none)]> grant all on sns.* to [email protected]'192.168.122.%' IDENTIFIED BY '123';

#授權sns庫給這個網段的所有使用者(grant 權限 on 資料庫.* to 使用者名@登入主機 identified by “密碼”;)

exit

[[email protected] ~]# mysql -u sns -p123 -h 192.168.122.200

MariaDB [(none)]> show databases;

[[email protected] ~]# mysql -u sns -p123 -h 192.168.122.200------------------->遠端登陸資料庫,測試連接配接是否正常

MariaDB [(none)]> show databases;

第三步:安裝JAVA(tomcat需求)

[[email protected] or 2 ~]# tar -xvf jdk-8u144-linux-x64.tar.gz -C /usr/local/------>解壓檔案到這個目錄下

[[email protected] or 2 ~]# mv /usr/local/jdk1.8.0_144/ /usr/local/java------------->改個名字

[[email protected] or 2 ~]# vim /etc/profile---------------------------------------->進此檔案配置環境變量

JAVA_HOME=/usr/local/java

PATH=$JAVA_HOME/bin:$PATH

export JAVA_HOME PATH

[[email protected] or 2 ~]# source /etc/profile------------------------------------->加載環境變量

[[email protected] or 2 ~]# env | grep JAVA----------------------------------------->檢視是否生效

[[email protected] or 2 ~]# java -version------------------------------------------->檢視JAVA版本,是否加載成功

[[email protected] or 2 ~]# tail -3 /etc/profile

JAVA_HOME=/usr/local/java

PATH=$JAVA_HOME/bin:$PATH

export JAVA_HOME PATH

第四步:部署tomcat

[[email protected] or 2 ~]# tar -xvf apache-tomcat-9.0.1.tar.gz -C /usr/local/

[[email protected] or 2 ~]# cd /usr/local/

[[email protected] or 2 ~]# ln -s apache-tomcat-9.0.1/ tomcat----------------------->做一個軟連接配接

[[email protected] or 2 ~]# vim /etc/profile

JAVA_HOME=/usr/local/java

PATH=$JAVA_HOME/bin:$PATH

CATALINA_HOME=/usr/local/tomcat

export JAVA_HOME PATH CATALINA_HOME

[[email protected] or 2 ~]# source /etc/profile

[[email protected] or 2 ~]# /usr/local/tomcat/bin/startup.sh------------------------>啟動tomcat

[[email protected] or 2 ~]# netstat -anplt------------------------------------------>檢視端口是否啟動成功

網頁測試http://192.168.122.10:8080/--------------------------------------------->顯示tomcat測試網頁

-------http://192.168.122.20:8080/

測試成功後

第五步:tomcat和nas結合(測試版)

[[email protected] or 2 ~]# mount 192.168.122.200:/webdir /usr/local/tomcat/webapps/ROOT/

[[email protected] or 2 ~]# echo tomcat1_2 > /usr/local/tomcat/webapps/ROOT/index.html

網頁測試http://192.168.122.10:8080/--------------------------------------------->顯示tomcat1_2

-------http://192.168.122.20:8080/--------------------------------------------->顯示tomcat1_2

第六步:部署nginx

[[email protected] ~]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[[email protected] ~]# rpm -ivh epel-release-latest-7.noarch.rpm

[[email protected] ~]# yum makecache

[[email protected] ~]# yum install  nginx

[[email protected] ~]# vim /usr/share/nginx/html/index.html--------------------------->nginx首頁(rpm安裝一般都是在/usr/share目錄下)

[[email protected] ~]# vim /etc/nginx/nginx.conf

http {

..

..

include /etc/nginx/conf.d/*.conf;

upstream tomcatlb {------------------------------------------------------>添加的内容

server 192.168.122.10:8080;---------------------------------------->添加的内容

server 192.168.122.20:8080;---------------------------------------->添加的内容

#算法解釋

#server 192.168.122.20:8080 down;---------------------------------->不參與負載

#server 192.168.122.20:8080 weight=2;------------------------------>權重兩次

#server 192.168.122.10:8080 weight=1;------------------------------>權重一次

#server 192.168.122.10:8080 max_fails=3 fail_timeout=20;----------->連續三次啟動失敗,在20秒内不參與負載

#server 192.168.122.100:80 backup;--------------------------------->當所有網頁都當機了,将本紀做為備用網頁機(内容可寫:正在搶修中)

#nginx負載均衡5種配置方式1.輪詢(預設什麼都不寫)2.權重weight 3.ip_hash相當于lvs裡的sh 4.fair(第三方)按響應時間來配置設定請求lvs裡的sed 5.url_hash(第三方)

}----------------------------------------------------------------------------------------------------------------------->添加的内容

server {

..

..

location / {

proxy_pass http://tomcatlb;--------------------------->添加的内容

proxy_set_header Host $host;--------------------------->添加的内容

}

..

..

}

..

..

}

[[email protected] ~]# systemctl restart nginx

測試

[[email protected] ~]# elinks --dump 192.168.122.100

tomcat2

[[email protected] ~]# elinks --dump 192.168.122.100

tomcat1

[[email protected] ~]# elinks --dump 192.168.122.100

tomcat2

[[email protected] ~]# elinks --dump 192.168.122.100

tomcat1

[[email protected] ~]# elinks --dump 192.168.122.100

tomcat2