LNMP项目案例
1、环境准备
2、搭建backup服务器
# 关闭防火墙、selinux
[root@backup ~]# systemctl stop firewalld
[root@backup ~]# setenforce 0
# 安装rsync
[root@backup ~]# yum install rsync -y
# 统一用户
[root@backup ~]# useradd -u1000 www
[root@backup ~]# id www
uid=1000(www) gid=1000(www) groups=1000(www)
# 编写配置文件(前端代码仓库、数据库备份、上传文件的备份)
[root@backup ~]# vim /etc/rsyncd.conf
uid=www
gid=www
port=873
fake super=yes
use chroot=no
max connection=200
timeuot=600
ignore errors
read only=false
list=false
auth users=dandan
secrets file=/etc/rsync.passwd
log file=/var/log/rsyncd/log
###################################
[web]
comment="前端代码仓库"
path=/backup/web
[database]
comment="数据库备份"
path=/backup/database
[download]
comment="上传文件备份"
path=/backup/download
# 创建仓库
[root@backup ~]# mkdir /backup
[root@backup ~]# mkdir /backup/web
[root@backup ~]# mkdir /backup/database
[root@backup ~]# mkdir /backup/download
[root@backup ~]# tree /backup/
/backup/
├── database
├── download
└── web
# 授权
[root@backup ~]# chown www.www -R /backup/
# 创建密码文件
[root@backup ~]# echo "dandan:111" > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd
# 启动 (立即启动和设置开机自启)
[root@backup ~]# systemctl enable --Now rsyncd
3、搭建NFS服务器
# 安装软件 nfs-utils rpcbind
[root@nfs ~]# yum install nfs-utils rpcbind -y
# 创建用户
[root@nfs ~]# useradd www -u1000
# 创建前端代码仓库、数据库备份、上传文件的备份
[root@nfs ~]# mkdir /nfs
[root@nfs ~]# mkdir /nfs/web
[root@nfs ~]# mkdir /nfs/database
[root@nfs ~]# mkdir /nfs/download
[root@nfs ~]# tree /nfs/
/nfs/
├── database
├── download
└── web
# 授权
[root@nfs ~]# chown www.www -R /nfs/
# 设置挂载点
[root@nfs ~]# vim /etc/exports
/nfs/web 172.16.1.0/20(rw,sync,all_squash,anonuid=1000,anongid=1000)
/nfs/database 172.16.1.0/20(rw,sync,all_squash,anonuid=1000,anongid=1000)
/nfs/download 172.16.1.0/20(rw,sync,all_squash,anonuid=1000,anongid=1000)
# 启动
[root@nfs ~]# systemctl enable --Now nfs-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
# 检测
[root@nfs ~]# showmount -e
Export list for nfs:
/nfs/download 172.16.1.0/20
/nfs/database 172.16.1.0/20
/nfs/web 172.16.1.0/20
[root@nfs ~]# cat /var/lib/nfs/etab #另外一种检测的命令
4、搭建数据库(mariadb)
# 安装软件
[root@db01 ~]# yum install mariadb* -y
# 启动
[root@db01 ~]# systemctl enable --Now mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# 创建密码并登陆数据库
[root@db01 ~]# MysqLadmin -uroot password '123'
[root@db01 ~]# MysqL -uroot -p123
# 创建用户给予web以及其它网站使用(授权)
MariaDB [MysqL]> grant all privileges on *.* to dandan@'%' identified by 'dandan';
Query OK, 0 rows affected (0.01 sec)
#重载数据库
MariaDB [MysqL]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#备份数据库
[root@db01 ~]# MysqLdump -uroot -p111 --all-databases --single-transaction > MysqL-all.sql
[root@db01 ~]# cat MysqL-all.sql
# 编写每天备份脚本
[root@db01 ~]# vim MysqL_dump.sh
#!/bin/bash
DATE=`date +%F`
BACKUP="/databases"
cd $BACKUP
MysqLdump -uroot -p123 --all-databases --single-transaction > MysqL-all-${DATE}.sql
tar -czf MysqL-all-${DATE}.tar.gz MysqL-all-${DATE}.sql
rm -rf MysqL-all-${DATE}.sql
[root@db01 ~]# chmod 600 MysqL_dump.sh #授权
[root@db01 ~]# mkdir /databases #创建目录
[root@db01 ~]# useradd www -u1000
[root@db01 ~]# chown www.www /databases/
[root@db01 ~]# mount -t nfs 172.16.1.31:/nfs/database /databases/ #挂载
# 脚本加入定时任务
[root@db01 ~]# crontab -e
01 00 * * * /databases/MysqL_dump.sh
5、搭建web服务器(点击xshell中的工具—>发送键输入到所有会话,web01-03)
# 安装官方源
[root@web01 ~]# vim /etc/yum.repos.d/Nginx.repo
[Nginx-stable]
name=Nginx stable repo
baseurl=http://Nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://Nginx.org/keys/Nginx_signing.key
module_hotfixes=true
[Nginx-mainline]
name=Nginx mainline repo
baseurl=http://Nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://Nginx.org/keys/Nginx_signing.key
module_hotfixes=true
[root@web01 ~]# yum clean all
[root@web01 ~]# yum makecache
# 安装Nginx
[root@web01 ~]# yum install -y Nginx
# 启动
[root@web01 ~]# systemctl enable --Now Nginx
6、安装PHP
# 安装PHP #最好用安装包安装
[root@web01 ~]# vim /etc/yum.repos.d/PHP.repo
[PHP-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
[root@web01 ~]# yum clean all
[root@web01 ~]# yum makecache
[root@web01 ~]# yum remove PHP-MysqL-5.4 PHP PHP-fpm PHP-common
[root@web01 ~]# yum -y install PHP71w PHP71w-cli PHP71w-common PHP71w-devel
PHP71w-embedded PHP71w-gd PHP71w-mcrypt PHP71w-mbstring PHP71w-pdo PHP71wxml PHP71w-fpm PHP71w-MysqLnd PHP71w-opcache PHP71w-pecl-memcached PHP71wpecl-redis PHP71w-pecl-mongodb
# 修改配置文件
[root@web01 ~]# vim /etc/PHP-fpm.d/www.conf
user = www
group = www
# 启动PHP
[root@web01 ~]# systemctl enable --Now PHP-fpm.service
7、将web01-web03加入集群
[root@web02 ~]# mkdir /www
[root@web02 ~]# chown -R www.www /www/
[root@web02 ~]# mount -t nfs 172.16.1.31:/nfs/web /www
[root@web02 ~]# mount -t nfs 172.16.1.31:/nfs/conf /etc/Nginx/conf.d/
[root@web02 ~]# systemctl restart Nginx
8、搭建wordpress,搭建WeCenter
# 共享代码、共享数据、共享Nginx配置
# 在nfs服务器上创建/nfs/conf目录
[root@nfs ~]# mkdir /nfs/conf
# 授权/nfs/conf
[root@nfs ~]# chown www.www /nfs/conf
# 加入nfs配置文件
[root@nfs ~]# vim /etc/exports
/nfs/conf 172.16.1.0/20(rw,sync,all_squash,anonuid=1000,anongid=1000)
# 重启nfs
[root@nfs ~]# systemctl restart nfs-server rpcbind
# 创建站点目录,授权,挂载,上传,解压
[root@web01 ~]# mkdir /www
[root@web01 ~]# chown www.www -R /www
[root@web01 ~]# mount -t nfs 172.16.1.31:/nfs/web /www
[root@web01 ~]# mount -t nfs 172.16.1.31:/nfs/conf /etc/Nginx/conf.d
[root@web01 ~]# cd /www
[root@web01 www]# rz -E
rz waiting to receive
[root@web01 www]# unzip zhihu.zip
# 增加wordpress配置
[root@web01 ~]# vim /etc/Nginx/conf.d/wordpress.conf
server {
listen 80;
server_name linux.wps.cluster.local.com;
root /www/wordpress;
location / {
index index.PHP;
}
location ~* \.PHP$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# vim /etc/Nginx/conf.d/wecenter.conf
server {
listen 80;
server_name linux.wecenter.cluster.local.com;
root /www/zhihu;
location / {
index index.PHP;
}
location ~* \.PHP$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 重启
[root@web01 ~]# systemctl restart Nginx
# 创建wordpress数据库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create database wecenter;
9. 添加Nginx访问认证模块
[root@web01 ~]# htpasswd -c /etc/Nginx/conf.d/auth_basic linux #创建用户并存密码
[root@web01 ~]# vim /etc/Nginx/conf.d/wordpress.conf #配置访问登录
server {
listen 80;
server_name linux.wps.cluster.local.com;
root /www/wordpress;
client_max_body_size 10m;
location / {
index index.PHP;
}
location ~* \.PHP$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location =/wp-admin {
auth_basic "please input password!";
auth_basic_user_file /etc/Nginx/conf.d/auth_basic;
index index.PHP;
}
}
10.数据备份与同步
#上传实时备份软件sersync
[root@nfs ~]# cd /nfs/download
[root@nfs download]# rz -E
rz waiting to receive.
[root@nfs download]# tar -xf sersync.gz
[root@nfs download]# cd GNU-Linux-x86/
[root@nfs GNU-Linux-x86]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveto start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/nfs/download">
<remote ip="172.16.1.41" name="download"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="dandan" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timetoExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.PHP"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.PHP"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
[root@nfs GNU-Linux-x86]# echo "111"> /etc/rsync.passwd
[root@nfs GNU-Linux-x86]# ./sersync2 -dro confxml.xml
[root@nfs ~]# cd /nfs/database
[root@nfs download]# rz -E
rz waiting to receive.
[root@nfs download]# tar -xf sersync.gz
[root@nfs download]# cd GNU-Linux-x86/
[root@nfs GNU-Linux-x86]# vim confxml.xml
cat confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveto start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/nfs/database">
<remote ip="172.16.1.41" name="database"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="ytt" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timetoExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.PHP"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.PHP"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
[root@nfs GNU-Linux-x86]# ./sersync2 -dro confxml.xml #开启resync[]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。