LNAMP架构项目一
一、第一阶段
假设:公司初期只有一台web服务器,搭建Web服务器的脚本如下:
1 #检查环境 2 setenforce 0 &> /dev/null 3 sed -i s/=enforcing/=disabled/g /etc/selinux/config 4 systemctl restart firewalld 5 #使用CentOS系统原始源下载相关软件 6 yum -y remove maria* 7 echo '[mysql57-community] 8 name=MySQL 5.7 Community Server 9 baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ 10 enabled=1 11 gpgcheck=0 12 ' > /etc/yum.repos.d/mysql57-community.repo 13 yum clean all && yum makecache -q 14 yum -y install epel-release -q 15 yum -y install vim bash-com* net-tools unzip nginx httpd php php-mysql mysql-com*server -q 16 #使用nginx实现动静分离,搭建网站 17 sed -i '42s/80/81/g' /etc/httpd/conf/httpd.conf 18 sed -i '38,$'c} /etc/nginx/nginx.conf 19 echo " server { 20 listen 80; 21 server_name 3344.com; 22 location ~*\.php$ { 23 proxy_pass http://127.0.0.1:81; 24 } 25 location / { 26 root /var/www/html; 27 } 28 }" > /etc/nginx/conf.d/3344.conf 29 systemctl restart httpd nginx 30 systemctl enable httpd nginx -q 31 firewall-cmd --add-port=80/tcp 32 firewall-cmd --add-port=80/tcp --permanent -q 33 #配置mysql数据库,开启二进制日志,创建数据库及用户 34 sed -i 4a"server-id=1\nlog-bin=binlog" /etc/my.cnf 35 systemctl restart mysqld 36 systemctl enable mysqld 37 PW=`cat /var/log/mysqld.log | grep password |head -n 1 |awk '{print $NF}'` 38 mysql -p$PW --connect-expired-password -e "alter user 'root'@'localhost' identified by 'Ryz0304/1';" 39 echo "[client] 40 user=root 41 password='Ryz0304/1'" > /root/.my.cnf #将数据库的用户密码写入配置文件中,方便登录 42 mysql -e "create database wordpress;" 43 mysql -e "grant all on wordpress.* to 'wordpress'@'localhost' identified by 'W0rdpress.';" 44 mysql -e "flush privileges;" 45 rm -rf /var/www/html/* 46 chmod -R 777 /var/www/html 47 #上传代码至nginx的工作目录/var/www/html(自定义的) 48 #浏览器访问即可
问题:
1、安装 MySQL 5.7 版本,需要卸载系统自带的mariadb-*
2、MySQL 5.7 版本安装完成后,启动命令时systemctl start mysqld,必须是mysqld;且第一次进入数据库时需要查看mysql的初始密码(/var/log/mysqld.log)
二、第二阶段
为防止一台服务器发生故障,需要动态添加一台新的服务器,使其成为主备关系,在第一台服务器宕机的情况下,web服务还可以正常运作:
#完成2台服务器的同时搭建 #各服务器的密码 MM=0304 #检查环境 setenforce 0 &> /dev/null sed -i s/=enforcing/=disabled/g /etc/selinux/config systemctl restart firewalld systemctl enable firewalld &> /dev/null echo "10.0.0.11 s1 10.0.0.12 s2" >>/etc/hosts sshpass -p $MM scp -r /etc/hosts 10.0.0.12:/etc ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa &> /dev/null cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys sed -i 35c'StrictHostKeyChecking no' /etc/ssh/ssh_config #配置yum源:使用原始CentOS系统的原始源,安装epel源,配置mysql源,安装服务 for a in s{1..2} do sshpass -p $MM scp -r /root/.ssh $a:/root ssh $a "yum -y remove maria*" ssh $a "echo '[mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/\$basearch/ enabled=1 gpgcheck=0 ' > /etc/yum.repos.d/mysql57-community.repo yum clean all -q && yum makecache -q yum -y install epel-release -q yum -y install vim bash-com* net-tools unzip nginx httpd php php-mysql mysql-com*server -q" done #配置nginx+httpd+php动静分离 for b in s{1..2} do ssh $b "sed -i '42s/80/81/g' /etc/httpd/conf/httpd.conf" ssh $b "sed -i '38,$'c} /etc/nginx/nginx.conf" ssh $b "echo \" server { listen 80; server_name 3344.com; location ~*\.php\$ { proxy_pass http://127.0.0.1:81; } location / { root /var/www/html; } }\" > /etc/nginx/conf.d/3344.conf" ssh $b "systemctl restart httpd nginx systemctl enable httpd nginx -q firewall-cmd --add-port=80/tcp firewall-cmd --add-port=80/tcp --permanent -q" done #配置nginx+keepalived实现负载均衡和高可用 for c in s{1..2} do ssh $c "cat >/etc/nginx/conf.d/3344.conf <<EOF server { listen 80; location ~*\.php\$ { proxy_pass http://127.0.0.1:81; } location / { root /var/www/html; proxy_pass http://3344.com; } } upstream 3344.com { server 10.0.0.11; server 10.0.0.12; } EOF " ssh $c "cat > /etc/keepalived/keepalived.conf<<EOF ! Configuration File for keepalived vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.100 } } EOF " ssh $c "firewall-cmd --add-port=80/tcp --permanent" ssh $c "firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --protocol vrrp -j ACCEPT" ssh $c "firewall-cmd --reload" ssh $c "systemctl enable keepalived" $> /dev/null ssh $c "systemctl restart nginx keepalived" done ssh s2 "sed -i s/MASTER/BACKUP/g /etc/keepalived/keepalived.conf" ssh s2 "sed -i s/100/90/g /etc/keepalived/keepalived.conf" ssh s2 "systemctl restart keepalived" #mysql5.7配置一 sed -i 4a'server-id=1\nlog-bin=log' /etc/my.cnf firewall-cmd --add-port=3306/tcp --permanent firewall-cmd --reload systemctl enable mysqld systemctl restart mysqld PWD1=`cat /var/log/mysqld.log |grep pass |head -n 1|awk '{print $NF}'` cat > ~/.my.cnf <<EOF [mysql] user=root password='$PWD1' EOF mysql --connect-expired-password -e "alter user 'root'@'localhost' identified by 'Ryz0304.';" cat > ~/.my.cnf <<EOF [mysql] user=root password='Ryz0304.' EOF mysql -e "grant REPLICATION SLAVE on *.* to 'repl'@'10.0.0.%' identified by 'Repl432/1';" mysql -e "create database wordpress;" mysql -e "grant all on wordpress.* to 'wordpress'@'s1' identified by 'W0rdpress/1';" mysql -e "grant all on wordpress.* to 'wordpress'@'s2' identified by 'W0rdpress/1';" mysql -e "flush privileges;" #mysql5.7配置二 ssh s2 "sed -i 4a'server-id=2\nlog-bin=log' /etc/my.cnf" ssh s2 "firewall-cmd --add-port=3306/tcp --permanent" ssh s2 "firewall-cmd --reload" ssh s2 "systemctl enable mysqld" ssh s2 "systemctl restart mysqld" PWD2=`ssh s2 "cat /var/log/mysqld.log" |grep pass |head -n 1|awk '{print $NF}'` ssh s6 "cat > ~/.my.cnf <<EOF [mysql] user=root password='$PWD2' EOF" ssh s2 "mysql --connect-expired-password -e \"alter user 'root'@'localhost' identified by 'Ryz0304.';\"" ssh s2 "cat > ~/.my.cnf <<EOF [mysql] user=root password='Abc1234/1' EOF" ssh s2 "mysql -e \"grant REPLICATION SLAVE on *.* to 'repl'@'10.0.0.%' identified by 'Repl432/1';\"" ssh s2 "mysql -e \"grant all on wordpress.* to 'wordpress'@'s1' identified by 'W0rdpress/1';\"" ssh s2 "mysql -e \"grant all on wordpress.* to 'wordpress'@'s2' identified by 'W0rdpress/1';\"" ssh s2 "mysql -e \"flush privileges;\"" #主主 FILE1=`mysql -e "show master status\G"|grep File |awk '{print $2}'` POSI1=`mysql -e "show master status\G"|grep Posit |awk '{print $2}'` FILE2=`ssh s2 "mysql -e \"show master status\G\""|grep File |awk '{print $2}'` POSI2=`ssh s2 "mysql -e \"show master status\G\""|grep Posit |awk '{print $2}'` mysql -e "change master to master_host='s5',master_user='repl',master_password='Repl432/1',master_port=3306,master_log_file='$FILE1',master_log_pos=$POSI1;" mysql -e "start slave;" ssh s2 "mysql -e \"change master to master_host='s6',master_user='repl',master_password='Repl432/1',master_port=3306,master_log_file='$FILE2',master_log_pos=$POSI2;\"" ssh s2 "mysql -e \"start slave;\""
三、第三阶段
四、第四阶段
五、第五阶段
六、架构