三、搭建wordpress、知乎、edusoho
1.上传代码包
[root@web01 ~]# mkdir /code [root@web01 ~]# cd /code/ [root@web01 /code]# rz [root@web01 /code]# ll total 86372 -rw-r--r-- 1 root root 68889387 Aug 26 21:41 edusoho-8.3.36.tar.gz -rw-r--r-- 1 root root 8451194 Aug 26 21:40 WeCenter_3-2-1.zip -rw-r--r-- 1 root root 11098483 Mar 22 22:41 wordpress-5.0.3-zh_CN.tar.gz
2.解压
[root@web01 /code]# tar xf edusoho-8.3.36.tar.gz [root@web01 /code]# tar xf wordpress-5.0.3-zh_CN.tar.gz [root@web01 /code]# yum install -y unzip [root@web01 /code]# unzip WeCenter_3-2-1.zip [root@web01 /code]# mv WeCenter_3-2-1 zh
3.授权代码
[root@web01 /code]# chown -R www.www /code/
4.配置nginx
1)wordpress的nginx配置
[root@web01 /code]# vim /etc/nginx/conf.d/linux.blog.com.conf server { listen 80; server_name linux.blog.com; root /code/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; } }
2)wecenter的nginx配置
[root@web01 /code]# vim /etc/nginx/conf.d/linux.zh.com.conf server { listen 80; server_name linux.zh.com; root /code/zh; 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; } }
3)wecenter的nginx配置
server { listen 80; server_name linux.edu.com; root /code/edusoho/web; location / { index app.php; try_files $uri @rewriteapp; } location @rewriteapp { rewrite ^(.*)$ /app.php/$1 last; } location ~ ^/udisk { internal; root /var/www/edusoho/app/data/; } location ~ ^/(app|app_dev)\.php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect; fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk; fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; } # 配置设置图片格式文件 location ~* \.(jpg|jpeg|gif|png|ico|swf)$ { # 过期时间为3年 expires 3y; # 关闭日志记录 access_log off; # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。 gzip off; } # 配置css/js文件 location ~* \.(css|js)$ { access_log off; expires 3y; } # 禁止用户上传目录下所有.php文件的访问,提高安全性 location ~ ^/files/.*\.(php|php5)$ { deny all; } # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。 location ~ \.php$ { # [改] 请根据实际php-fpm运行的方式修改 fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } }
5.配置本地hosts文件
10.0.0.7 linux.blog.com
10.0.0.7 linux.zh.com
10.0.0.7 linux.edu.com
6.重启访问测试
#检查配置 [root@web01 /code]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #重启 [root@web01 /code]# systemctl restart nginx
7.数据库建库
[root@web01 /code]# mysql -uroot -pLinhd@123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.01 sec) MariaDB [(none)]> create database wordpress; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> create database zh; Query OK, 1 row affected (0.00 sec)
8.根据页面提示操作
http://linux.blog.com
http://linux.zh.com
http://linux.zh.com
四、拆分数据库
1.为什么要拆分
由于单台服务器运行LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom从而kill掉MySQL数据库,所以要将web和数据库进行独立部署。
2.数据库拆分解决的问题
1.缓解网站压力
2.增强数据库读写性能
3.提高用户访问速度
3.环境准备
主机 | IP | 服务 |
---|---|---|
web01 | 172.16.1.7 | nginx、php |
db01 | 172.16.1.51 | mariadb |
4.在新的服务器上搭建数据库(盖新房子)
[root@db01 ~]# yum install -y mariadb-server
5.配置启动(装修)
[root@db01 ~]# systemctl start mariadb [root@db01 ~]# systemctl enable mariadb
6.配置密码(换锁)
[root@db01 ~]# mysqladmin -uroot password "Linhd@123"
7.连接测试(尝试居住)
#本机连接 [root@db01 ~]# mysql -uroot -pLinhd@123 #远程连接测试 [root@web01 ~]# mysql -uroot -pLinhd@123 -h172.16.1.51 ERROR 1130 (HY000): Host '172.16.1.7' is not allowed to connect to this MariaDB server mysql #命令 -u #指定用户 root #数据库用户 -p #指定密码 Linhd@123 #数据库用户的密码 -h #指定主机 172.16.1.51 #远端数据库的主机地址
8.授权远程连接数据库(想办法住)
#连接数据库 [root@db01 ~]# mysql -uroot -pLinhd@123 #授权 MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) grant #授权命令 all #所有权限 on #在...上 *.* #所有库.所有表 to #给... root@'172.16.1.%' #用户 root@'172.16.1.%' root用户通过172.16.1.0-255连接都允许 identified #指定密码 by #是 '123456'; #密码
9.再次远程连接(再尝试居住)
[root@web01 ~]# mysql -uroot -p123456 -h172.16.1.51 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
10.旧库数据导出(打包般出家门)
[root@web01 ~]# mysqldump -uroot -pLinhd@123 -B wordpress > /tmp/wordpress.sql [root@web01 ~]# mysqldump -uroot -pLinhd@123 -B zh > /tmp/zh.sql [root@web01 ~]# mysqldump -uroot -pLinhd@123 -B edusoho > /tmp/edu.sql
11.将数据推送至新库(搬到新家门口)
[root@web01 ~]# scp /tmp/*.sql 172.16.1.51:/tmp
12.将数据导入新库(把物品搬到新家里面)
1)方式一:库外导入
[root@db01 ~]# mysql -uroot -pLinhd@123 < /tmp/wordpress.sql [root@db01 ~]# mysql -uroot -pLinhd@123 < /tmp/zh.sql [root@db01 ~]# mysql -uroot -pLinhd@123 < /tmp/edu.sql
2)方式二:库内读取
MariaDB [wordpress]> source /tmp/wordpress.sql; MariaDB [wordpress]> source /tmp/zh.sql; MariaDB [wordpress]> source /tmp/edu.sql;
3)方式三:传送门
[root@web01 ~]# mysql -uroot -p123456 -h172.16.1.51 < /tmp/wordpress.sql [root@web01 ~]# mysql -uroot -p123456 -h172.16.1.51 < /tmp/zh.sql [root@web01 ~]# mysql -uroot -p123456 -h172.16.1.51 < /tmp/edu.sql
13.查看数据迁移完成(有没有忘记物品)
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | edusoho | | mysql | | performance_schema | | test | | wordpress | | zh | +--------------------+ 7 rows in set (0.00 sec)
14.修改项目的数据库地址(告诉亲戚新住址)
1)wordpress数据库地址
[root@web01 ~]# vim /code/wordpress/wp-config.php /** WordPress数据库的名称 */ define('DB_NAME', 'wordpress'); /** MySQL数据库用户名 */ define('DB_USER', 'root'); /** MySQL数据库密码 */ define('DB_PASSWORD', '123456'); /** MySQL主机 */ define('DB_HOST', '172.16.1.51');
2)wecenter数据库地址
[root@web01 ~]# vim /code/zh/system/config/database.php <?php $config['charset'] = 'utf8';^M $config['prefix'] = 'aws_';^M $config['driver'] = 'MySQLi';^M $config['master'] = array ( 'charset' => 'utf8', 'host' => '172.16.1.51', 'username' => 'root', 'password' => '123456', 'dbname' => 'zh', );^M $config['slave'] = false;^M
3)edusoho数据库地址
[root@web01 ~]# vim /code/edusoho/app/config/parameters.yml parameters: database_driver: pdo_mysql database_host: 172.16.1.51 database_port: 3306 database_name: edusoho database_user: root database_password: '123456' #edusoho修改后需要清除cache缓存 [root@web01 ~]# rm -rf /code/edusoho/app/cache/*
15.停止旧库
[root@web01 ~]# systemctl stop mariadb
16.访问页面测试
linux.blog.com
linux.zh.com
linux.edu.com
五、扩展web服务器
1.配置官方源
[root@web03 ~]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
2.yum安装依赖
3.yum安装nginx
[root@web03 ~]# yum install -y nginx
4.配置nginx
[root@web01 ~]# scp -r /etc/nginx/.* 172.16.1.9:/etc/nginx/
5.创建用户
[root@web03 ~]# groupadd www -g 666 [root@web03 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
6.启动并加入开机自启
[root@web03 ~]# systemctl start nginx [root@web03 ~]# systemctl enable nginx #验证 [root@web01 ~]# ps -ef | grep nginx
7.上传php安装包
[root@web03 ~]# mkdir /package [root@web03 ~]# cd /package/ [root@web03 /package]# rz
8.解压安装包
[root@web03 /package]# tar xf php.tar.gz
9.卸载旧版本PHP
[root@web03 /package]# yum remove php-mysql-5.4 php php-fpm php-common
10.安装新版本PHP
[root@web03 /package]# yum localinstall -y *.rpm
11.配置
[root@web01 ~]# scp /etc/php-fpm.d/www.conf 172.16.1.9:/etc/php-fpm.d/ [root@web01 ~]# scp /etc/php.ini 172.16.1.9:/etc/
12.启动并加入开机自启
[root@web03 ~]# systemctl start php-fpm [root@web03 ~]# systemctl enable php-fpm
13.同步站点目录文件
#web01打包 [root@web01 ~]# tar zcf code.tar.gz /code [root@web01 ~]# scp -r code.tar.gz 172.16.1.9:/ #web03解压 [root@web03 ~]# cd / [root@web03 /]# tar xf code.tar.gz
14.授权站点目录
[root@web03 ~]# chown -R www.www /code/
15.配置本地hosts
#10.0.0.7 www.zuoye.com linux.blog.com linux.zh.com linux.edu.com 10.0.0.9 www.zuoye.com linux.blog.com linux.zh.com linux.edu.com
16.访问测试
#问题: web01上传的图片,web02看不到 web02上传的图片,web01看不到
六、文件共享
1.搭建nfs服务端
1)安装
[root@nfs ~]# yum install -y rpcbind nfs-utils
2)规划目录
1.wordpress文件目录: [root@nfs ~]# mkdir /data/wp -p 2.zh文件目录: [root@nfs ~]# mkdir /data/zh -p 3.edu文件目录: [root@nfs ~]# mkdir /data/edu -p
3)配置NFS
[root@nfs ~]# vim /etc/exports /data/wp 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) /data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
4)创建用户
[root@nfs ~]# groupadd www -g 666 [root@nfs ~]# useradd www -u 666 -g 666
5)授权
[root@nfs ~]# chown -R www.www /data/
6)启动服务
[root@nfs ~]# systemctl start rpcbind nfs [root@nfs ~]# systemctl enable rpcbind nfs
7)查看配置是否正确
[root@nfs ~]# cat /var/lib/nfs/etab /data/edu 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash) /data/zh 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash) /data/wp 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)
2.客户端
1)安装nfs
[root@web01 ~]# yum install -y rpcbind nfs-utils [root@web03 ~]# yum install -y rpcbind nfs-utils
2)启动rpcbind
[root@web01 ~]# systemctl start rpcbind [root@web03 ~]# systemctl start rpcbind
3)查看挂载点
[root@web01 ~]# showmount -e 172.16.1.31 Export list for 172.16.1.31: /data/edu 172.16.1.0/24 /data/zh 172.16.1.0/24 /data/wp 172.16.1.0/24
4)先推送目录下文件
[root@web01 ~]# scp -r /code/wordpress/wp-content/uploads/* 172.16.1.31:/data/wp/ [root@web03 ~]# scp -r /code/wordpress/wp-content/uploads/* 172.16.1.31:/data/wp/ [root@web01 ~]# scp -r /code/zh/uploads/* 172.16.1.31:/data/zh/ [root@web03 ~]# scp -r /code/zh/uploads/* 172.16.1.31:/data/zh/
5)挂载
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads [root@web01 ~]# mount -t nfs 172.16.1.31:/data/zh /code/zh/uploads [root@web03 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads [root@web03 ~]# mount -t nfs 172.16.1.31:/data/zh /code/zh/uploads [root@web01 ~]# df -h 172.16.1.31:/data/wp 98G 1.9G 96G 2% /code/wordpress/wp-content/uploads 172.16.1.31:/data/zh 98G 1.9G 96G 2% /code/zh/uploads
七、数据文件实时备份
1.服务端(backup)
1)安装
[root@backup ~]# yum install -y rsync
2)配置rsync
[root@backup ~]# vim /etc/rsyncd.conf uid = www gid = www port = 873 fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = false auth users = rsync_backup secrets file = /etc/rsync.passwd log file = /var/log/rsyncd.log ##################################### [data] comment = welcome to oldboyedu backup! path = /data
3)创建用户
[root@backup ~]# groupadd www -g 666 [root@backup ~]# useradd www -u 666 -g 666
4)创建密码文件
[root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd [root@backup ~]# chmod 600 /etc/rsync.passwd
5)创建目录
[root@backup ~]# mkdir /data [root@backup ~]# chown -R www.www /data/
6)启动
[root@backup ~]# systemctl start rsyncd [root@backup ~]# ps -ef | grep rsync
2.客户端
1)安装
[root@nfs ~]# yum install -y rsync inotify-tools #安装sersync [root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/sersync
2)配置sersync
[root@nfs ~]# vim /usr/local/sersync/confxml.xml
3)创建密码文件
[root@nfs ~]# echo 123456 > /etc/rsync.password [root@nfs ~]# chmod 600 /etc/rsync.password
4)启动
[root@nfs /data]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
3.测试
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。