2019-05-08day48

1、将web服务用户上传的目录挂载到NFS服务器

实践:统一NFS和web用户,关键是UID

[root@web02 ~]# id nginx
uid=1111(nginx) gid=1111(nginx) 组=1111(nginx)

[root@nfs01 ~]# grep 1111 /etc/passwd
[root@nfs01 ~]# useradd nginx -u 1111 -s /sbin/nologin -M
[root@nfs01 ~]# grep 1111 /etc/passwd
nginx:x:1111:1111::/home/nginx:/sbin/nologin

强调:所有的WEB和NFS都要统一一个UID的用户

[root@nfs01 ~]# cat /etc/exports
#oldboy shared dir at time
#/data 172.16.1.0/24(rw,sync) 10.0.0.0/24(ro)
/data 172.16.1.0/24(rw,sync)

[root@nfs01 ~]# cat /etc/exports
#oldboy shared dir at time
#/data 172.16.1.0/24(rw,sync) 10.0.0.0/24(ro)
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=1111,anongid=1111)

[root@nfs01 ~]# cd /data
[root@nfs01 /data]# mkdir blog_nfs

[root@nfs01 ~]# systemctl reload nfs

测试NFS挂载OK

[root@web02 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

[root@web02 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web02 ~]# ls /mnt
ddddf  dddfff  oldboy.txt  oldgirl.txt

推送及挂载:

scp -rp 2019 172.16.1.31:/data/blog_nfs/
mount -t nfs 172.16.1.31:/data/blog_nfs /application/nginx/html/blog/wp-content/uploads

给/data目录权限,让其保持一致:

[root@nfs01 /]# chown -R nginx.nginx /data

location复习讲解:

作用:

如果匹配uri,然后执行{
执行里面的内容
}

语法:

location [ = | ~ | ~* | ^~ ] uri {
...
}

~* 不区分大小写
~ 区分大小写
^~ 匹配常规字符串,不做正则匹配检查。
= 精确匹配。

企业场景:

动态分离
#1)路径动态分离
location  ^~ /dynamic/ {
root   html/blog;
proxy_pass  172.16.1.8:8000; #<===动态服务器
}

    location ~ \.php$ {
        root   html/blog;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include fastcgi.conf;
    }
静态分离:
#1)扩展名静态分离
location  ~* \.(gif|jpg|jpeg)$ {
root   html/blog;
proxy_pass   172.16.1.8:8000; #<===静态服务器
}
#2)路径静态分离。
location  ^~ /images/ {
root   html/blog;
proxy_pass  172.16.1.8:8000; #<===静态服务器
}

匹配顺序:

不用URI及特殊字符组合匹配顺序 匹配说明
第一名:“location = / {” 精确匹配/
第二名:“location ^~ /images/ {” 匹配常规字符串,不做正则匹配检查
第三名:“location ~* .(gif|jpg|jpeg)$ {” 正则匹配
第四名:“location /documents/ {” 匹配常规字符串,如果有正则则优先匹配正则
第五名:“location / {” 所有location都不能匹配后的默认匹配

yum方式部署LNMP:

1)yum安装NGINX

[root@web02 ~]# cat /etc/yum.repos.d/nginx.repo #<==配置Nginx官方yum源。
[nginx]
name=Nginx repo by oldboy
baseurl=http://nginx.org/packages/centos/7/$basearch/ 
gpgcheck=0
enabled=1
#说明:如果是用epel源安装需要提前配置epel源,本书安装系统时已经配置过了epel源了。
[root@web02 ~]# rpm -qa nginx              #<==提前检查,发现没有。
[root@web02 ~]# yum install nginx -y       #<==开始yum安装nginx。
[root@web02 ~]# rpm -qa nginx
nginx-1.16.0-1.el7.ngx.x86_64              #<==安装完毕后检查,发现有了。
[root@web02 ~]# systemctl start nginx      #<==启动Nginx服务。。
[root@web02 ~]# systemctl enable nginx     #<==设置开机自启动。
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web02 ~]# systemctl status nginx     #<==查看状态。
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 六 2019-05-04 11:04:21 CST; 9s ago 
[root@web02 ~]# netstat -lntup|grep nginx  #<==检查端口。
tcp        0      0 0.0.0.0:80     0.0.0.0:*       LISTEN      7854/nginx: master
[root@web02 ~]# wget 127.0.0.1             #<==测试访问。
--2019-05-04 11:05:37--  http://127.0.0.1/
正在连接 127.0.0.1:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:612 [text/html]
正在保存至: “index.html”               #<==出现这个提示表示,Nginx成功安装了。
100%[========================================>] 612         --.-K/s 用时 0s      
2019-05-04 11:05:37 (39.5 MB/s) - 已保存 “index.html” [612/612])

2) yum安装PHP

#第一个:解决PHP软件冲突
yum remove php-mysql php php-fpm php-common
#第二个:更新yum源信息,用户安装php程序

# 准备yum安装软件扩展源信息
wget -q https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget -q https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

rpm -Uvh epel-release-latest-7.noarch.rpm
rpm -Uvh webtatic-release.rpm
    
#第三个:直接安装php服务相关软件
yum install php71w php71w-cli php71w-common php71w-devel php71w-embedded   -y
yum install php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml  -y
yum install php71w-fpm php71w-mysqlnd php71w-opcache  php71w-pecl-memcached  -y
yum install php71w-pecl-redis php71w-pecl-mongodb -y
###############检查是否安装成功###############
rpm -qa mod_php71w php71w-cli php71w-common php71w-devel php71w-embedded
rpm -qa php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml
rpm -qa php71w-fpm php71w-mysqlnd php71w-opcache 
rpm -qa php71w-pecl-redis php71w-pecl-mongodb php71w-pecl-memcached

#启动
php-fpm -t
systemctl start php-fpm
systemctl enable php-fpm

============================================
#php71w         --- 主程序软件
#php71w-gd      --- 和显示图形相关的软件
#php71w-mcrypt  --- 和数据传输加密相关
#php71w-pdo     --- 让php和数据库建立联系
#php71w-fpm     --- 
==========================================
[root@web01 /]# rpm -ql mod_php71w
/etc/httpd/conf.d/php.conf            --- 
/etc/httpd/conf.modules.d/10-php.conf
/usr/lib64/httpd/modules/libphp7-zts.so
/usr/lib64/httpd/modules/libphp7.so
/usr/share/httpd/icons/php.gif
/var/lib/php/session
/var/lib/php/wsdlcache
===========================
[root@web01 /]# rpm -ql php71w-fpm
/etc/logrotate.d/php-fpm
/etc/php-fpm.conf         --- PHP FPM配置
/etc/php-fpm.d
/etc/php-fpm.d/www.conf   --- PHP FPM子目录配置
/etc/sysconfig/php-fpm
/usr/lib/systemd/system/php-fpm.service
/usr/lib/tmpfiles.d/php-fpm.conf
/usr/sbin/php-fpm         --- PHP FPM启动命令


#重要文件和目录信息
#/etc/php-fpm.conf    ---php-fpm进程的配置文件
#/etc/php-fpm.d       ---php-fpm进程加载配置文件的目录
#/etc/php-fpm.d/www.conf
#user = nginx        --- 利用指定用户管理php工作进程    建议配置和nginx服务相同的用户
#group = nginx       --- 利用指定用户组管理php工作进程
#listen = 127.0.0.1:9000   --- 指定php服务运行后, 监听的地址和端口信息
#listen.allowed_clients = 127.0.0.1   --- 只允许本地访问php 9000端口服务

3) yum安装mariadb数据库(mysql同源)

yum install mariadb mariadb-server -y
systemctl start mariadb.service 
systemctl enable mariadb.service 

4)验证MariaDB安装

[root@web01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-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)]>  #<==成功标识。

5)配置测试PHP

#a.调整转发PHP请求
[root@web01 /etc/nginx/conf.d]# cat blog.conf
server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
        root   /usr/share/nginx/html/blog;
            index  index.php;

            location ~ .*\.(php|php5)?$ {
            root   /usr/share/nginx/html/blog;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }
    }
    
nginx -t
systemctl reload nginx
netstat -lntup|grep nginx

测试:

echo "" > /usr/share/nginx/html/test_info.php
php /usr/share/nginx/html/test_info.php 

浏览器打开。
10.0.0.7/test_info.php 

6)配置测试mysql

[root@web01 /usr/share/nginx/html]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-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)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified by 'oldboy123';
Query OK, 0 rows affected (0.05 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye
[root@web01 /usr/share/nginx/html]# cat test_mysql.php

[root@web01 /usr/share/nginx/html]# php test_mysql.php
mysql successful by oldboy.

集群调整:

1)web02 blog数据迁移至web01:
scp -rp 172.16.1.8:/application/nginx/html/blog /usr/share/nginx/html

2)配置调整,伪静态,路径加blog:

[root@web01 /etc/nginx/conf.d]# cat blog.conf
server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
        root   /usr/share/nginx/html/blog; ###################
            index  index.php;
        }
    if (-f $request_filename/index.html){
        rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
    }
    if (!-f $request_filename){
        rewrite (.*) /index.php;
    }
        location ~ .*\.(php|php5)?$ {
            root   /usr/share/nginx/html/blog;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }
    }

3)共享存储调整:

yum install rpcbind nfs-utils -y
systemctl start rpcbind
systemctl enable rpcbind

mount -t nfs 172.16.1.31:/data/blog_nfs  /usr/share/nginx/html/blog/wp-content/uploads
ls /usr/share/nginx/html/blog/wp-content/uploads
别忘了,开机自动挂载。

你可能感兴趣的:(2019-05-08day48)