Brief introduction:
This project combines SVN, LNMP and MySQL three environments to deploy a social networking site that uses PHP language development, builds SVN server for versioning, and centrally manages code developed by PHP programmers, using Nginx as a front-end server. Access the back-end PHP server via the FASTCGI protocol, invoke the PHP page, set up the MySQL master-slave replication environment to store the user's important data, and build the MFS Distributed File system to store the user photos.
1.SVN服务器:简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的2.MFS分布式文件系统,比NFS分布式文件系统更加强大,其他分布式式文件系统还有HDFS分布式文件系统和fastDFS分布式文件系统等3.MySQL环境,此案列环境限于本人机器设备配置问题,只配置了mysql主从复制,如果是真实环境中,还可以配置双击热备,借助amoeba环境实现读写分离,负载均衡等功能,大型网络环境还可以配置MySQL集群
Case Design:
The implementation of four-layer mode is mainly divided into front-end reverse proxy layer, Web layer, database cache layer and database layer. The front-end reverse proxy layer adopts the main standby mode, the Web layer adopts cluster mode, the database cache layer adopts the main standby mode, and the database layer adopts master-slave mode.
To get closer to the production environment, deploy the environment with two physical machines, deploy the front-end reverse proxy layer, the database cache layer, the database tier on the physical machine, and deploy only the Web tier in the KVM virtual machine. At the same time, each layer has made a high-availability architecture to ensure the stability of the business.
Topology: The implementation is normal data flow, the dashed line is the abnormal situation of the data flow
Lab Environment:
Experimental steps: First, Nginx reverse proxy and keepalived1, front-end two reverse proxy server installation with Nginx RPM package source (master and slave to do)
[[email protected] ~]#rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2, use the Yum warehouse to complete the following installation
[[email protected] ~]#yum install -y keepalived nginx
3, configure the front-end reverse proxy host/etc/keepalived/keepalived.conf
[[email protected] ~] #vi/etc/keepalived/keepalived.conf! Configuration File for Keepalivedvrrp_script nginx {interval 2} global_defs {notification_email {}global_ defs {notification_email {[email protected] [email protected]} notification_email_from [email protected] Smtp_server 192.168.200.1 smtp_connect_timeout) vrrp_instance vi_1 {State MASTER interface ENS33 virtual_router_id 100! Configuration File for keepalivedvrrp_script nginx {#添加 script "/opt/shell/nginx.sh" Interval 2}global_defs {notification_email {[email protected] [email protected] [email Protected]} Notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout ro uter_id Nginx_ha} State MASTER [email protected]} notification_email_from [email protected] smtp_s Erver 192.168.200.1 Smtp_connect_timeout router_id nginx_ha}vrrp_instance vi_1 {State MASTER #主服务器 interface Ens33 virtual_router_id Wuyi Priority #优先级 Advert_int 1 Authentication {auth_type PASS auth_pass 1111}track_s cript {#添加 Nginx} virtual_ipaddress {192.168.126.188 #内网虚拟IP 192.168.100. 188 #外网虚拟IP}}
Note: A nginx.sh script is loaded into the configuration file, and the script means that if keepalived is started, the Nginx service is checked and started every 2 seconds
4. Create a/opt/shell/nginx.sh file
[[email protected] ~]# mkdir /opt/shell[[email protected] ~]# cd /opt/shell/[[email protected] shell]# vim nginx.sh#!/bin/bashk=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $k -gt 0 ];then /bin/systemctl start nginx.serviceelse/bin/systemctl stop nginx.servicefi[[email protected] shell]# chmod +x nginx.sh #脚本增加可执行权限
5, the front-end reverse proxy standby configuration is basically the same configuration as the primary server, the only difference is the keepalived configuration, as follows
[[email protected] ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalivedvrrp_script nginx { #nginx触发脚本 script "/opt/shell/nginx.sh" interval 2}global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id NGINX_HB # 名称}vrrp_instance VI_1 { state BACKUP #从服务器 interface ens33 virtual_router_id 52 #id号 priority 90 #优先级 advert_int 1 authentication { auth_type PASS auth_pass 1111 }track_script { #添加 nginx} virtual_ipaddress { 192.168.126.188 #虚拟IP 192.168.100.188 }}
The other one is the same, no longer repeat here
6, configure the Nginx front-end scheduling function on the master-slave server
[[email protected] PV]# vim /etc/nginx/nginx.conf #gzip on; upstream tomcat_pool { server 192.168.126.193:8080; #两台节点服务器的IP server 192.168.126.194:8080; ip_hash; #会话稳固功能,否则无法通过vip地址登陆 } server { listen 80; server_name 192.168.126.188; #虚拟出的IP location / { proxy_pass http://tomcat_pool; proxy_set_header X-Real-IP $remote_addr; } }
1) test configuration file Syntax
[[email protected] PV]# nginx -t -c /etc/nginx/nginx.confnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
2) After starting the keepalived service, the Nginx service will also start.
[[email protected] PV]# systemctl stop firewalld.service [[email protected] PV]# setenforce 0[[email protected] PV]# systemctl start keepalived.service
3) test keepalived's dual-machine hot-standby function
Turn off the host's keepalived service to see if the virtual IP can automatically drift to the standby machine.
Primary server: [[email protected] pv]# IP addrinet 192.168.126.138/24 BRD 192.168.126.255 Scope Global Dynamic Ens33 valid_lft 1697sec preferred_lft 1697sec inet 192.168.126.188/32 scope global Ens33 Valid_lft forever Preferred_lft forever[[email protected] pv]# systemctl stop keepalived.service link/ether 00:0c:29:15:68:5d BRD FF : Ff:ff:ff:ff:ff inet 192.168.126.138/24 BRD 192.168.126.255 scope Global Dynamic ENS33 valid_lft 1625sec Preferre D_lft 1625sec inet6 fe80::7f06:3732:bb38:c9e1/64 scope link Valid_lft forever Preferred_lft forever Backup server: [[email] protected] ~]# IP addrlink/ether 00:0c:29:5b:03:86 brd ff:ff:ff:ff:ff:ff inet 192.168.126.192/24 BRD 192.168.126. 255 scope Global Dynamic ENS33 valid_lft 1180sec preferred_lft 1180sec inet 192.168.126.188/32 Scope Global ENS33 Valid_lft Forever Preferred_lft Forever inet 192.168.100.188/32 Scope global Ens33 #虚拟IP自动漂移成功
注意:再次将主服务器的keepalived服务开启后,虚拟IP又会漂移到主服务器上。
Ii. install Tomcat on both node servers (same configuration) 1. Install jdk-8u144-linux-x64.tar.gz and apache-tomcat-8.5.23.tar.gz packages
[[email protected] PV]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/[[email protected] PV]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/
2. Move the extracted files to the/usr/local directory and create a new directory for easy management.
[[email protected] PV]# cd /opt/[[email protected] opt]# lsapache-tomcat-8.5.16 apache-tomcat-8.5.23 jdk1.8.0_144 jdk1.8.0_91 rh[[email protected] opt]# mv jdk1.8.0_144/ /usr/local/java[[email protected] opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
3. At the end of the/etc/profile profile line, add the following:
[[email protected] opt]# vim /etc/profileexport JAVA_HOME=/usr/local/javaexport JRE_HOME=/usr/local/java/jreexport PATH=$PATH:/usr/local/java/binexport CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib[[email protected] opt]# source /etc/profile #启动
[[email protected] opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup[[email protected] opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
4. Start the Tomcat service and see if its 8080 ports are turned on
[[email protected] opt]# systemctl stop firewalld.service [[email protected] opt]# setenforce 0[[email protected] opt]# tomcatup [[email protected] opt]# netstat -ntap | grep 8080tcp6 0 0 :::8080 :::* LISTEN
5. Open the browser to test whether the default test page is displayed correctly
6. Modify the default page content
[[email protected] opt]# cd /usr/local/tomcat8/webapps/ROOT/[[email protected] ROOT]# mv index.jsp index.jsp.bk[[email protected] ROOT]# vim index.jsp
7. Enter the scheduler address, which is the virtual address, to test the scheduling of the two nodes. :Stop the Tomcat Service on node server 1 and refresh the browser again to see if the dispatch was successful. :
8. Modify/usr/local/tomcat8/conf/server.xml under host name, add the following:[[email protected] conf]# vim server.xml <Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> #日志调试信息debug为0表示信息越少,docBase指定访问目录。
Third, install the MySQL database on the master-slave server 1. Install MARIADB Service[[email protected] PV]# yum install mariadb-server mariadb -y[[email protected] PV]# systemctl enable mariadb.service #开启自启动[[email protected] PV]# systemctl start mariadb.service #开启服务[[email protected] PV]# netstat -ntap | grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
2. General Security Settings[[email protected] PV]# mysql_secure_installation Enter current password for root (enter for none): #EnterSet root password? [Y/n] y 设置用户密码New password: #abc123Re-enter new password: #确认密码abc123 Remove anonymous users? 删除匿名用户 nDisallow root login remotely? 不允许远程root登录吗? nRemove test database and access to it? 删除测试数据库并访问它? nReload privilege tables now? 现在重新加载权限表吗? y
3. Import the Database Slsaledb[[email protected] PV]# mysql -u root -p < slsaledb-2014-4-10.sql [[email protected] PV]# mysql -u root -pMariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || slsaledb || test |+--------------------+5 rows in set (0.02 sec)MariaDB [(none)]> GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc123‘; #授权MariaDB [(none)]> flush privileges; #刷新
4. Modify the database IP address on both Tomcat nodes is the virtual IP of VRRP, and the authorized user name root and password abc123.[[email protected] PV]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/[[email protected] PV]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/[[email protected] classes]# vim jdbc.properties driverClassName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://192.168.126.188\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8uname=root #用户名password=abc123 #密码minIdle=10maxIdle=50initialSize=5maxActive=100maxWait=100removeAbandonedTimeout=180removeAbandoned=true
5. Open the browser to test the siteThird, install and configure the Redis master-Slave cache serverAbout Redis:
Redis is a high-performance Key-value database, similar to memcached, but it supports more value types. As with memcached, data is cached in memory to ensure efficiency.
The difference is that Redis periodically writes the updated data to disk or writes the modification to the appended record file, and Master-slave (master/Slave) synchronization is implemented on this basis.
1. Install and configure the Redis master-slave[[email protected] ~]# yum install -y epel-release[[email protected] ~]#yum install redis -y
2. Modify the listening port in the Redis Master profile/etc/redis.conf of the master-slave cache server and modify it to bind 0.0.0.0.[[email protected] ~]# vim /etc/redis.confbind 0.0.0.0 #61行从服务器上多如下一行配置slaveof 192.168.126.138 6379 #主服务器的IP不是虚拟IP(266行)
3. Start the Redis service[[email protected] ~]# systemctl start redis.service[[email protected] ~]# netstat -anpt | grep 6379tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 50475/redis-server
4. Test the connection主服务器:[[email protected] ~]# redis-cli -h 192.168.126.138 -p 6379 192.168.126.138:6379> set name test #设置name 值是testOK192.168.126.138:6379> get name #获取name值"test"从服务器:[[email protected] ~]# redis-cli -h 192.168.126.192 -p 6379 #登录从,获取值,成功说明主从同步成功。192.168.126.192:6379> get name
5. On the Tomcat node server, configure the parameters for the Redis connection in the Marketplace project.[[email protected] classes]#cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/[[email protected] classes]# vim applicationContext-mybatis.xml <constructor-arg value="192.168.126.188"/> #47行 <constructor-arg value="6379"/> #48行
6. Test the cache effect[[email protected] ~]# redis-cli -h 192.168.126.188 -p 6379192.168.126.188:6379> info.....//省略evicted_keys:0keyspace_hits:1 #keyspace_hits:1 或者 keyspace_misses:2//关注这个值,命中数和未命中数keyspace_misses:0pubsub_channels:0
Log in to the mall, multiple clicks need to participate in the database Operation page, and then come back to check keyspace_hits or keyspace_misses: value changes. If the keyspace_hits:1 value changes to 2, Redis is working correctly.
Iv. Configuring the Redis cluster master-slave switchover---Only the primary server is operational[[email protected] ~]# vi /etc/redis-sentinel.conf protected-mode no #17行 开启 sentinel monitor mymaster 192.168.126.138 6379 1 #1表示1台从 注意:修改为主IP sentinel down-after-milliseconds mymaster 3000 #故障切换时间单位是毫秒
1. Start the cluster[[email protected] ~]# service redis-sentinel start[[email protected] ~]# netstat -anpt | grep 26379tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 57151/redis-sentine tcp6 0 0 :::26379 :::* LISTEN 57151/redis-sentine [[email protected] ~]# redis-cli -h 192.168.126.138 -p 26379 infoSentinel #查看集群信息# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.126.138:6379,slaves=1,sentinels=1
2. Verify Master-Slave switching主服务器:[email protected] ~]# systemctl stop redis.service #关闭主服务器的redis服务[[email protected] ~]# redis-cli -h 192.168.126.138 -p 26379 info Sentinel # Sentinel #发现主切换成从服务器的IPsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.126.192:6379,slaves=1,sentinels=1
3. Verifying data synchronization在主服务器上:[[email protected] ~]# redis-cli -h 192.168.126.192 -p 6379 192.168.126.192:6379> set name2 test2OK192.168.126.192:6379> get name2"test2"192.168.126.192:6379>在从服务器上查看:[[email protected] ~]# redis-cli -h 192.168.126.192 -p 6379 192.168.126.192:6379> get name2"test2"启动主服务器的redis服务:[[email protected] ~]# systemctl start redis.service[[email protected] ~]# redis-cli -h 192.168.126.138 -p 6379192.168.126.138:6379> get name2"test2" #同步成功
V. Configure MySQL master-slave replication on the master/slave server 1. The preferred main library needs to open the Binlog log and add the following to the/etc/my.cnf file that opens the main library:[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockbinlog-ignore-db=mysql,information_schema #日志character_set_server=utf8 #字符集log_bin=mysql_bin #二进制日志server_id=1 #从服务器的id号不能相同log_slave_updates=true sync_binlog=1
2. Restart the database[[email protected] ~]# systemctl restart mariadb.service [[email protected] ~]# netstat -ntap | grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 65321/mysqld
3. Log in to the main library and authorize, allowing the log to be read from the main library from the specified user of the library.[[email protected] ~]# mysql -u root -pMariaDB [(none)]> show master status; #记录日志文件名称和 位置值+------------------+----------+--------------+--------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+--------------------------+| mysql_bin.000001 | 245 | | mysql,information_schema |+------------------+----------+--------------+--------------------------+1 row in set (0.00 sec)MariaDB [(none)]> grant replication slave on *.* to ‘rep‘@‘192.168.126.%‘ identified by ‘123456‘; #授权MariaDB [(none)]> flush privileges;
4.Mysql from Database Configuration[[email protected] ~]# mysql -u root -p MariaDB [(none)]> change master to master_host=‘192.168.126.138‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;MariaDB [(none)]> start slave;MariaDB [(none)]> show slave status\G;.....//省略 Slave_IO_Running: Yes Slave_SQL_Running: Yes
5. Test Master-Slave synchronizationCreate a library on the MySQL master server to see if you can synchronize the new database on the primary server from the server.
主服务器:MariaDB [(none)]> flush privileges;MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || school || slsaledb || test |+--------------------+6 rows in set (0.28 sec)从服务器:MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || school | #| slsaledb || test |+--------------------+6 rows in set (0.19 sec)
Step by step operation, you can also succeed!
Careful analysis, detailed how to build the million PV site architecture, easy to understand!!!