1 什么是FastCGI

FastCGI是一个可伸缩地、高速地在HTTP服务器和动态脚本语言间通信的接口

CGI(common gateway interface)通用网关接口

FastCGI特点总结:

1、是HTTP服务器和动态脚本语言间通信的接口或者工具;

2FastCGI优点是把动态语言解析和HTTP服务器分离开来;

3nginxapachelighttpd以及多数动态语言都支持FastCGI

4FastCGI接口方式采用C/S结构,客户端(http服务器),服务端(动态语言解析服务器);

5PHP动态语言服务端可以启动多个FastCGI的守护进程(php-fpm

6http服务器通过FastCGI客户端(例:Nginx fastcgi_pass)和动态语言FastCGI服务端通信(例:php-fpm

 

以下为Nginx结合PHP FastCGI运行原理图:

图片.png

 

2 PHP安装前的准备

要安装PHP,最好先安装nginxmysql

检查nginx是否安装
[root@lnmp02 data]# /application/nginx/sbin/nginx
[root@lnmp02 data]# ps -ef|grep nginx           
root       1900      1  0 18:12 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx      1901   1900  0 18:12 ?        00:00:00 nginx: worker process       
root       1903   1035  0 18:12 pts/0    00:00:00 grep nginx
查看Nginx版本信息,编译环境等
[root@lnmp02 data]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module
检查数据库有没有安装
[root@lnmp02 data]# ls /application/mysql/ -al
[root@lnmp02 data]# lsof -i :3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  1633 mysql   10u  IPv4  18284      0t0  TCP *:mysql (LISTEN)

3 安装PHP(编译安装)

拷贝PHP文件到服务器
[root@lnmp02 data]# cd /home/oldboy/tools/
[root@lnmp02 tools]# rz -y
[root@lnmp02 tools]# ls
mysql-5.5.32-linux2.6-x86_64.tar.gz  nginx-1.6.3  nginx-1.6.3.tar.gz  php-5.3.27.tar.gz
查看php运行时调用的一些函数库是否已经安装
[root@lnmp02 tools]# rpm -qa freetype-devel libpng-devel gd-devel curl-devel
[root@lnmp02 tools]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
安装相关的函数库包


yum install zlib-devel libxml2-devel libjpeg-devel libiconv-devel -y
yum install freetype-devel libpng-devel gd-devel curl-devel libxslt-devel -y
echo $?
vi /etc/yum.conf  修改keepcache=1 可以让安装了的包保留下来


 

检查包是否顺利安装


[root@lnmp02 tools]# rpm -qa freetype-devel libpng-devel gd-devel curl-devel
freetype-devel-2.3.11-17.el6.x86_64
libpng-devel-1.2.49-2.el6_7.x86_64
gd-devel-2.0.35-11.el6.x86_64
[root@lnmp02 tools]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
libxml2-devel-2.7.6-21.el6_8.1.x86_64
 
libjpeg-devel  curl-devel要变名字
[root@lnmp02 tools]# rpm -qa|grep libjpeg
libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64
 
[root@lnmp02 tools]# rpm -qa|grep curl
libcurl-devel-7.19.7-53.el6_9.x86_64
yum源里没有libiconv-devel,手动安装后再次检查
[root@lnmp02 tools]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
[root@lnmp02 tools]#tar zxf libiconv-1.14.tar.gz
[root@lnmp02 tools]#cd libiconv-1.14
[root@lnmp02 libiconv-1.14]#./configure --prefix=/usr/local/libiconv
[root@lnmp02 libiconv-1.14]#make
[root@lnmp02 libiconv-1.14]#make install

 

rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel zlib-devel libxml2-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
libjpeg-turbo-devel-1.2.1-3.el6_5.x86_64
freetype-devel-2.3.11-17.el6.x86_64
libpng-devel-1.2.49-2.el6_7.x86_64
libcurl-devel-7.19.7-53.el6_9.x86_64
libxml2-devel-2.7.6-21.el6_8.1.x86_64
gd-devel-2.0.35-11.el6.x86_64
安装libmcrypt
[root@lnmp02 tools]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@lnmp02 tools]# yum install libmcrypt-devel -y
[root@lnmp02 tools]# rpm -qa libmcrypt-devel
libmcrypt-devel-2.5.8-9.el6.x86_64
安装mhash加密扩展库,已经装了epel源,可以快速安装
yum install mhash mhash-devel -y
[root@lnmp02 tools]# rpm -qa  mhash mhash-devel          
mhash-devel-0.9.9.9-3.el6.x86_64
mhash-0.9.9.9-3.el6.x86_64
安装mcrypt加密扩展库
[root@lnmp02 tools]# yum install mcrypt -y
[root@lnmp02 tools]# rpm -qa mcrypt
mcrypt-2.6.8-10.el6.x86_64
开始安装PHP服务
[root@lnmp02 tools]# ls
libiconv-1.14         mysql-5.5.32-linux2.6-x86_64.tar.gz  nginx-1.6.3.tar.gz
libiconv-1.14.tar.gz  nginx-1.6.3                          php-5.3.27.tar.gz
[root@lnmp02 tools]# tar xf php-5.3.27.tar.gz
[root@lnmp02 tools]# ls
libiconv-1.14                        nginx-1.6.3         php-5.3.27.tar.gz
libiconv-1.14.tar.gz                 nginx-1.6.3.tar.gz
mysql-5.5.32-linux2.6-x86_64.tar.gz  php-5.3.27
[root@lnmp02 tools]# cd php-5.3.27
[root@lnmp02 php-5.3.27]#
编译安装PHP
./configure \
--prefix=/application/php-5.3.27 \
--with-mysql=/application/mysql \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curlwrappers \
--enable-mbregex \
--enable-mbstring \
--enable-fpm \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
检查错误
[root@lnmp02 php-5.3.27]# echo $?
0
make: *** [ext/phar/phar.php] 错误 127
安装报错的处理
将libmysqlclient.so.18库做个软链接,创建没有的文件,重新make
[root@lnmp02 php-5.3.27]# ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
 [root@lnmp02 php-5.3.27]# touch ext/phar/phar.phar
[root@lnmp02 php-5.3.27]# make
 
Build complete.
Don't forget to run 'make test'.
 
[root@lnmp02 php-5.3.27]# make install
[root@lnmp02 php-5.3.27]# echo $?
0
做个软链接
[root@lnmp02 php-5.3.27]# ln -s /application/php-5.3.27/ /application/php
[root@lnmp02 php-5.3.27]# ls -l /application/php
lrwxrwxrwx 1 root root 24 6月  25 22:19 /application/php -> /application/php-5.3.27/
查看php配置默认模板文件
[root@lnmp02 php-5.3.27]# ls php.ini*
php.ini-development  php.ini-production
拷贝配置文件到PHP默认目录并改名
[root@lnmp02 php-5.3.27]# cp php.ini-production /application/php/lib/php.ini
php.ini比较重要的参数优化

1)比较重要的安全参数优化:


338  safe_mode = On       338行 开启安全模式
435  expose_php = Off      关闭版本信息
521  error_reporting = E_ALL & ~E_DEPRECATED    报错的级别
538  display_errors = Off         错误信息控制,测试的时候开启
559  log_errors = On             打开log日志
643  ;error_log = php_errors.log     log日志的路径
703  register_globals = Off         关闭全局变量(默认关闭,不能开启)
756  magic_quotes_gpc = Off       防止SQL注入
854  ;cgi.fix_pathinfo=0           防止Nginx文件类型错误解析漏洞
902  allow_url_fopen = Off         打开远程打开(禁止)
2)根据具体业务可修改的参数:
444  max_execution_time = 30      单个脚本最大运行时间,单位秒*****
454  max_input_time = 60          单个脚本等待输入的最长时间
465  memory_limit = 128M        单个脚本最大使用内存,单位K或M
                               (128M稍大,可以适当调小)
891  upload_max_filesize = 2M      上传文件最大许可
894  max_file_uploads = 20         可以通过单个请求上载的最大文件数


配置PHP服务的配置文件php-fpm.conf

[

root@lnmp02 php-5.3.27]# cd /application/php/etc/
[root@lnmp02 etc]# ls
pear.conf  php-fpm.conf.default
[root@lnmp02 etc]# cp php-fpm.conf.default php-fpm.conf

 

php-fpm.conf配置文件详解

pid = /usr/local/var/run/php-fpm.pid

#pid设置,一定要开启,上面是Mac平台的。默认在php安装目录中的var/run/php-fpm.pid。比如centos的在: /usr/local/php/var/run/php-fpm.pid

 

error_log = /usr/local/var/log/php-fpm.log

#错误日志,上面是Mac平台的,默认在php安装目录中的var/log/php-fpm.log,比如centos的在: /usr/local/php/var/log/php-fpm.log

 

log_level = notice

#错误级别. 上面的php-fpm.log纪录的登记。可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.

 

emergency_restart_threshold = 60

emergency_restart_interval = 60s

#表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。0 表示 '关闭该功能'. 默认值: 0 (关闭).

 

process_control_timeout = 0

#设置子进程接受主进程复用信号的超时时间. 可用单位: s(), m(), h(小时), 或者 d() 默认单位: s(). 默认值: 0.

 

daemonize = yes

#后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。

 

listen = 127.0.0.1:9000

#fpm监听端口,即nginxphp处理的地址,一般默认值即可。可用格式为: 'ip:port', 'port', '/path/to/unix/socket'. 每个进程池都需要设置。如果nginxphp在不同的机器上,分布式处理,就设置ip这里就可以了。

 

listen.backlog = -1

#backlog数,设置 listen 的半连接队列长度,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:http://www.3gyou.cc/?p=41

 

listen.allowed_clients = 127.0.0.1

#允许访问FastCGI进程的IP白名单,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接。

 

listen.owner = www

listen.group = www

listen.mode = 0666

#unix socket设置选项,如果使用tcp方式访问,这里注释即可。

 

user = www

group = www

#启动进程的用户和用户组,FPM 进程运行的Unix用户, 必须要设置。用户组,如果没有设置,则默认用户的组被使用。

 

pm = dynamic

#php-fpm进程启动模式,pm可以设置为staticdynamicondemand

#如果选择static,则进程数就数固定的,由pm.max_children指定固定的子进程数。

#如果选择dynamic,则进程数是动态变化的,由以下参数决定:

 

pm.max_children = 50 #子进程最大数

pm.start_servers = 2 #启动时的进程数,默认值为: min_spare_servers + (max_spare_servers - min_spare_servers) / 2

pm.min_spare_servers = 1 #保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程

pm.max_spare_servers = 3 #,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理

 

pm.max_requests = 500

#设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.

 

pm.status_path = /status

#FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到

 

ping.path = /ping

#FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)

 

ping.response = pong

#用于定义ping请求的返回相应. 返回为 HTTP 200 text/plain 格式文本. 默认值: pong.

 

access.log = log/$pool.access.log

#每一个请求的访问日志,默认是关闭的。

 

access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"

#设定访问日志的格式。

 

slowlog = log/$pool.log.slow

#慢请求的记录日志,配合request_slowlog_timeout使用,默认关闭

 

request_slowlog_timeout = 10s

#当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 '0' 表示 'Off'

 

request_terminate_timeout = 0

#设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用. 设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项。

 

rlimit_files = 1024

#设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。

 

rlimit_core = 0

#设置核心rlimit最大限制值. 可用值: 'unlimited' 0或者正整数. 默认值: 系统定义值.

 

chroot =

#启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, chroot不被使用.

 

 

chdir =

#设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)

 

catch_workers_output = yes

#重定向运行过程中的stdoutstderr到主要的错误日志文件中. 如果没有设置, stdout stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: .

php-fpm.conf一些重要的设置

php-fpm进程分配

在之前的文章中就说过了。在fasgcgi模式下,php会启动多个php-fpm进程,来接收nginx发来的请求,那是不是进程越多,速度就越快呢?这可不一定!得根据我们的机器配置和业务量来决定。

我们先来看下,设定进程的配置在哪里?

 

pm = static | dynamic | ondemand

pm可以设置成这样3种,我们用的最多的就上前面2种。

pm = static 模式

pm = static 表示我们创建的php-fpm子进程数量是固定的,那么就只有pm.max_children = 50这个参数生效。你启动php-fpm的时候就会一起全部启动51(1个主+50个子)个进程,颇为壮观。

pm = dynamic 模式

pm = dynamic模式,表示启动进程是动态分配的,随着请求量动态变化的。他由 pm.max_childrenpm.start_serverspm.min_spare_serverspm.max_spare_servers 这几个参数共同决定。

上面已经讲过,这里再重申一下吧:

pm.max_children 50 是最大可创建的子进程的数量。必须设置。这里表示最多只能50个子进程。

pm.start_servers = 20 随着php-fpm一起启动时创建的子进程数目。默认值:min_spare_servers + (max_spare_servers - min_spare_servers) / 2。这里表示,一起启动会有20个子进程。

pm.min_spare_servers = 10 
设置服务器空闲时最小php-fpm进程数量。必须设置。如果空闲的时候,会检查如果少于10个,就会启动几个来补上。

pm.max_spare_servers = 30 
设置服务器空闲时最大php-fpm进程数量。必须设置。如果空闲时,会检查进程数,多于30个了,就会关闭几个,达到30个的状态。

到底选择static还数dynamic?

很多人恐惧症来袭,不知道选什么好?

一般原则是:动态适合小内存机器,灵活分配进程,省内存。静态适用于大内存机器,动态创建回收进程对服务器资源也是一种消耗。

如果你的内存很大,有8-20G,按照一个php-fpm进程20M算,100个就2G内存了,那就可以开启static模式。如果你的内存很小,比如才256M,那就要小心设置了,因为你的机器里面的其他的进程也算需要占用内存的,所以设置成dynamic是最好的,比如:pm.max_chindren = 8, 占用内存160M左右,而且可以随时变化,对于一半访问量的网站足够了。

 

慢日志查询

我们有时候会经常饱受500,502问题困扰。当nginx收到如上错误码时,可以确定后端php-fpm解析php出了某种问题,比如,执行错误,执行超时。

这个时候,我们是可以开启慢日志功能的。

slowlog = /usr/local/var/log/php-fpm.log.slow

request_slowlog_timeout = 15s

当一个请求该设置的超时时间15秒后,就会将对应的PHP调用堆栈信息完整写入到慢日志中。

php-fpm慢日志会记录下进程号,脚本名称,具体哪个文件哪行代码的哪个函数执行时间过长:

[21-Nov-2013 14:30:38] [pool www] pid 11877

script_filename = /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php

[0xb70fb88c] file_get_contents() /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php:2

通过日志,我们就可以知道第2行的file_get_contents 函数有点问题,这样我们就能追踪问题了

 

启动PHP服务
[root@lnmp02 ~]# /application/php/sbin/php-fpm
[root@lnmp02 etc]# ps -ef|grep php-fpm
root     126238      1  0 23:34 ?        00:00:00 php-fpm: master process (/application/php-5.3.27/etc/php-fpm.conf)
nginx    126239 126238  0 23:34 ?        00:00:00 php-fpm: pool www           
nginx    126240 126238  0 23:34 ?        00:00:00 php-fpm: pool www           
root     126242 126214  0 23:34 pts/0    00:00:00 grep php-fpm
[root@lnmp02 etc]# lsof -i :9000    <============默认9000端口提供服务
COMMAND    PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 126238  root    7u  IPv4 121633      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 126239 nginx    0u  IPv4 121633      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 126240 nginx    0u  IPv4 121633      0t0  TCP localhost:cslistener (LISTEN)
配置Nginx支持PHP程序请求访问
[root@lnmp02 etc]# cd /application/nginx/conf/
[root@lnmp02 conf]# vi extra/blog.conf
     server {
        listen       80;
        server_name  blog.etiantian123.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
        location ~ .*\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    }
检查语法并重启服务
[root@lnmp02 conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@lnmp02 conf]# ../sbin/nginx -s reload
测试LNMP环境生效的情况

1)测试PHP解析请求是否OK

[root@lnmp02 conf]# cd ../html/blog/
[root@lnmp02 blog]# echo "<?php phpinfo(); ?>" >test_info.php
[root@lnmp02 blog]# cat test_info.php
<?php phpinfo(); ?>

2)调整windows下的host解析

3)打开浏览器输入http://blog.etiantian123.org/test_info.php 若能显示PHP的信息 PHP解析已经正常

 

PHP连接mysql的情况进行测试
[root@lnmp02 blog]# vi test_mysql.php
<?php
       $link_id=mysql_connect("localhost","root","wangxin") or mysql_error();
       if ($link_id) {
                echo "mysql successful by wangxin !";
       }else{      
                echo mysql_error();
       }
?>

浏览器测试:

http://blog.etiantian123.org/test_mysql.php

出现mysql successful by wangxin ! OK

4 部署一个开源博客wordpress

4.1 mysql数据库配置准备

登录数据库
[root@lnmp02 ~]# mysql -uroot -pwangxian
mysql> create database wordpress;   <========创建一个专用的数据库wordpress用于存放blog数据
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
4 rows in set (0.07 sec)
创建一个专用的wordpress管理用户

命令如下:

mysql> grant all on wordpress .* to wordpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

当数据库和PHP服务不在一台机器上,修改地址如下:   10.0.0.%为客户端地址段

mysql> grant all on wordpress .* to wordpress@'10.0.0.%' identified by '123456';

刷新权限,使得创建用户生效

mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)

 

查看用户对应的权限
show grants for wordpress@'localhost';
查看数据库里创建的wordpress用户
mysql> select user,host from mysql.user;
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| root      | 127.0.0.1 |
| root      | localhost |
| wordpress | localhost |      <==============只允许本机通过wordpress用户访问数据库
+-----------+-----------+
3 rows in set (0.00 sec)

4.2 NginxPHP环境配置准备

1、选择配置好的支持LNMPblog域名对应的虚拟主机
[root@lnmp02 ~]# cd /application/nginx/conf/extra/
[root@lnmp02 extra]# vi blog.conf
     server {
        listen       80;
        server_name  blog.etiantian123.org;
        location / {
            root   html/blog;
            index  index.php index.html index.htm;   《=========补充一个首页文件index.php
        }
        location ~ .*\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    }

检查语法以及重新加载服务

[root@lnmp02 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@lnmp02 extra]# ../../sbin/nginx -s reload

 

2、获取wordpress程序,放置到blog域名对应虚拟主机的目录下


[root@lnmp02 blog]# ls
index.html  test_info.php  test_mysql.php  wordpress-4.7.4-zh_CN.tar.gz
[root@lnmp02 blog]# tar xf wordpress-4.7.4-zh_CN.tar.gz
[root@lnmp02 blog]# ls
index.html  test_info.php  test_mysql.php  wordpress  wordpress-4.7.4-zh_CN.tar.gz
[root@lnmp02 blog]# mv wordpress/*  .  《=======把wordpress目录的内容移动到blog根目录
[root@lnmp02 blog]# /bin/mv wordpress-4.7.4-zh_CN.tar.gz /home/oldboy/tools/    将源文件移走备份


授权nginxphp服务访问blog站点目录,此授权方法不安全,临时办法

[root@lnmp02 blog]# chown -R nginx.nginx ../blog/


5 wordpress数据库的备份和恢复

mysqldump 最常用的备份工具

5.1数据备份分类

逻辑备份(mysqldump

小于50G的数据量

原理:将数据库的数据以逻辑的SQL语句的方式到处

 

物理备份

1 scp /application/mysql/data 拷贝到独立数据库上就可以

2 xtrabackup开源的物理备份工具

5.2 mysqldump备份

[root@lnmp02 ~]# mysqldump -uroot -pwangxian -B -A -x|gzip >/opt/backup_$(date +%F).sql.gz
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@lnmp02 ~]# ll /opt/
总用量 296
-rw-r--r--  1 root root 298664 6月  27 21:41 backup_2017-06-27.sql.gz
drwxr-xr-x. 2 root root   4096 11月 22 2013 rh

-A  备份所有库

-B  备份多个库,并添加use 库名; create database库等的功能

-x  备份时锁表,会影响读写,尽量晚上执行。

gzip  压缩效率高

只备份wordpress库里的数据
[root@lnmp02 ~]# mysqldump -uroot -pwangxian -B  -x wordpress|gzip >/opt/backup_wordpress_$(date +%F).sql.gz
数据库备份的迁移到mysql-server
[root@lnmp02 opt]# scp -rp -P52113 backup_wordpress_2017-06-27.sql.gz root@192.168.4.124:/opt/

 

数据库恢复
先解压
[root@mysql-server tools]# cd /opt/
[root@mysql-server opt]# gzip -d backup_wordpress_2017-06-27.sql.gz
[root@mysql-server opt]# ll
总用量 616
-rw-r--r--  1 root root 625542 6月  27 21:47 backup_wordpress_2017-06-27.sql
[root@mysql-server opt]# mysql -uroot -pwangxian <backup_wordpress_2017-06-27.sql
[root@mysql-server opt]# mysql -uroot -pwangxian
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

 

给客户端主机授权访问数据库
mysql> grant select,insert,update,delete on wordpress.* to wordpress@'192.168.4.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
 
mysql> select user,host from mysql.user;                                                 
+-----------+--------------+
| user      | host         |
+-----------+--------------+
| root      | 127.0.0.1    |
| wordpress | 192.168.4.%  |
| root      | ::1          |
|           | localhost    |
| root      | localhost    |
|           | mysql-server |
| root      | mysql-server |
+-----------+--------------+
7 rows in set (0.00 sec)

Nginx服务器上修改wp-config.php参数,并且修改hosts文件

[root@lnmp02 blog]# pwd
/application/nginx/html/blog
 
[root@lnmp02 blog]# vi wp-config.php
/** MySQL主机 */
define('DB_HOST', 'db.etiantian123.org');
 
[root@lnmp02 blog]# vi /etc/hosts
192.168.4.124 db.etiantian123.org

6 搭建LB负载均衡环境

添加虚拟IP(VIP)

[root@lb01 ~]# ip addr add 192.168.4.126/24 dev eth0
[root@lb01 ~]# ip add|grep 4.126
    inet 192.168.4.126/24 scope global secondary eth0
负载均衡常用软件:

L4tcp负载均衡  lvs

L4-L7 Haproxy

L7    Nginx  国内中小企业集群比较流行的负载均衡软件,严格来说叫反向代理。Nginx已经支持L4-L7

下载安装nginx
[root@lb01 tools]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
[root@lb01 tools]# ls -l
总用量 788
-rw-r--r-- 1 root root 805253 4月   8 2015 nginx-1.6.3.tar.gz
[root@lb01 tools]# useradd nginx -s /sbin/nologin -M
[root@lb01 tools]# tar xf nginx-1.6.3.tar.gz
[root@lb01 tools]# cd nginx-1.6.3
[root@lb01 nginx-1.6.3]# yum install pcre-devel -y
[root@lb01 nginx-1.6.3]# yum install pcre-devel openssl-devel -y
[root@lb01 nginx-1.6.3]# mkdir /application
[root@lb01 nginx-1.6.3]#./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@lb01 nginx-1.6.3]#make
[root@lb01 nginx-1.6.3]#make install
做个软链接
[root@lb01 nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx
[root@lb01 nginx-1.6.3]# ls -l /application/
总用量 4
lrwxrwxrwx 1 root root   25 6月  28 14:25 nginx -> /application/nginx-1.6.3/
drwxr-xr-x 6 root root 4096 6月  28 14:24 nginx-1.6.3
[root@lb01 nginx-1.6.3]# ll /application/nginx/
总用量 16
drwxr-xr-x 2 root root 4096 6月  28 14:24 conf
drwxr-xr-x 2 root root 4096 6月  28 14:24 html
drwxr-xr-x 2 root root 4096 6月  28 14:24 logs
drwxr-xr-x 2 root root 4096 6月  28 14:24 sbin

 

Nginx负载均衡需要哪些模块:

1upstream模块      ngx_http_upstream_module

定义有哪些节点,调度算法是什么?

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;
 
    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}
 
server {
    location / {
        proxy_pass http://backend;
    }
}

2http_proxy模块    ngx_http_proxy_module

此模块负责请求的转发,例如:proxy_pass发给定义好的指定upstream名字

location / {
    proxy_pass       http://localhost:8000;
    proxy_set_header Host      $host;
    proxy_set_header X-Forwarded-For  $remote_addr; 《===获取用户真实IP
}