環境信息
centos7.2
安裝目錄
MYSQL安裝
JDK安裝
ACTIVEMQ安裝
防火牆和白名單配置
Tomcat下載與安裝
Redis下載與安裝
nginx安裝與配置實現負載均衡
MYSQL安裝
1. 安裝前工作
在安裝前需要確定現在這個系統有沒有 mysql,如果有那麼必須卸載(在 centos7 自帶的是 mariaDb 數據庫,所以第一步是卸載數據庫)。
卸載系統自帶的Mariadb:
查看mariadb數據庫:
rpm -qa | grep mariadb
*卸載mariadb數據庫:*
rpm -e --nodeps mariadb文件名
卸載已安裝的mysql:
查看 mysql 數據庫:
rpm -qa | grep -i mysql
卸載 mysql 數據庫:
rpm -e mysql文件名 # 如果有關聯文件,不能直接卸載。可以用一下命令強制卸載:rpm -e –nodeps mysql文件名)
刪除etc目錄下的my.cnf文件:
rm /etc/my.cnf
2. 安裝MYSQL步驟
mysql官方(http://dev.mysql.com/downloads/repo/yum/)獲取Yum Repository。
獲取rpm鏡像文件
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
md5校驗文件
md5sum mysql57-community-release-el7-11.noarch.rpm ##md5校驗
安裝mysql
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
yum -y install mysql-server
設置mysql爲自啓動
systemctl enable mysqld #設置開機啓動
systemctl disable mysqld #關閉開機啓動
重啓mysql服務
#
service mysqld restart [start, stop, restart, try-restart, reload, force-reload, status] #後面的都是可選項
查看3306端口是否啓動
netstat -ln | grep 3306
首次查看自動產生的隨即密碼
grep "paasword" /var/log/mysqld.log
或者使用
cat /var/log/mysqld.log | grep password
修改密碼策略(支持簡單密碼)也可以看下一步的配置文件關閉密碼插件
set global validate_password_policy=0; ###基於密碼長度(修改爲LOW方式)
set global validate_password_length=1 ###
修改配置文件(vim /etc/my.cnf)
編輯配置文件
vim /etc/my.cnf
插入以下文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password=off
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
登錄並修改密碼
登錄
mysql -uroot -p
修改密碼
set password=password('新密碼');
# 或者set PASSWORD = PASSWORD('新密碼'); 也可以
生效
flush privileges; #刷新
添加遠程訪問權限
#登錄mysql
mysql> use mysql
mysql> update user set host='%' where user='root';
mysql> select host,user from user;
mysql> flush privileges;
#退出mysql,重啓mysql服務生效
JAVA安裝
1. 安裝前工作
檢查版本,卸載openjdk
java -version #查看jdk安裝版本
rpm -qa | grep java #查看安裝的包信息
#卸載上面查到的安裝包,卸載完
[root@localhost ~]# rpm -e --nodeps tzdata-java-2014i-1.el7.noarch
[root@localhost ~]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.71-2.5.3.1.el7_0.x86_64
[root@localhost ~]# rpm -e --nodeps java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el7_0.x86_64
上傳下載的jdk或者wget
rz #進行上傳(也可以自己使用ftp上傳)
或者
wget http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.rpm #jdk1.8版本的
安裝jdk
rpm -ivh jdk-8u181-linux-x64.rpm
驗證安裝
java -version
配置環境變量
vi /etc/profile
追加信息
JAVA_HOME=/usr/java/jdk1.8.0_25
JRE_HOME=/usr/java/jdk1.8.0_25/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
使修改生效
[root@localhost ~]# source /etc/profile //使修改立即生效
[root@localhost ~]# echo $PATH //查看PATH值
查看系統環境狀態
[root@localhost ~]# echo $PATH
ACTIVEMQ安裝
1. 安裝前工作
下載
使用此插件進行上傳剛剛上一步(下載activeMQ傳送門)下載的文件
rz
或者直接卸載(5.15.5)版本
wget http://apache.fayea.com/activemq/5.15.5/apache-activemq-5.15.5-bin.tar.gz
解壓
tar -zxvf apache-activemq-5.15.5-bin.tar.gz
啓動
進入bin目錄
[root@iz2ze7qyvg1mht4uptlc5uz bin]# pwd
/usr/local/java/apache-activemq-5.15.4/bin
./activemq start
查看啓動情況(進程)
ps -ef | grep activemq
防火牆和白名單配置
iptables防火牆
安裝防火牆
yum -y install iptables-services
編輯防火牆的配置文件
vi /etc/sysconfig/iptables
基本操作
查看防火牆狀態
service iptables status
停止防火牆
service iptables stop
啓動防火牆
service iptables start
重啓防火牆
service iptables restart
永久關閉防火牆
chkconfig iptables off
永久關閉後重啓
chkconfig iptables on
禁止iptables開機啓動
systemctl disable iptables.service
設置防火牆開機啓動
systemctl enable iptables.service
查看防火牆狀態
service iptables status
增加開放端口
這個需要看看配置文件的位置
/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
或者
vim /etc/sysconfig/iptables
# 加入如下代碼,比着兩葫蘆畫瓢 :)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
保存後重啓防火牆
service iptables restart
firewall防火牆
基本操作
查看狀態systemctl status firewalld
或者
firewall-cmd –state
開啓
service firewalld start
重啓
service firewalld restart
關閉
service firewalld stop
查看防火牆規則
firewall-cmd –list-all
禁止firewall開機啓動
systemctl disable firewalld.service
設置防火牆開機啓動
systemctl enable firewalld.service
端口操作
#查詢端口是否開放
firewall-cmd --query-port=8080/tcp
#開放80端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
#移除端口
firewall-cmd --permanent --zone=public --remove-port=8080/tcp
查看配置文件的端口開放
cat /etc/firewalld/zones/public.xml
重啓防火牆
#重啓防火牆(修改配置後要重啓防火牆)
firewall-cmd --reload
參數解釋
1. firwall-cmd:是Linux提供的操作firewall的一個工具;
2. --permanent:表示設置爲持久;
3. --add-port:標識添加的端口;
tomcat下載與安裝
1. 安裝前工作
下載
wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.32/bin/apache-tomcat-8.5.32.tar.gz
解壓
tar -xzvf apache-tomcat-9.0.2.tar.gz
修改系統配置
[root@JDK-Tomcat apache-tomcat-9.0.2]# vi /etc/profil
export TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.5.32/
[root@JDK-Tomcat bin]# source /etc/profile
設置Tomcat開機自啓動
在Tomcat的bin下面的catalina.sh添加以下配置
export JAVA_HOME=/usr/java/jdk1.8.0_151
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export PATH=$JAVA_HOME/bin
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.32/bin/catalina.sh start
#編輯/etc/rc.d/rc.local配置文件
/usr/local/tomcat/apache-tomcat-8.5.32/bin/startup.sh
#給/etc/rc.d/rc.local加上執行權限,這個很重要,別忘了!否則不會執行!
chmod u+x /etc/rc.d/rc.local
優化
#在/usr/local/tomcat/apache-tomcat-8.5.32/bin/catalina.sh新增如下一行:
JAVA_OPTS="-XX:MaxPermSize=192M -Xms1000M -Xmx2000M"
驗證
關閉服務器,重啓觀察是否正常開機自啓動?
redis下載與安裝
1. 安裝前工作
下載
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
配置編譯環境:
sudo yum install gcc-c++
解壓
tar -xzvf redis-4.0.10.tar.gz
編譯
#進入redis的解壓目錄
make install
編輯配置文件
vim /usr/local/redis/redis.conf
① 在bind 127.0.0.1修改爲bind 0.0.0.0(設置爲可以遠程訪問)
② 默認爲保護模式,把 protected-mode yes 改爲 protected-mode no
③ 默認爲不守護進程模式,把daemonize no 改爲daemonize yes(後臺啓動)
④ 將 requirepass foobared前的“#”去掉,密碼改爲你想要設置的密碼(我爲了練習用,設置爲123456)
⑤ 可以改rdb的文件位置dir ./改爲 dir /usr/redis/db(你的rdb目錄)
⑥ 可以改redis的日誌文件位置logfile "",改爲logfile "redis6379.log"
⑦ 保存
跟隨系統啓動
./usr/local/java/redis-4.0.10/utils/install_server.sh
Redis服務查看、開啓、關閉:
a.通過ps -ef|grep redis命令查看Redis進程
b.開啓Redis服務操作通過/etc/init.d/redis_6379 start命令,也可通過(service redis_6379 start)
c.關閉Redis服務操作通過/etc/init.d/redis_6379 stop命令,也可通過(service redis_6379 stop)
d.重啓 /etc/init.d/redis_6379 restart
創建redis的軟連接
# 相當於超鏈接
ln -s /usr/local/redis/redis-cli /usr/bin/redis
端口開發
vim /etc/sysconfig/iptables
增加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
重啓
systemctl restart iptables.service
驗證
不報錯就可以了
telnet ip 6379
nginx安裝與配置實現負載均衡
1. 安裝前工作
下載
wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
(ps:網上有一些源是不完全的,只是安裝了簡單的nginx,其他一些依賴並沒有一起安裝,導致配置文件不 全。請用這裏的連接,完整可用。)
安裝nginx
rpm -ivh epel-release-latest-7.noarch.rpm
yum install nginx
基礎操作
systemctl enable nginx (加入開機啓動)
systemctl start nginx (開啓nginx)
systemctl status nginx (查看狀態)
systemctl stop nginx (關閉nginx)
systemctl restart nginx (重啓nginx)
nginx -s reload (重載配置文件-不會重啓)
nginx實現負載均衡
顧名思義就是將多個請求分發到不同的服務上,實現均衡的負載,減小單個服務的壓力。
$ vi /etc/nginx/nginx.conf (修改配置文件,全局配置文件)
修改實例
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
#(默認爲自動,可以自己設置,一般不大於cpu核數)
worker_processes auto;
#(錯誤日誌路徑)
error_log /var/log/nginx/error.log;
#(pid文件路徑)
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
#(設置網路連接序列化,防止驚羣現象發生,默認爲on)
accept_mutex on;
#(設置一個進程是否同時接受多個網絡連接,默認爲off)
multi_accept on;
#(一個進程的最大連接數)
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
#(這裏註釋掉)
#tcp_nopush on;
tcp_nodelay on;
#(連接超時時間)
keepalive_timeout 65;
types_hash_max_size 2048;
#開啓壓縮
gzip on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# 這裏設置負載均衡,負載均衡有多種策略,nginx自帶的有輪詢,權重,ip-hash,響應時間等策略。
# 默認爲平分http負載,爲輪詢的方式。
# 權重則是按照權重來分發請求,權重高的負載大
# ip-hash,根據ip來分配,保持同一個ip分在同一臺服務器上。
# 響應時間,根據服務器都nginx 的響應時間,優先分發給響應速度快的服務器。
#集中策略可以適當組合
upstream tomcat { #(tomcat爲自定義的負載均衡規則名,可以自己定義)
ip_hash; #(ip_hash則爲ip-hash方法)
server 192.168.14.132:8080 weight=5; #(weihgt爲權重)
server 192.168.14.133:8080 weight=3;
## 可以定義多組規則
}
server {
#(默認監聽80端口)
listen 80 default_server;
#(監聽的服務器)
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#( / 表示所有請求,可以自定義來針對不同的域名設定不同負載規則 和服務)
location / {
# proxy_pass http://tomcat; (反向代理,填上你自己的負載均衡規則名)
# proxy_redirect off; (下面一些設置可以直接複製過去,不要的話,有可能導致一些 沒法認證等的問題)
# proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_connect_timeout 90; (下面這幾個都只是一些超時設置,可不要)
#proxy_send_timeout 90;
#proxy_read_timeout 90;
}
# location ~\.(gif|jpg|png)$ { (比如,以正則表達式寫)
# root /home/root/images;
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
反向代理作用
現在許多大型web網站都用到反向代理。除了可以防止外網對內網服務器的惡性攻擊、緩存以減少服務器的壓力和訪問安全控制之外,還可以進行負載均衡,將用戶請求分配給多個服務器。
配置實例
upstream tomcatserver1 {
server 192.168.72.49:8081;
}
upstream tomcatserver2 {
server 192.168.72.49:8082;
}
server {
listen 80;
server_name 8081.max.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
}
}
server {
listen 80;
server_name 8082.max.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
配置講解原文傳送門
Nginx作爲近年來較火的反向代理服務器,安裝在目的主機端,主要用於轉發客戶機請求,後臺有多個http服務器提供服務,nginx的功能就是把請求轉發給後面的服務器,決定哪臺目標主機來處理當前請求。下面演示如何進行配置使Nginx發揮作用。
- 模擬n個http服務器作爲目標主機
用作測試,簡單的使用2個tomcat實例模擬兩臺http服務器,分別將tomcat的端口改爲8081和8082 - 配置IP域名
192.168.72.49 8081.max.com
192.168.72.49 8082.max.com - 配置nginx.conf
- 流程:
1)瀏覽器訪問8081.max.com,通過本地host文件域名解析,找到192.168.72.49服務器(安裝nginx)
2)nginx反向代理接受客戶機請求,找到server_name爲8081.max.com的server節點,根據proxy_pass對應的http路徑,將請求轉發到upstream tomcatserver1上,即端口號爲8081的tomcat服務器。 - 效果展示
請求8081.max.com,tomcat1接收返回首頁
請求8082.max.com,tomcat2接收返回首頁 - 總結
通過分析我們不難得出,以百度爲例,如果客戶機的IP和百度服務器(目標主機)的IP在同一個網段,那就形同局域網內部發送請求,速度極快。
但如果滿足不了這種需求還想到達到一個較好的請求響應時,百度服務器就可以對外提供一個與目標服務器在一個網段的公網IP,也就是反向代理服務的IP,通過代理服務器轉發客戶機請求,決定幕後的N臺服務器誰來處理這個請求,並且由於反向代理服務器與目標主機在一個網段,訪問速度也會很快。
Nginx用作反向代理服務器時,它就是衆多反向代理服務器中的一種,通過簡單的配置,指定到服務器IP或域名地址便可將客戶機請求轉發給指定服務器處理請求。