一回铛、實(shí)驗環(huán)境
? ? 1、物理機(jī)系統(tǒng)配置:Windows 10 企業(yè)版或?qū)I(yè)版克锣,I7 處理器茵肃,16G 內(nèi)存,SSD 固態(tài)盤512G 分區(qū)袭祟,安裝 Hyper-V 管理器验残。
? ? 2、虛擬機(jī)系統(tǒng)配置:Centos1810巾乳,1 CPU您没,512M 內(nèi)存
? ? 3、物理機(jī)與虛擬機(jī)文件傳輸:WinSCP-5.13.7 + PuTTY-0.70.00
? 4胆绊、 實(shí)驗規(guī)劃(Mongo 可以不安裝氨鹏,但提供安裝步驟):
? ? ? ? 1):NGINX 服務(wù)器:192.168.31.100/24/1/1
? ? ? ? 2):App-01 服務(wù)器:192.168.31.101/24/1/1
? ? ? ? 3):App-02 服務(wù)器:192.168.31.102/24/1/1
? ? ? ? 4):Mongo 服務(wù)器:192.168.31.199/24/1/1
? ? 5、所有 APP-XXXXX 應(yīng)用服務(wù)器压状,均使用 NGINX 承載仆抵。
二跟继、安裝步驟
? ? 1、安裝 Centos1810 模板機(jī)镣丑,虛擬機(jī)名稱:CentOS-1810-Mother-IP-Yes-192.168.31.88舔糖,選擇最小化安裝,配置 IP 為:192.168.31.88/24/1/1莺匠,機(jī)器名稱:centos1810.test.com金吗,安裝完成,關(guān)機(jī)
? ? 2趣竣、創(chuàng)建 NGINX 差異盤摇庙,取名:192.168.31.100-NGINX;創(chuàng)建虛擬機(jī)遥缕,取名:192.168.31.100-NGINX跟匆,1CPU,512M內(nèi)存通砍,網(wǎng)絡(luò)選擇內(nèi)部玛臂,修改機(jī)器名為:nginx,IP為:192.168.31.100
? ? 3封孙、創(chuàng)建 App-01 差異盤迹冤,取名:192.168.31.101-App-01;創(chuàng)建虛擬機(jī)虎忌,取名:192.168.31.101-App-01泡徙,1CPU,512M內(nèi)存膜蠢,網(wǎng)絡(luò)選擇內(nèi)部堪藐,修改機(jī)器名為:app01,IP為:192.168.31.101
? ? 4挑围、創(chuàng)建 App-02 差異盤礁竞,取名:192.168.31.102-App-02;創(chuàng)建虛擬機(jī)杉辙,取名:192.168.31.102-App-02模捂,1CPU,512M內(nèi)存蜘矢,網(wǎng)絡(luò)選擇內(nèi)部狂男,修改機(jī)器名為:app02,IP為:192.168.31.102
? ? 5品腹、創(chuàng)建 Mongo 差異盤岖食,取名:192.168.31.199-Mongo;創(chuàng)建虛擬機(jī)舞吭,取名:192.168.31.199-Mongo泡垃,1CPU析珊,512M內(nèi)存,網(wǎng)絡(luò)選擇內(nèi)部兔毙,修改機(jī)器名為:mongo,IP為:192.168.31.199
三兄春、安裝并配置 NGINX 開機(jī)啟動
? ? ? ? 從網(wǎng)站下載最新的 NGINX 安裝包澎剥,目前為最新nginx-1.14.2.tar.gz,再啟動WinSCP并且連接成功后赶舆,將安裝包上傳到/usr/local/src目錄哑姚。
? ? ? ? 通過 WinSCP 打開 PuTTY,或者直接打開 PuTTY芜茵,切換到 /usr/local/src 目錄叙量,運(yùn)行 tar -zxvf nginx-1.14.2.tar.gz 解壓,再切換到 nginx-1.14.2 目錄下九串,即 cd nginx-1.14.2绞佩,執(zhí)行 ./configure命令,缺包 gcc猪钮,安裝 yum -y install gcc品山,再執(zhí)行 ./configure命令,缺包 pcre烤低,安裝 yum -y install pcre-devel肘交,再執(zhí)行 ./configure命令,缺包 zlib扑馁,安裝 yum -y install zlib-devel涯呻,再執(zhí)行 ./configure命令,提示正常腻要,編譯并安裝 make && make install复罐,查位置whereis nginx,顯示 nginx: /usr/local/nginx雄家。
? ? ? ? 切換到 /lib/systemd/system/ 目錄市栗,創(chuàng)建 nginx.service 文件 nginx.service,文件內(nèi)容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存文件退出咳短。
四填帽、打開防火墻
查看已開放的端口(默認(rèn)不開放任何端口)
firewall-cmd --list-ports
開啟端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重啟防火墻
firewall-cmd --reload
停止防火墻
systemctl stop firewalld.service
設(shè)置防火墻開機(jī)啟動
systemctl enable firewalld.service
禁止防火墻開機(jī)啟動
systemctl disable firewalld.service
刪除端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --zone=public --remove-port=40000-40080/tcp --permanent
五、配置NGINX開機(jī)啟動
執(zhí)行
systemctl enable nginx.service? ? 使 nginx 開機(jī)啟動
systemctl start nginx.service? ? 啟動 nginx
systemctl stop nginx.service? ? 結(jié)束 nginx
systemctl restart nginx.service? ? 重啟 nginx
驗證http://localhost咙好,出現(xiàn)界面
六篡腌、重復(fù)三、四勾效、五步驟嘹悼,配置兩臺 APP 應(yīng)用服務(wù)器
? ? 1叛甫、注意機(jī)器名和 IP 地址的修改
? ? 2、把 App-01 服務(wù)器:192.168.31.101 的 /usr/local/nginx/html 下的 index.html 的 body 中的第一行增加 <h1>192.168.31.101</h1>
? ? 3杨伙、把 App-02 服務(wù)器:192.168.31.102 的 /usr/local/nginx/html 下的 index.html 的 body 中的第一行增加 <h1>192.168.31.102</h1>
七其监、配置NGINX,實(shí)現(xiàn)負(fù)載均衡
? ? ? ? 1限匣、打開 NGINX 服務(wù)器:192.168.31.100抖苦,的配置文件,/usr/local/nginx/conf/nginx.conf米死,修改內(nèi)容如下:(或者直接拷貝即可)
#user? nobody;
worker_processes? auto;#工作進(jìn)程的個數(shù)锌历,可以配置多個
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid;
events {
#epoll是多路復(fù)用IO(I/O Multiplexing)中的一種方式,僅用于linux2.6以上內(nèi)核,可以大大提高nginx的性能
use? epoll;
#單個進(jìn)程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進(jìn)程數(shù))
worker_connections? 1024;
}
http {
#設(shè)定mime類型,類型由mime.type文件定義,#文件擴(kuò)展名與文件類型映射表
include? ? ? mime.types;
#默認(rèn)文件類型
default_type? application/octet-stream;
#設(shè)定日志格式
#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? logs/access.log? main;
sendfile? ? ? ? on;#開啟高效文件傳輸模式,sendfile指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件峦筒,對于普通應(yīng)用設(shè)為 on究西,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為off物喷,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度卤材,降低系統(tǒng)的負(fù)載。注意:如果圖片顯示不正常把這個改成off峦失。
#tcp_nopush? ? on;
#keepalive_timeout 0;
keepalive_timeout 65; #長連接超時時間商膊,單位是秒
tcp_nodelay on;
gzip on;#啟用Gizp壓縮
#服務(wù)器的群集
? ? upstream? yzj.com {? #服務(wù)器集群名字
#每個請求按訪問ip的hash結(jié)果分配,這樣每個訪客固定訪問一個后端服務(wù)器宠进,可以解決session的問題晕拆。
#ip_hash;
#服務(wù)器配置? weight是權(quán)重的意思,權(quán)重越大材蹬,分配的概率越大实幕。
server? ? 192.168.31.101:80 weight=1;
server? ? 192.168.31.102:80 weight=1;
}
#當(dāng)前的Nginx的配置
? ? server {
? ? listen? ? ? 80;#監(jiān)聽80端口,可以改成其他端口
? ? server_name? yzj.com localhost www.yzj.com;#當(dāng)前服務(wù)的域名,用空格隔開的多個域名
? ? #charset koi8-r;
? ? #access_log? logs/host.access.log? main;
? ? #location / {
? ? #? ? root? html;
? ? #? ? index? index.html index.htm;
? ? #}
location / {
? ? ? ? ? ? proxy_pass http://yzj.com;
? ? ? ? ? ? proxy_redirect default;
? ? ? ? }
location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
? ? ? ? {
? ? ? ? ? ? expires 30d;
? ? ? ? ? ? root /nginx-1.14.2;#root:
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page? 500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? html;
? ? ? ? }
? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? proxy_pass? http://127.0.0.1;
? ? ? ? #}
? ? ? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? root? ? ? ? ? html;
? ? ? ? #? ? fastcgi_pass? 127.0.0.1:9000;
? ? ? ? #? ? fastcgi_index? index.php;
? ? ? ? #? ? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
? ? ? ? #? ? include? ? ? ? fastcgi_params;
? ? ? ? #}
? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? #? ? deny? all;
? ? ? ? #}
? ? }
? ? # another virtual host using mix of IP-, name-, and port-based configuration
? ? #
? ? #server {
? ? #? ? listen? ? ? 8000;
? ? #? ? listen? ? ? somename:8080;
? ? #? ? server_name? somename? alias? another.alias;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
? ? # HTTPS server
? ? #
? ? #server {
? ? #? ? listen? ? ? 443;
? ? #? ? server_name? localhost;
? ? #? ? ssl? ? ? ? ? ? ? ? ? on;
? ? #? ? ssl_certificate? ? ? cert.pem;
? ? #? ? ssl_certificate_key? cert.key;
? ? #? ? ssl_session_timeout? 5m;
? ? #? ? ssl_protocols? SSLv2 SSLv3 TLSv1;
? ? #? ? ssl_ciphers? HIGH:!aNULL:!MD5;
? ? #? ? ssl_prefer_server_ciphers? on;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
}
保存退出堤器。
? ? ? ? 2昆庇、重啟 NGINX: systemctl restart nginx.service
? ? ? ? 3、測試:在100上打開 http://localhost闸溃,會出現(xiàn)101和102切換的效果
? ? ? ? 4整吆、開啟 ip_hash:去掉 ip_hash 前邊的 #,使 持久化后端服務(wù)器
? ? ? ? 5辉川、重啟 NGINX: systemctl restart nginx.service
? ? ? ? 6表蝙、測試:在100上打開 http://localhost,只會出現(xiàn)101乓旗,因為101排在第一位府蛇,和其它服務(wù)器的 weight 相同
? ? ? ? 7、修改 upstream? yzj.com 中的 weight 為2: server? ? 192.168.31.102:80 weight=2屿愚,保存
? ? ? ? 8汇跨、重啟 NGINX: systemctl restart nginx.service
? ? ? ? 9务荆、測試:在100上打開 http://localhost,只會出現(xiàn)102穷遂,因為101雖然排在第一位函匕,但 weight 權(quán)重比102小,所以102優(yōu)先提供服務(wù)
? ? ? ? 10蚪黑、增加其它服務(wù)器:103盅惜、104,調(diào)整 weight祠锣,看到效果
八酷窥、MongoDB 離線安裝及開機(jī)啟動
? ? ? ? 1咽安、以管理員 root 登錄伴网,從官網(wǎng)https://www.mongodb.com/download-center/community(https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz)下載最新文件mongodb-linux-x86_64-4.0.5.gz,并上傳到 /usr/local/src下妆棒,cd /usr/local/src澡腾,執(zhí)行tar -zxvf mongodb-linux-x86_64-4.0.5.gz,移動并改目錄名 mv mongodb-linux-x86_64-4.0.5 /usr/local/mongodb
? ? ? ? 2糕珊、查看當(dāng)前磁盤空間:df -h动分,發(fā)現(xiàn) home 下空間較大,在 home 下創(chuàng)建 mongodb 目錄红选,mkdir -p /home/mongodb/db澜公,mkdir -p /home/mongodb/log,創(chuàng)建兩個目錄喇肋,并設(shè)置目錄權(quán)限:chmod -R 7777 /home/mongodb
? ? ? ? 3坟乾、創(chuàng)建配置文件。進(jìn)入 /usr/local/mongodb 目錄下的 bin 目錄下蝶防,新建 mongodb.conf 文件甚侣,port 默認(rèn)為 27017,為安全起見间学,使用其它端口殷费,如:1972。具體內(nèi)容如下
bind_ip = 0.0.0.0
port = 27017
dbpath = /home/mongodb/db
logpath = /home/mongodb/log/mongodb.log
logappend = true
fork = true
注意低葫,開啟守護(hù)進(jìn)程模式 fork 的時候详羡,一定要設(shè)置 log 日志;
設(shè)置log日志要注意嘿悬,logpath的路徑一定要是文件路徑殷绍,而不是文件夾路徑。
? ? ? ? 4鹊漠、開啟端口供網(wǎng)絡(luò)訪問
firewall-cmd --zone=public --add-port=27017/tcp --permanent
? ? ? ? 5主到、查看端口
firewall-cmd --permanent --query-port=27017/tcp
? ? ? ? 6茶行、重啟防火墻
firewall-cmd --reload
? ? ? ? 注意事項:
a,要加上? --permanent 才能保證重啟后也能打開
b登钥,這里最好重啟一下防火墻畔师,有時候開啟端口并不能立即生效
好了,現(xiàn)在局域網(wǎng)內(nèi)其它機(jī)器也可以連接到數(shù)據(jù)庫了牧牢。
? ? ? ? 7看锉、注冊到系統(tǒng)開機(jī)啟動
? ? ? ? centos 7的開機(jī)啟動跟之前版本的 centos 有很大不同。現(xiàn)在用 systemctl 命令代替了之前的 chkconfig 和 service 命令
注冊到開機(jī)啟動的方法如下:
在系統(tǒng)服務(wù)目錄下新建mongodb的啟動服務(wù)塔鳍,并給與754的權(quán)限
cd /lib/systemd/system
創(chuàng)建 mongodb.service 伯铣,內(nèi)容如下
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/bin/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
路徑必須要寫絕對路徑
并給與754的權(quán)限chmod 754 /lib/systemd/system/mongodb.service
? ? ? ? 8、啟動 systemctl start mongodb.service
? ? ? ? 9轮纫、關(guān)閉 systemctl stop mongodb.service
? ? ? ? 10腔寡、注冊到開機(jī)啟動 systemctl enable mongodb.service
? ? ? ? 11、檢查數(shù)據(jù)庫是否安裝成功
ps -aux | grep mongod#查看數(shù)據(jù)庫的進(jìn)程是否存在
? ? ? ? 12掌唾、重啟機(jī)器驗證init 6放前,或者 reboot,網(wǎng)絡(luò)可以訪問了糯彬。