背景: 最近幾天一直在琢磨Nginx反向代理以及使用Redis保存session,因為本人對java開發比較熟悉,所以在閑暇之余將公司的一個系統在虛擬機上搭建一個集群。特此總結過程。
一.需要使用的一些Linux命令。
1.1. 查看端口占用程序
netstat -tunlp |grep 22
1.2. 查看服務的pid
ps -ef |grep tomcat
1.3. 根據對應端口殺死進程
kill pid(找到端口號信息后通過pid)
1.4. 查看防火牆的狀態
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
