简单的负载均衡
简单的负载均衡
实验目的:通过负载均衡实现,通过访问nginx服务器,查看apache页面
实验设备:四台CentOS7服务器
实验规划:配置两台apache服务器,一台NFS+MySQL服务器,一台nginx服务器
通过NFS服务将两台apache实现共享,nginx服务器上进行轮询IP的方式达到负载均衡的目的
apache服务器192.168.2.2,192.168.2.3
NFS+MySQL服务器192.168.2.4
nginx服务器192.168.2.5
实验应用:httpd,php,php-mysql,mariadb,mariadb-server,nginx,nfs,rpcbind,
实验前请检测是否安装最基本的 gcc gcc-c++ make
Apache服务器:
准备服务环境
1 Yum -y install ttpd php php-mysql
编写测试页面
1 echo “aaa” >> /var/www/html/index.php
启动apache服务
1 Systemctl start httpd
NGINX服务器:
源码安装nginx,准备上传工具
1 yum -y install lrzsz 2 3 rz
因为是通过源码安装的nginx,系统不会自动生成nginx程序用户所有需要手动创建账号
1 useradd -M -s /sbin/nolongin nginx
准备环境
1 yum -y install pcre-devel zlib-devel
解压源码包,并编译安装
1 tar xf nginx-1.6.0.tar.gz -C /usr/src/ 2 cd /usr/src/nginx-1.6.0/ 3 ./configure --prefix=/usr/local/nginx-1.6 --user=nginx --group=nginx && make && make install
创建软链接
1 ln -s /usr/local/nginx-1.6/ /usr/local/nginx 2 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
开启nginx服务
1 nginx
修改配置文件,优化服务
1 vim /usr/local/nginx/conf/nginx.conf 2 3 配置文件内 4 user nginx nginx; 5 worker_processes 1; 6 error_log logs/error.log; 7 pid logs/nginx.pid; 8 events { 9 use epoll; 10 worker_connections 10240; 11 } 12 http { 13 include mime.types; 14 default_type application/octet-stream; 15 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 16 '$status $body_bytes_sent "$http_referer" ' 17 '"$http_user_agent" "$http_x_forwarded_for"'; 18 access_log logs/access.log main; 19 sendfile on; 20 keepalive_timeout 65; 21 gzip on; 22 upstream apache{ 23 server 200.200.2.6:80; 24 server 200.200.2.7:80; 25 } 26 server { 27 listen 80; 28 server_name localhost; 29 access_log logs/host.access.log main; 30 location ~ \.php { 31 proxy_pass http://apache; 32 proxy_set_header Host $http_host; 33 proxy_set_header X-Real-IP $remote_addr; 34 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 35 } 36 location /data { 37 proxy_pass http://apache; 38 proxy_set_header Host $http_host; 39 proxy_set_header X-Real-IP $remote_addr; 40 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 41 } 42 location /static/ { 43 proxy_pass http://apache; 44 proxy_set_header Host $http_host; 45 proxy_set_header X-Real-IP $remote_addr; 46 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 47 } 48 error_page 500 502 503 504 /50x.html; 49 location = /50x.html { 50 root html; 51 } 52 location /install/ { 53 proxy_pass http://apache; 54 index index.php; 55 } 56 location ~ \.php$ { 57 proxy_pass http://apache; 58 } 59 } 60 }
重启服务
1 nginx -s reload
搭建NFS+MySQL服务器
创建环境
1 yum -y install nfs-utils rpcbind
创建共享目录
1 mkdir /web_data 2 vim /etc/exports 3 配置文件内 4 #share apache data 5 /web_data 192.168.2.0/24(rw)
启动服务 nfs没有固定端口号,所有需要rpc来通知服务端的rpc端口
1 systemctl start rpcbind 2 systemctl start nfs
在共享目录下放置服务
1 rz 2 yum -y install unzip 3 unzip Discuz_x3.3_SC_UTF8.zip 4 mv upload/* /web_data/www/html
配置共享目录的权限
1 chmod -R 777 /web_data/
搭建数据库
1 yum -y install mariadb mariadb-server
启动数据库
1 systemctl start mariadb
创建数据库管理员并登陆数据库
1 mysqladmin -uroot password 123123
2 musql -uroot -p123123
数据库中创建库,用户权限,
1 create database discuz;
2 grant all on discuz.* to rundiscuz@'200.200.2.%' identified by '123456'; 3 exit
apache配置
搭建nfs环境
1 yum -y install nfs-utils rpcbind
挂载共享目录
1 mount 192.168.2.23:/web_data /var/www/html
安装数据库
1 yum -y install mariadb
网页设置
1 数据库服务器 192.168.2.4 2 数据库名 discuz 3 数据库用户名 rundiscuz 4 数据库密码 123123

浙公网安备 33010602011771号