背景: 最近幾天一直在琢磨Nginx反向代理以及使用Redis保存session,由於本人對java開發比較熟悉,因此在閒暇之餘將公司的一個系統在虛擬機上搭建一個集羣。特此總結過程。java
一.須要使用的一些Linux命令。node
1.1. 查看端口占用程序mysql
netstat -tunlp |grep 22linux
1.2. 查看服務的pidnginx
ps -ef |grep tomcatweb
1.3. 根據對應端口殺死進程redis
kill pid(找到端口號信息後經過pid)sql
1.4. 查看防火牆的狀態vim
service iptables status後端
1.5. 開啓防火牆
chkconfig iptables on
/etc/init.d/iptables start
service iptables start
1.6. 關閉防火牆
chkconfig iptables off
/etc/init.d/iptables stop
service iptables stop
1.7. 開啓80端口
vi /etc/sysconfig/iptables
添加
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
1.8. 禁止指定ip訪問80端口
iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP
1.9. 只打開22端口
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp –sport 22 -j ACCEPT
1.10 參數講解:
–A 參數就當作是添加一條規則
–p 指定是什麼協議,咱們經常使用的tcp 協議,固然也有udp,例如53端 口的DNS
–dport 就是目標端口,當數據從外部進入服務器爲目標端口
–sport 數據從服務器出去,則爲數據源端口使用
–j 就是指定是 ACCEPT -接收 或者 DROP 不接收
二.須要安裝的軟件
Mysql5.1+JDK1.7+Tomcat7+Redis3.0.7+Nginx1.8.1
三.環境
Centos6.5
四.安裝JDK1.7
4.1.1. 列出全部jdk版本
yum list java*
4.1.2. 安裝
yum install java-1.7.0-openjdk.x86_64
4.2.1. 查看自帶版本
rpm -qa|grep jdk
4.2.2. 安裝
rpm -ivh jdk-7u40-linux-i586.rpm
4.3.1. 配置環境變量
vim ~/.bashrc;vim /etc/profile
配置過程略
五.安裝Mysql
1.系統自帶mysql版本太低
yum update 更新試試沒有放棄吧
2.經過wget下載後rpm安裝
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.11-1.el7.x86_64.rpm-bundle.tar(能夠去官網下載)
tar -xvf 解壓後rpm -ivh 安裝
六. Mysql 初始化密碼
My.cnf是mysql的配置文件能夠配置mysql的字符集,大小寫敏感等,還能夠加入
[mysqld]
skip-grant-tables
重啓服務進入無密碼方式進入mysql
use user;
UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
FLUSH;
quit;
七. 安裝Redis
1. 下載
wget http://download.redis.io/releases/redis-3.0.7.tar.gz
2. 解壓
tar -zxvf redis-3.0.7.tar.gz
3. 安裝
1. cd redis-3.0.7
2.ll
已經有Makefile,直接能夠用make安裝
make&&make install
再試試: make MALLOC=libc
redis-cli
八. 將Tomcat的session存儲到redis中已達到在集羣下用戶session的數據一致性。
1. 將所需的jar導入tomcat7的bin中
commons-pool2-2.2.jar
jedis-2.5.2.jar
tomcat-redis-session-manager-2.0.0.jar
2. 在./conf/context.xml作如下配置
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- tomcat-redis-session共享配置 -->
<Valve className="com.orangefunction.tomcat.redissessions
.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions
.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60" />
<!----------------------------- -->
</Context>
以上配置參數很容易明白什麼意思。配置完成後最好重啓tomcat7看看日誌信息瞭解是否配置成功。
九. 下載安裝nginx-1.8.1穩定版(能夠去官網找到下載路徑)
1.下載安裝包
wget http://nginx.org/download/nginx-1.8.1.tar.gz
2. 解壓
tar-zxvf nginx-1.8.1.tar.gz
3. 安裝
能夠看到裏面有個configuer文件那麼直接編譯安裝便可。
./configure
錯誤緣由沒有找到pcre-**須要指定pcre路徑
查找:find/-name fileName沒有找到須要安裝
yum remove pcre*
yum list pcre*
yum install pcre*
make && make install
安裝完成
十.配置啓動
1. 啓動nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2.查看是否啓動
ps -ef | grep nginx
或瀏覽器訪問http://127.0.0.1
3. 關閉nginx
ps -ef |grep nginx
kll 5578(殺掉進程)
4. 配置nginx實現反向代理
vim /usr/local/nginx/conf/nginx.conf
下面是配置信息:
#Nginx所用用戶和組,window下不指定
#user maybo maybo;
#工做的子進程數量(一般等於CPU數量或者2倍於CPU)
worker_processes 2;
#錯誤日誌存放路徑
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#指定pid存放文件
pid logs/nginx.pid;
events {
#使用網絡IO模型linux建議epoll,FreeBSD建議採用kqueue,window下不指定。
#use epoll;
#容許最大鏈接數
worker_connections 2048;
}
http {
include 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 off;
access_log logs/access.log;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 75 20;
include gzip.conf;
upstream localhost {
#根據ip計算將請求分配各那個後端tomcat,許多人誤認爲能夠解決session問題,其實並不能。
#同一機器在多網狀況下,路由切換,ip可能不一樣
#ip_hash;
server localhost:8080;
server localhost:8088;
}
server {
listen 80;
server_name localhost;
location / {
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://localhost;
}
}
}
以上是配置文件內容
5. 測試文件的正確性
nginx -t -c /usr/local/nginx/conf/nginx.conf
6. 在從新啓動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf