linux高级运维

案例1:搭建Nginx服务器

步骤一:构建Nginx服务器
1)使用源码包安装nginx软件包
[root@localhost ~]# yum -y install gcc pcre-devel openssl-devel              //安装依赖包
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# cd 桌面
[root@localhost 桌面]# ls
nginx-1.22.1.tar.gz
[root@localhost 桌面]# tar -xf nginx-1.22.1.tar.gz            //解包
[root@localhost 桌面]# cd nginx-1.22.1/
[root@localhost nginx-1.22.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.22.1]# useradd -s /sbin/nologin nginx                   //指定账户不让登陆
[root@localhost nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module               //检查环境    //指定安装路径 默认的路径 可要可不要 //指定用户(以这个用户来执行这些,防止别的用户操作,为了安全)  //指定组  //开启SSL加密功能
[root@localhost nginx-1.22.1]# make&&make install    //编译并安装,两条命令一起执行用&&连接,也可以分开执行
[root@localhost nginx-1.22.1]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@localhost nginx-1.22.1]# ls /usr/local/nginx/sbin/
nginx
2)nginx命令的用法
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx        //启动服务
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s stop              //关闭服务
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx              //启动
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload                  //重新加载配置文件,服务必须启动才能重新加载
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx -V               //查看软件信息
3)  设置防火墙与SELinux
[root@localhost nginx-1.22.1]# systemctl stop firewalld
[root@localhost nginx-1.22.1]# setenforce 0
4)  测试首页文件
Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下默认有一个名为index.html的文件,使用客户端访问测试页面:

案例2:用户认证


通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证。最后使用htpasswd命令创建用户及密码即可。

步骤一:修改Nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
2.    .. ..
3.    server {
4.            listen       80;
5.            server_name  localhost;
6.            auth_basic "zhu ren:";                     //认证提示符
7.             auth_basic_user_file "/usr/local/nginx/pass";       //认证密码文件
8.            location / {
9.                root   html;
10.                index  index.html index.htm;
11.            }
12.      }
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/pass tom           //创建密码文件    用户可随意创建:-c 创建用户的
New password: 
Re-type new password: 
Adding password for user tom
[root@localhost ~]# /usr/local/nginx/sbin/nginx 
root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload       //重新加载配置文件    
2.    //请先确保nginx是启动状态才可以执行命令成功,否则报错


案例3:基于域名的虚拟主机


1)修改Nginx服务配置,添加相关虚拟主机配置如下
1.    [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
2.    .. ..
3.    server {
4.            listen       80;                                      //端口
5.            server_name  www.a.com;                            //域名
6.    auth_basic "Input Password:";                        //认证提示符
7.            auth_basic_user_file "/usr/local/nginx/pass";        //认证密码文件
8.    location / {
9.                root   html;                                    //指定网站根路径
10.                index  index.html index.htm;
11.           }
12.           
13.    }
14.    … …
15.    
16.        server {
17.            listen  80;                                        //端口
18.            server_name  www.b.com;                            //域名
19.    location / { 
20.    root   web;                                 //指定网站根路径
21.    index  index.html index.htm;
22.    }
[root@localhost ~]# mkdir /usr/local/nginx/web                       //创建网页根目录
[root@localhost ~]# echo "web" > /usr/local/nginx/web/index.html             //写测试页面
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload       //重新加载
客户机测试:
[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.134   www.a.com www.b.com


案例4:SSL虚拟主机(加密)


步骤一:配置SSL虚拟主机
1)生成私钥与证书
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# openssl genrsa > cert.key         //生成私钥
[root@localhost conf]# openssl req -new -x509 -key cert.key > cert.pem       //req:申请,-new:新建,-x509:证书格式,-key cert.key指定私钥,生成证书(加了签名的公钥)
Country Name (2 letter code) [XX]:CN           #国家名字
State or Province Name (full name) []:qingyang             #省份
Locality Name (eg, city) [Default City]:qingyang              #城市
Organization Name (eg, company) [Default Company Ltd]:tarena                #公司名称
Organizational Unit Name (eg, section) []:tech          #部门名称
Common Name (eg, your name or your server's hostname) []:web               #你的名字或服务器的名字
Email Address []:test@test.com                #邮箱
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf                        //修改Nginx配置文件,设置加密网站的虚拟主机
… …    
    server {
        listen       443 ssl;
        server_name  www.c.com;
        ssl_certificate      cert.pem;    #这里是证书文件
        ssl_certificate_key  cert.key;        #这里是私钥文件
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            root   html;
            index  index.html index.htm;
        }
}
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload          //重新加载

客户机测试
[root@localhost ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.134   www.a.com www.b.com www.c.com
火狐浏览器访问:https://www.c.com


案例5:  nginx代理


1.环境准备
2.部署web服务器
web1:
[root@localhost ~]# yum -y install php php-mysql php-fpm
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php index.html index.htm;

server {
 location  ~  \.php$  {
            root           html;
            fastcgi_pass   127.0.0.1:9000;    #将请求转发给本机9000端口,PHP解释器
            fastcgi_index  index.php;
            #fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi.conf;       #加载其他配置文件
        }
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# vim /usr/local/nginx/html/test.php
<?php
$i=33;
echo $i;
?>
web2:
和web1相同
[root@localhost ~]# vim /usr/local/nginx/html/test.php
<?php
$i=88;
echo $i;
?>
1)部署代理服务器(七层):
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
upstream servers {            #upstream模块要写到http的里面
        server 192.168.2.100:80;
        server 192.168.2.200:80;
}
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://servers;    #通过proxy_pass将用户的请求转发给servers集群,他的语句优先级高于root,所以放root前面和后面都可以
            root   html;
            index   index.html index.htm;
        }
[root@localhost ~]# /usr/local/nginx/sbin/nginx 
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
浏览器测试,反复访问代理服务器测试效果
http://proxy的地址/test.php

2)测试健康检查功能:
模拟web故障,将web1 的nginx关闭
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s stop
测试,访问的是web2的内容
http://192.168. 4.5/ test. php
将web1 的nginx启动
[root@web1 ~]# /usr/local/nginx/sbin/nginx 
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s reload
测试,web1和web2的内容出现
http://192.168. 4.5/test. php

3)配置upstream服务器集群池属性

案例6:  nginx代理(四层)及优化:


1)部署支持4层TCP/UDP代理的Nginx服务器
[root@proxy ~]# rm -rf /usr/local/nginx/        //清理之前七层代理的环境
killall  nginx           //彻底杀死之前的环境
编译安装必须要使用--with-stream参数开启4层代理模块
[root@localhost nginx-1.22.1]# ./configure --with-http_ssl_module  --with-stream        //开启SSL加密功能,开启4层反向代理功能
[root@localhost nginx-1.22.1]#  make && make install        //编译并安装
[root@localhost nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf        //不能在配置到http里面
stream {
        upstream backend {
                server 192.168.2.131:22;    //后端SSH服务器的IP和端口
                server 192.168.2.133:22;
        }
        server {
                listen 12345;                //Nginx监听的端口
                proxy_pass backend;
        }
}
http {
.. ..
}
[root@localhost nginx-1.22.1]#  /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.22.1]#  /usr/local/nginx/sbin/nginx -s reload
测试:
客户端使用访问代理服务器测试轮询效果
[root@localhost nginx-1.22.1]# ssh 192.168.4.133 -p 12345
root@192.168.4.5's password: 
[root@web1 ~]# exit
[root@localhost nginx-1.22.1]# ssh 192.168.4.133 -p 12345
root@192.168.4.5's password: 
[root@web2 ~]#exit
2)nginx的优化方案(上)

P1 自定义错误页面

[root@localhost nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf        //修改Nginx配置文件,自定义报错页面
.. ..
        error_page  404              /404.html;     //打开注释,自定义错误页面
.. ..
[root@localhost nginx-1.22.1]# vim /usr/local/nginx/html/404.html
tdr!!!
[root@localhost nginx-1.22.1]#  /usr/local/nginx/sbin/nginx -s reload    //重新加载
浏览器访问: 192.168.4.133
继续访问:192.168.4.133/vgdsvhd
换成图片
[root@localhost 桌面]# ls
nginx-1.22.1  nginx-1.22.1.tar.gz  狗头.jpg
[root@localhost 桌面]# cp 狗头.jpg  /usr/local/nginx/html
[root@localhost 桌面]# vim /usr/local/nginx/conf/nginx.conf
    error_page  404              /狗头.jpg;
[root@localhost 桌面]#  /usr/local/nginx/sbin/nginx -s reload

  P2 nginx状态页面

如何查看服务器状态信息(非常重要的功能)
编译安装时使用--with-http_stub_status_module开启状态页面模块
已升级的形式进行安装
注释掉四层代理做过的配置
#stream {
 #       upstream backend {
  #              server 192.168.2.131:22;       
   #             server 192.168.2.133:22;
    #    }
    # server {
    #           listen 12345;                          
  #          proxy_pass backend;
  # }
#}
[root@localhost ~]# cd 桌面
[root@localhost 桌面]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost 桌面]# cd nginx-1.22.1/
[root@localhost nginx-1.22.1]# ./configure --with-http_ssl_module  --with-http_stub_status_module      //开启status状态页面
[root@localhost nginx-1.22.1]# make&&make install     //编译安装
[root@localhost nginx-1.22.1]# ls 
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@localhost nginx-1.22.1]#  mv  /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old    //进行备份
[root@localhost nginx-1.22.1]#  cp objs/nginx /usr/local/nginx/sbin/           //objs下存放二进制程序,更新版本
[root@localhost nginx-1.22.1]# make upgrade                 检查nginx是否正常
[root@localhost nginx-1.22.1]#  /usr/local/nginx/sbin/nginx -V
修改配置文件
[root@localhost nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf          //新添加
location /status {
                stub_status on;
        }

[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
优化后,查看状态页面信息
Active connections:当前活动的连接数量(实时并发连接数)。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量。(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。
[root@localhost nginx-1.22.1]# curl 192.168.4.133/status
Active connections: 2 
server accepts handled requests
 2 2 3                          //nginx的累计值,
Reading: 0 Writing: 1 Waiting: 1

3) nginx的优化方案(下)

P1 优化并发连接数

优化前使用ab高并发测试
[root@localhost nginx-1.22.1]# rpm -qa httpd-tools
httpd-tools-2.4.6-80.el7.centos.x86_64
[root@localhost nginx-1.22.1]# ab -c 100 -n 100 http://192.168.2.132/    #-c代表总人数,-n代表总访问量,必须加/
[root@localhost nginx-1.22.1]# ab -c 2000 -n 2000 http://192.168.2.132/
socket: Too many open files (24)                //提示打开文件数量过多
修改Nginx配置文件,增加并发量
cpu处理器图形模式查看,改成2个
[root@localhost nginx-1.22.1]# lscpu
[root@localhost nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes  2;            //与CPU核心数量一致
events {
    worker_connections  5000;         //每个worker最大并发连接数
}
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost nginx-1.22.1]#  ab -c 2000 -n 2000 http://192.168.2.132/        //再次测试,依旧报错
优化Linux内核参数(最大文件数量)
[root@localhost nginx-1.22.1]# ulimit -a                         //查看所有属性值
open files                      (-n) 1024        //打开文件数量
[root@localhost nginx-1.22.1]# ulimit -n 100000            //更改打开的文件数量,修改内核限制(临时)
[root@localhost nginx-1.22.1]#  ab -c 2000 -n 2000 http://192.168.2.132/                 //成功

案例7:  LNMP动态网站

 

部署lnmp

[root@localhost ~]# yum -y install gcc pcre-devel openssl-devel             //安装依赖包
[root@localhost ~]# useradd -s /sbin/nologin nginx                 //指定账户不让登陆
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# cd 桌面
[root@localhost 桌面]# ls
nginx-1.22.1.tar.gz  wordpress-5.0.3-zh_CN.zip
[root@localhost 桌面]# tar -xf nginx-1.22.1.tar.gz                     //解包
[root@localhost 桌面]# ls 
nginx-1.22.1  nginx-1.22.1.tar.gz  wordpress-5.0.3-zh_CN.zip
[root@localhost 桌面]# cd nginx-1.22.1/
[root@localhost nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.22.1]# make&&make install                   //编译并安装
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx  
安装MariaDB,安装php解释器和php-fpmfastcgi接口,php连接数据库的扩展包
[root@localhost nginx-1.22.1]# yum -y install mariadb mariadb-server mariadb-devel php   php-mysql php-fpm
[root@localhost nginx-1.22.1]# systemctl restart mariadb                //启动MySQL服务,设置开机自启
[root@localhost nginx-1.22.1]# systemctl restart php-fpm                //启动PHP-FPM服务,设置开机自启
[root@localhost nginx-1.22.1]# systemctl enable mariadb
[root@localhost nginx-1.22.1]# systemctl enable php-fpm
构建LNMP平台(配置动静分离)
[root@localhost nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf           修改Nginx配置文件并启动服务
server {
        listen       80;
        server_name  www.tdr.com;
location / {
            root   html;
            index  index.php index.html index.htm;
          } 
 location  ~  \.php$  {
            root           html;
            fastcgi_pass   127.0.0.1:9000;    #将请求转发给本机9000端口,PHP解释器
            fastcgi_index  index.php;
            #fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi.conf;       #加载其他配置文件
        }
[root@localhost nginx-1.22.1]# vim /usr/local/nginx/html/test.php          //创建PHP测试页面
<?php
$i=33;
echo $i;
?>
[root@localhost nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost nginx-1.22.1]# systemctl restart php-fpm
[root@localhost nginx-1.22.1]# vim /etc/hosts 
 192.168.2.135 www.tdr.com       
[root@localhost nginx-1.22.1]# curl www.tdr.com/test.php          //测试
[root@localhost nginx-1.22.1]# mysql
MariaDB [(none)]> create database wordpress character set utf8mb4;
MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by 'wordpress';
MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.2.134' identified by 'wordpress';
MariaDB [(none)]> grant all on wordpress.* to wordpress@'%' identified by 'wordpress';
MariaDB [(none)]> flush privileges;        #刷新权限
MariaDB [(none)]> exit

[root@localhost nginx-1.22.1]# systemctl stop firewalld.service 
[root@localhost nginx-1.22.1]# setenforce 0
[root@localhost nginx-1.22.1]# cd
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# cd 桌面
[root@localhost 桌面]# unzip wordpress-5.0.3-zh_CN.zip 
[root@localhost 桌面]# ls
nginx-1.22.1  nginx-1.22.1.tar.gz  wordpress  wordpress-5.0.3-zh_CN.zip
[root@localhost 桌面]# cp -r wordpress/* /usr/local/nginx/html/
[root@localhost 桌面]# cd wordpress/
[root@localhost wordpress]# chown -R apache:apache /usr/local/nginx/html/
提示:动态网站运行过程中,php脚本需要对网站目录有读写权限,而php-fpm默认启动用户为apache。
初始化网站配置,发现显示的是nginx的默认首页,需要修改配置文件,更改首页

[root@localhost wordpress]# /usr/local/nginx/sbin/nginx -s reload

 

案例8:  Session与Cookie


web1:
环境准备,部署LNMP动静分离
[root@web1 ~]# yum -y install gcc pcre-devel openssl-devel        //安装依赖包
[root@web1 ~]# rm -rf /usr/local/nginx/
[root@web1 nginx-1.16.1]# ./configure --with-http_ssl_module
[root@web1 nginx-1.16.1]# make && make install
[root@web1 nginx-1.16.1]#  yum -y install   mariadb   mariadb-server   mariadb-devel php   php-fpm php-mysql

[root@web1 nginx-1.16.1]# vim /usr/local/nginx/conf/nginx.conf
……
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
启动Nginx服务, MySQL服务, PHP-FPM服务
[root@web1 nginx-1.16.1]# /usr/local/nginx/sbin/nginx
[root@web1 nginx-1.16.1]# systemctl restart mariadb
[root@web1 nginx-1.16.1]# systemctl restart php-fpm
[root@web1 nginx-1.16.1]# systemctl stop firewalld
[root@web1 nginx-1.16.1]# setenforce 0
部署测试页面
[root@web1~]# cd 桌面
[root@web1 桌面]# tar -xf lnmp_soft.tar.gz
[root@web1 桌面]#cd lnmp_soft/php_scripts/
[root@web1 php_scripts]# tar -xf php-memcached-demo.tar.gz 
[root@web1 php_scripts]# cd php-memcached-demo/
[root@web1 php-memcached-demo]# cp -r * /usr/local/nginx/html
浏览器访问192.168.2.131/index.php
在提交之前先查看,服务器本地的session信息
[root@web1 php-memcached-demo]# ls /var/lib/php/session/      //查看服务器本地的Session信息
浏览器点击提交,再次查看
[root@web1 php-memcached-demo]# ls /var/lib/php/session/
sess_neh9aoh5tnudbf6k70krvmca45             //注意这里的ID是随机的
[root@web1 php-memcached-demo]# cat /var/lib/php/session/sess_neh9aoh5tnudbf6k70krvmca45         //记录登录的状态
cookie是服务器自动发给浏览器的,一般是看不到的

web2:
root@web2 ~]# yum -y install gcc pcre-devel openssl-devel        #安装依赖包
[root@web2 ~]# rm -rf /usr/local/nginx/
[root@web2 ~]# cd lnmp_soft/nginx-1.16.1/
[root@web2 nginx-1.16.1]# ./configure --with-http_ssl_module
[root@web2 nginx-1.16.1]# make && make install
[root@web2 nginx-1.16.1]#  yum -y install   mariadb   mariadb-server   mariadb-devel php   php-fpm php-mysql

[root@web2 nginx-1.16.1]# vim /usr/local/nginx/conf/nginx.conf
……
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
启动Nginx服务, MySQL服务, PHP-FPM服务
[root@web2 nginx-1.16.1]# /usr/local/nginx/sbin/nginx
[root@web2 nginx-1.16.1]# systemctl restart mariadb
[root@web2 nginx-1.16.1]# systemctl restart php-fpm
[root@web2 nginx-1.16.1]# iptables -F 
[root@web2 nginx-1.16.1]# setenforce 0
部署测试页面
[root@web2 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@web2 ~]# cd 桌面
[root@web2 桌面]# tar -xf lnmp_soft.tar.gz   
[root@web2 桌面]# cd lnmp_soft/php_scripts/
[root@web2 php_scripts]# tar -xf php-memcached-demo.tar.gz 
[root@web2 php_scripts]# cd php-memcached-demo/
[root@web2 php-memcached-demo]# cp -r * /usr/local/nginx/html/
修改网页代码
修改index.php和home.php两个文件的内容,添加页面颜色属性,以区别后端两台不同服务器:<body bgcolor=blue>
[root@web2 php-memcached-demo]# cd /usr/local/nginx/html/
[root@web2 html]# vim index.php 
[root@web2 html]# vim home.php 

测试访问192.168.2.131/index.php 和 192.168.2.133/index.php背景颜色会不同

部署代理服务器(七层)


案例9:nginx地址重写:


实验一:修改配置文件(访问a.html重定向到b.html)
修改Nginx服务配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  www.jd.com;
rewrite /a.html$  /b.html redirect;     // redirect刷新地址栏的标签
location / {
            root   html;
index  index.php index.html index.htm;
}
}
[root@localhost ~]# vim /etc/hosts
192.168.2.131 www.jd.com
[root@localhost ~]# echo "BBB" > /usr/local/nginx/html/b.html
[root@localhost ~]# /usr/local/nginx/sbin/nginx 
[root@localhost ~]# systemctl restart php-fpm
浏览器访问www.jd.com/a.html ,能够访问到b.html的页面内容

实验二:修改配置文件(访问www.jd.com的请求重定向至www.bb.com)
修改Nginx服务配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  www.jd.com;;
        rewrite  ^/  http://www.bb.com;     #^/ 匹配所有
location / {
            root   html;
index  index.html index.htm;
}
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

实验三:修改配置文件(访问web1.tedu.cn/子页面的请求重定向至www.bb.cn/子页面)
修改Nginx服务配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 
. ..
server {
        listen       80;
        server_name  www.jd.com;
        rewrite  ^/(.*)$ http://www.bb.cn/$1; 
location / {
            root   html;
index  index.html index.htm;
}
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

实验4:WordPress代码数据迁移

P1 部署NFS

proxy:
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mkdir /tdr
[root@localhost ~]# vim /etc/exports
/tdr 192.168.2.*(rw)
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# /usr/local/nginx/sbin/nginx 
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
web1:
web1搭建wordpres拷贝到NFS共享
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# tar -czpf html.tar.gz html/                     #-p,保留整个目录的权限
[root@localhost nginx]#  scp html.tar.gz  192.168.2.132:/tdr         //拷贝到nfs共享
回到proxy主机解压:
[root@localhost ~]# cd /tdr
[root@localhost tdr]# tar -xf html.tar.gz

P2 挂载共享存储

Web服务器访问共享存储:
web2:
[root@web2 ~]# yum -y install nfs-utils
[root@web2 ~]# vim /etc/fstab
192.168.2.132:/tdr/html  /usr/local/nginx/html/ nfs defaults,_netdev 0 0 #_netdev:声明网络设备,系统在网络服务配置完成后,再挂载本设备
[root@web2 ~]# mount -a
浏览器测试


 ceph:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值