一、回顾
1.搭建LNMP环境
1.配置官方源
2.yum安装依赖
3.yum安装nginx
4.配置nginx
5.创建用户
6.启动并加入开机自启
7.上传安装包
8.解压安装包
9.卸载旧版本PHP
10.安装新版本PHP
11.配置
12.启动并加入开机自启
13.yum安装mariadb-server
14.启动并加入开机自启
15.连接验证
16.设置登陆密码
17.使用密码登录
2.搭建wordpress、知乎、edusoho
1.上传代码包
2.解压
3.授权代码
4.配置nginx
5.配置本地hosts文件
6.重启访问测试
7.数据库建库
8.根据页面提示操作
二、搭建LNMP
1.配置官方源
[root@web01 ~]# 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@web01 ~]# yum install -y nginx
4.配置nginx
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;
... ...
http {
... ...
client_max_body_size 200m;
... ...
}
5.创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
6.启动并加入开机自启
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
#验证
[root@web01 ~]# ps -ef | grep nginx
7.上传php安装包
[root@web01 ~]# mkdir /package
[root@web01 ~]# cd /package/
[root@web01 /package]# rz
8.解压安装包
[root@web01 /package]# tar xf php.tar.gz
9.卸载旧版本PHP
[root@web01 /package]# yum remove php-mysql-5.4 php php-fpm php-common
10.安装新版本PHP
[root@web01 /package]# yum localinstall -y *.rpm
11.配置
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www
[root@web01 ~]# vim /etc/php.ini
post_max_size = 100M
upload_max_filesize = 100M
#如果nginx和php不在一台机器,需要额外配置
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
listen = 172.16.1.7:9000
listen.allowed_clients = 172.16.1.8
12.启动并加入开机自启
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
13.yum安装mariadb-server
[root@web01 ~]# yum install -y mariadb-server
14.启动并加入开机自启
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
15.连接验证
[root@web01 ~]# mysql
16.设置登陆密码
[root@web01 ~]# mysqladmin -uroot password 'Linhd@123'
17.使用密码登录
[root@web01 ~]# mysql -u root -pLinhd@123
三、搭建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)edusoho的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.搭建两台nginx
2.数据库单独部署
3.搭建博客和知乎
4.文件实现共享
1.搭建两台Nginx与PHP
1.Nginx
# 安装nginx
[root@web01 ~]# 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
# 依赖安装
[root@web01 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
[root@web01 ~]# yum install -y nginx
# 配置Nginx
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;
... ...
http {
... ...
client_max_body_size 200m;
... ...
}
# 创建用户
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
# 启动Nginx
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
# 查看是否启动
[root@web01 ~]# ps -ef | grep nginx
2.PHP
# 上传php安装包
[root@web01 ~]# mkdir /package
[root@web01 ~]# cd /package/
[root@web01 /package]# rz php.tar.gz
# 解压安装包
[root@web01 /package]# tar xf php.tar.gz
# 卸载旧版本PHP
[root@web01 /package]# yum remove -y php-mysql-5.4 php php-fpm php-common
# 安装新版本PHP
[root@web01 /package]# yum localinstall -y *.rpm
# 配置PHP
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www
# 修改上传文件大小
[root@web01 ~]# vim /etc/php.ini
post_max_size = 100M
upload_max_filesize = 100M
# 如果nginx和php不在一台机器,需要额外配置
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
listen = 172.16.1.7:9000
listen.allowed_clients = 172.16.1.8
# 启动并加入开机自启
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
3.重复上面布置web02
2.数据库部署
# yum安装mariadb-server
[root@db01 ~]# yum install -y mariadb-server
# 启动并加入开机自启
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
# 连接验证
[root@db01 ~]# mysql
# 设置登陆密码
[root@db01 ~]# mysqladmin -uroot password '123456'
# 使用密码登录
[root@db01 ~]# mysql -u root -p123456
MariaDB [(none)]> show databases;
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> create database zh;
MariaDB [(none)]> show databases;
#授权
MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
#远程连接测试
[root@web01 ~]# mysql -uroot -p123456 -h172.16.1.51
[root@web02 ~]# mysql -uroot -p123456 -h172.16.1.51
3.搭建博客和知乎
1.安装配置WP和ZH
# 上传代码包
[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code/
[root@web01 /code]# rz
[root@web01 /code]# ll
-rw-r--r-- 1 root root 11098483 Mar 22 22:41 wordpress-5.0.3-zh_CN.tar.gz
-rw-r--r-- 1 root root 8451194 Aug 26 21:40 WeCenter_3-2-1.zip
[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
# 修改权限
[root@web01 /code]# chown -R www.www /code/
# 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;
}
}
# 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;
}
}
2.web02
# wordpress的nginx配置
[root@web01 /code]# vim /etc/nginx/conf.d/linux.blog.com.conf
server {
listen 80;
server_name xxs.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;
}
}
# wecenter的nginx配置
[root@web01 /code]# vim /etc/nginx/conf.d/linux.zh.com.conf
server {
listen 80;
server_name xxs.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;
}
}
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# systemctl restart php-fpm.service
[root@web01 /code]# tar czf ./wordpress.tar.gz ./wordpress/
[root@web01 /code]# tar czf ./zh.tar.gz ./zh/
[root@web01 /code]# scp -r /code/* 172.16.1.8:/code/
[root@web02 /code]# tar xf wordpress.tar.gz
[root@web02 /code]# tar xf zh.tar.gz
4.文件实现共享
# 安装NFS
[root@nfs ~]# yum install -y rpcbind nfs-utils
# 创建用户和目录并授权
[root@nfs ~]# mkdir -p /data/{wp,zh}
[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666 -g 666
[root@nfs ~]# chown -R www.www /data/
# 配置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)
# 启动NFS和rpcbind
[root@nfs ~]# systemctl start rpcbind nfs
[root@nfs ~]# systemctl enable rpcbind nfs
# 检查配置是否正确
[root@nfs ~]# cat /var/lib/nfs/etab
/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)
# 客户端安装nfs和rpcbind
[root@web01 ~]# yum install -y rpcbind nfs-utils
[root@web02 ~]# yum install -y rpcbind nfs-utils
# 启动NFS和rpcbind
[root@web01 ~]# systemctl start rpcbind
[root@web02 ~]# systemctl start rpcbind
# 查看可挂载点
[root@web01 ~]# showmount -e 172.16.1.31
[root@web02 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/wp 172.16.1.0/24
# 进行挂载
[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@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /code/wordpress/wp-content/uploads
[root@web02 ~]# 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
测试
# 本地hosts(C:WindowsSystem32driversetchosts)
10.0.0.7 linux.blog.com linux.zh.com
10.0.0.8 xxs.blog.com xxs.zh.com
# 浏览器查看
linux.blog.com
xxs.zh.com