LNMP一体化部署

LNMP组合工作流程

在LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务,如果请求是静态资源,则由Nginx解析返回给用户;如果是动态请求(.php结尾),那么Nginx就会把它通过FastCGI接口(快速接口规则,生产常用方法)发送FastCGI数据包给PHP引擎服务(FastCGI进程php-fpm)进行解析,如果这个动态请求要读取数据库数据,那么PHP就会继续向后请求MySQL数据库,以读取需要的数据,并最终通过Nginx服务把获取的数据返回给用户,这就是LNMP环境的基本请求顺序流程。这个请求流程是企业使用LNMP环境的常用流程。
在这里插入图片描述
用户通过浏览器将请求发到nginx
(1)若请求为静态请求,location/{
(静态)
root html;
index index.html index.htm;

}
nginx就会去网页目录根据URL的URI部分取得网页,并将网页返回给用户。(磁盘IO读写),图片、视频在存储(NFS)中(代码不放在存储中),此时NFS需要挂载Nginx
(2) 若请求为动态请求,location ~* .(php|php5)${
(动态)
root html;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000
}
nginx就会将用户的请求转换为fastcgi格式的数据包推给fastcgi的服务端php-fpm(一个socket进程,端口为9000,加快了PHP解析速度),PHP激活JS代码
动态网页由实体文件index.php(放在内存中),通过php语言到数据库取得数据,并写回index.php文件,再打包给用户(代码不放在存储中)

  1. 分离式部署中,NGINX服务器和PHP服务器上网页、代码必须两者都有,否则会触发404

  2. nginx和Apache一样,若没有PHP支持,都处理不了所谓的动态请求,他们自身其实都只能出路静态,只是Apache转发动态数据包的速度快,但是只是单个包速度,Apache并发低。

  3. 读取网页时无需挂载存储NFS,但是动态(如上传图片)需要挂载存储NFS

  4. fastcgi快速接口有两端:
    (1)作为客户端的fastcgi_pass(Nginx安装包包含)
    (2)作为服务端的php-fpm(PHP安装包包含),一个socket进程,监听9000端口
    http数据的结构很松散,格式要求不够严谨,所以http协议的数据包特点:比较小,解析速度快,但是网络传输快
    fastcgi数据包格式非常严谨,PHP解析速度非常快,但是越严谨的数据包越大,所以fsatcgi数据包大小 要绝对大于http数据包

  5. 优化:往往与nginx的内存和php的内存性能有关

  6. PHP解析器:php.ini
    php.ini解析php语言用于向MySQL发送SQL语句,MySQL返回数据后转换成静态页面,写回到磁盘,形成伪静态文件(需要设定有效期)

LNMP的部署方式:

(1)全都部署在一台服务器上
(2)全都不部署在一台服务器上(N+P+M)
(3)只分离MySQL(NP+M)

部署LNMP

将nginx和PHP的程序用户设为同一个

  1. 安装nginx
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.10.2/
[root@localhost ~]# mount /dev/sr0 /media/cdrom
[root@localhost nginx-1.10.2]# yum -y install pcre-devel openssl-devel
[root@localhost nginx-1.10.2]# useradd -s /sbin/nologin -M www
[root@localhost nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 
[root@localhost nginx-1.10.2]# make && make install
[root@localhost nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@localhost conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / { 
            root   html;
            index  index.html index.htm;
        }
    }
}
              
# nginx编译完毕
  1. 安装MySQL(PHP需要MySQL的支持环境)
[root@localhost ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
# 创建mysql程序用户
[root@localhost ~]# useradd -s /sbin/nologin -M mysql
[root@localhost mysql-5.5.32-linux2.6-x86_64]# echo "192.168.214.163 LNMP" >> /etc/hosts
[root@localhost mysql-5.5.32-linux2.6-x86_64]# ln -s /usr/local/mysql-5.5.32-linux2.6-x86_64/ /usr/local/mysql
# 初始化MySQL配置文件my.conf
[root@localhost ~]# cd /usr/local/mysql-5.5.32-linux2.6-x86_64/
[root@localhost mysql-5.5.32-linux2.6-x86_64]# /bin/cp support-files/my-small.cnf  /etc/my.cnf 
# 初始化mysql
[root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql
[root@localhost mysql]# yum -y install libaio
[root@localhost mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!


初始化故障排错集锦

错误示例1:

usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared ob

#错误原因是没有libaio函数库的支持。需要
yum -y install libaio

错误示例2:

WARNING:The host'mysql'could not be looked up with resolveip

#需要修改主机名解析,使其和uname -n一样,修改后的结果如下:
[root@localhost ~] # grep `uname -n` /etc/hosts

错误示例3:

ERROR:1004Can't create file '/tmp/#sql300e_1_o.frm'(errno:13)
#原因是/tmp目录的权限有问题。
解决办法为处理/tmp目录,如下:
[root@localhost ~]# ls -ld /tmp
drwxrwxrwt. 3 root root 4096 Jul 14 07:56 /tmp
[root@localhost ~]# chmod -R 1777 /tmp/

此故障必须解除,否则,后面会出现登陆不了数据库等问题。
3. 配置并启动MySQL数据库

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/init.d/mysqld 
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS! 
[root@localhost mysql]# netstat -antup | grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4688/mysqld         
# 设置MySQL开机自启动
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# chkconfig --list mysqld
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@localhost mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost mysql]# which mysqladmin
/usr/local/bin/mysqladmin
[root@localhost mysql]# mysqladmin -uroot password '123456'
[root@localhost mysql]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
  1. PHP(fastcgi方式)服务的安装和准备(PHP需要MySQL的支持环境)
[root@localhost mysql]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[root@localhost mysql]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
libxml2-devel-2.7.6-14.el6.x86_64
libjpeg-turbo-devel-1.2.1-1.el6.x86_64
#这里仅缺少libiconv-devel包
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
freetype-devel-2.3.11-14.el6_3.1.x86_64
libpng-devel-1.2.49-1.el6_2.x86_64
libcurl-devel-7.19.7-37.el6_4.x86_64
libxslt-devel-1.1.26-2.el6_3.1.x86_64
gd-2.0.35-11.el6.x86_64
[root@localhost ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/libiconv-1.14/
[root@localhost libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install

 [root@localhost ~]# rpm -ivh mhash-0.9.9.9-3.el6.x86_64.rpm 
warning: mhash-0.9.9.9-3.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:mhash                  ########################################### [100%]
[root@localhost ~]# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm 
warning: libmcrypt-2.5.8-9.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:libmcrypt              ########################################### [100%]
[root@localhost ~]# rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm 
warning: libmcrypt-devel-2.5.8-9.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:libmcrypt-devel        ########################################### [100%]
[root@localhost ~]# rpm -ivh mcrypt-2.6.8-10.el6.x86_64.rpm 
warning: mcrypt-2.6.8-10.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:mcrypt                 ########################################### [100%]

  1. 开始安装PHP(fastcgi方式)服务
    (1)解压配置PHP
[root@localhost ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ./configure \
> --prefix=/usr/local/php5.3.28 \
> --with-mysql=/usr/local/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-curl \
> --with-curlwrappers \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --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=www \
> --with-fpm-group=www \
> --enable-ftp
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.


对于上面命令,部分参数说明如下:
(1)–prefix=/usr/local/php5.2.28
表示指定PHP的安装路径为/usr/local/php5.3.28
(2)–with-mysql=/usr/local/mysql
表示需要指定MySQL的安装路径,安装PHP需要的MySQL相关内容。当然,如果没有MySQL软件包,也可以不单独安装,这样的情况可使用–with-mysql=mysqlnd替代–with-mysql=/usr/local/mysql,因为PHP软件里已经自带了连接MySQL的客户端工具。
(3)–with-fpm-user=www
nginx表示指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一
(4)–with-fpm-group=www
表示指定PHP-FPM进程管理的组为www,此处最好与Nginx服务用户组统一。
(5)–enable-fpm
表示激活PHP-FPM方式服务,即以FastCGIF方式运行PHP服务。

(2)编译安装PHP

[root@localhost php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@localhost php-5.3.28]# touch ext/phar/phar.phar
[root@localhost php-5.3.28]# make
[root@localhost php-5.3.28]# make install
[root@localhost php-5.3.28]#  ln -s /usr/local/php5.3.28/ /usr/local/php
[root@localhost php-5.3.28]# cd /usr/local/php
[root@localhost php]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ls php.ini*
php.ini-development  php.ini-production
[root@localhost php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
[root@localhost php-5.3.28]# cd /usr/local/php/etc/
[root@localhost etc]# ls 
pear.conf  php-fpm.conf.default
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]#  /usr/local/php/sbin/php-fpm
[root@localhost etc]# netstat -antup | grep 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      129604/php-fpm      
[root@localhost etc]# vim php-fpm.conf
151 listen = 127.0.0.1:9000  #若设为分布式部署,需要将IP地址改为对方IP地址

  1. 配置nginx支持PHP程序请求访问
[root@localhost etc]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.bak
[root@localhost conf]# vim nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  blog.yunjisuan.com;
        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;
        }
    }
}
 # 启动nginx
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mkdir blog
[root@localhost html]# ls
50x.html  blog  index.html
[root@localhost html]# echo "`hostname -I` blog.yunjisuan.com" > blog/index.html
[root@localhost blog]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.214.163 LNMP blog.yunjisuan.com

[root@localhost html]# /usr/local/nginx/sbin/nginx 
[root@localhost html]# netstat -antup | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      129672/nginx        
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1380/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      1380/sshd           
[root@localhost blog]# curl blog.yunjisuan.com
192.168.214.163  blog.yunjisuan.com
[root@localhost blog]# vim test_info.php
<?php phpinfo(); ?>
 #本机/我的电脑/磁盘C/Windows/system32/drivers/etc/hosts
 192.168.214.163		blog.yunjisuan.com                                   

到浏览器查看静态网页内容
在这里插入图片描述
到浏览器查看动态网页内容
在这里插入图片描述
检查MySQL与PHP的连接是否成功

[root@localhost blog]# vim test_mysql.php 
<?php
        //$link_id=mysql_connect('主机名','用户','密码');
        $link_id=mysql_connect('localhost','root','123456');
        if($link_id){
                echo "mysql successful by daisy\n";
        }else{
                echo mysql_error();
        }       
?>
[root@localhost blog]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@localhost blog]# ls
index.html  test_info.php  test_mysql.php
[root@localhost blog]# php test_mysql.php    #激活php脚本
mysql successful by daisy
[root@localhost blog]# 

通过浏览器查看测试mysql的php脚本
在这里插入图片描述

FastCGI介绍

  1. CGI
    CGI的全称为“通用网关接口”(Common Gateway Interface),为HTTP服务器与其他机器上的程序服务通信交流的一种工具,CGI程序须运行在网络服务器上。
    传统CGI接口方式的主要缺点是性能较差,因为每次HTTP服务器遇到动态程序时都需要重新启动解析器来执行解析,之后结果才会被返回给HTTP服务器。这在处理高并发访问时几乎是不可用的,因此就诞生了FastCGI。另外,传统的CGI接口方式安全性也很差,故而现在已经很少被使用了。
  2. FastCGI
    FastCGI是一个可伸缩的,高速地在HTTP服务器和动态脚本语言间通信的接口(在Linux下,FastCGI接口即为socket,这个socket可以是文件socket,也可以是IP socket),主要优点是把动态语言和HTTP服务器分离出来。多数流行的HTTP服务器都支持FastCGI,包括Apache,Nginx和Lighttpd等。
    同时,FastCGI也被许多脚本语言所支持,例如当前比较流程的脚本语言PHP。FastCGI接口采用的是C/S架构,它可以将HTTP服务器和脚本解析服务器分开,同时还能在脚本解析服务器上启动一个或多个脚本来解析守护进程。当HTTP服务器遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求,或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。
    FastCGI的重要特点如下:
    HTTP服务器和动态脚本语言间通信的接口或工具。
    可把动态语言解析和HTTP服务器分离开。
    Nginx,Apache,Lighttpd,以及多数动态语言都支持FastCGI。
    FastCGI接口方式采用C/S结构,分为客户端(HTTP服务器)和服务器端(动态语言解析服务器)
    PHP动态语言服务器端可以启动多个FastCGI的守护进程(例如php-fpm(fcgi process mangement))
    HTTP服务器通过(例如Nginx fastcgi_pass)FastCGI客户端和动态语言FastCGI服务器端通信(例如php-fpm)
  3. Nginx FastCGI的运行原理
    Nginx不支持对外部动态程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,为了调用CGI程序,还需要一个FastCGI的wrapper(可以理解为用于启动另一个程序的程序),这个wrappper绑定在某个固定的socket上,如端口或文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或外部程序处理脚本来读取返回的数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。

屏幕快照 2017-07-13 下午9.09.02.png-535.6kB

FastCGI的主要优点是把动态语言和HTTP服务器分离开来,使Nginx专门处理静态请求及向后转发的动态请求,而PHP/PHP-FPM服务器则专门解析PHP动态请求。

WordPress博客程序的搭建准备

  1. MySQL数据库配置准备
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

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

mysql> exit
Bye

  1. Nginx及PHP环境配置准备
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  blog.yunjisuan.com;
        location / {
            root   html/blog;
            index  index.php 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@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
# 获取WordPress博客程序
[root@localhost ~]# tar xf wordpress-4.7.4-zh_CN.tar.gz 
[root@localhost ~]# cd /usr/local/nginx/html/blog/
[root@localhost blog]# rm -rf *
[root@localhost blog]# ls
[root@localhost blog]# mv ~/wordpress .
[root@localhost blog]# ls
wordpress
[root@localhost blog]# /bin/mv wordpress/* .
[root@localhost blog]# ls
index.php        wp-admin              wp-cron.php        wp-mail.php
license.txt      wp-blog-header.php    wp-includes        wp-settings.php
readme.html      wp-comments-post.php  wp-links-opml.php  wp-signup.php
wordpress        wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
[root@localhost blog]# rm -rf wordpress/
[root@localhost blog]# ls
index.php        wp-blog-header.php    wp-includes        wp-settings.php
license.txt      wp-comments-post.php  wp-links-opml.php  wp-signup.php
readme.html      wp-config-sample.php  wp-load.php        wp-trackback.php
wp-activate.php  wp-content            wp-login.php       xmlrpc.php
wp-admin         wp-cron.php           wp-mail.php
[root@localhost blog]# ll
total 188
-rw-r--r--.  1 nobody 65534   418 Sep 25  2013 index.php
-rw-r--r--.  1 nobody 65534 19935 Jan  3  2017 license.txt
-rw-r--r--.  1 nobody 65534  6956 Apr 23  2017 readme.html
-rw-r--r--.  1 nobody 65534  5447 Sep 28  2016 wp-activate.php
drwxr-xr-x.  9 nobody 65534  4096 Apr 23  2017 wp-admin
-rw-r--r--.  1 nobody 65534   364 Dec 19  2015 wp-blog-header.php
-rw-r--r--.  1 nobody 65534  1627 Aug 29  2016 wp-comments-post.php
-rw-r--r--.  1 nobody 65534  2930 Apr 23  2017 wp-config-sample.php
drwxr-xr-x.  5 nobody 65534  4096 Apr 23  2017 wp-content
-rw-r--r--.  1 nobody 65534  3286 May 25  2015 wp-cron.php
drwxr-xr-x. 18 nobody 65534 12288 Apr 23  2017 wp-includes
-rw-r--r--.  1 nobody 65534  2422 Nov 21  2016 wp-links-opml.php
-rw-r--r--.  1 nobody 65534  3301 Oct 25  2016 wp-load.php
-rw-r--r--.  1 nobody 65534 33939 Nov 21  2016 wp-login.php
-rw-r--r--.  1 nobody 65534  8048 Jan 11  2017 wp-mail.php
-rw-r--r--.  1 nobody 65534 16255 Apr  7  2017 wp-settings.php
-rw-r--r--.  1 nobody 65534 29896 Oct 19  2016 wp-signup.php
-rw-r--r--.  1 nobody 65534  4513 Oct 15  2016 wp-trackback.php
-rw-r--r--.  1 nobody 65534  3065 Sep  1  2016 xmlrpc.php
[root@localhost blog]# chown -R www.www /usr/local/nginx/html/blog
[root@localhost blog]# ll
total 188
-rw-r--r--.  1 www www   418 Sep 25  2013 index.php
-rw-r--r--.  1 www www 19935 Jan  3  2017 license.txt
-rw-r--r--.  1 www www  6956 Apr 23  2017 readme.html
-rw-r--r--.  1 www www  5447 Sep 28  2016 wp-activate.php
drwxr-xr-x.  9 www www  4096 Apr 23  2017 wp-admin
-rw-r--r--.  1 www www   364 Dec 19  2015 wp-blog-header.php
-rw-r--r--.  1 www www  1627 Aug 29  2016 wp-comments-post.php
-rw-r--r--.  1 www www  2930 Apr 23  2017 wp-config-sample.php
drwxr-xr-x.  5 www www  4096 Apr 23  2017 wp-content
-rw-r--r--.  1 www www  3286 May 25  2015 wp-cron.php
drwxr-xr-x. 18 www www 12288 Apr 23  2017 wp-includes
-rw-r--r--.  1 www www  2422 Nov 21  2016 wp-links-opml.php
-rw-r--r--.  1 www www  3301 Oct 25  2016 wp-load.php
-rw-r--r--.  1 www www 33939 Nov 21  2016 wp-login.php
-rw-r--r--.  1 www www  8048 Jan 11  2017 wp-mail.php
-rw-r--r--.  1 www www 16255 Apr  7  2017 wp-settings.php
-rw-r--r--.  1 www www 29896 Oct 19  2016 wp-signup.php
-rw-r--r--.  1 www www  4513 Oct 15  2016 wp-trackback.php
-rw-r--r--.  1 www www  3065 Sep  1  2016 xmlrpc.php

  1. 安装blog博客程序
    (1)打开浏览器输入blog.yunjisuan.com(提前做好hosts或DNS解析),回车
    在这里插入图片描述
    (2)仔细阅读页面的文字信息后,单击“现在就开始”按钮继续,然后在出现的页面表单上填写相应的内容
    在这里插入图片描述
    (3)在页面表单里填好内容后,单击结尾的“提交”按钮继续
    在这里插入图片描述
    (4)出现上图就表示可以安装了,单击“进行安装”按钮继续
    在这里插入图片描述
    (5)根据界面提示设置blog站点的信息后,单击“安装WordPress”按钮继续。
    出现下图所示的信息就表明已经成功安装了WordPress博客。
    在这里插入图片描述
    (6)后台登录
    在这里插入图片描述
    在这里插入图片描述
  2. 实现WordPress博客程序URL静态化
    实现此功能时,首先要在WordPress后台依次单击设置—>固定链接—>自定义结构,然后输入下面的代码,并保存更改。

/archives/%post_id%.html

#说明:%post_id%是数据库对应博文内容的唯一ID,例如423
在这里插入图片描述
接着,在Nginx配置文件的server容器中添加下面的代码:

[root@localhost blog]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  blog.yunjisuan.com;
        location / {
            root   html/blog;
            index  index.php index.html index.htm;
                if (-f $request_filename/index.html){     #如果用户的请求的文件名是index.html,就会重写:以任意开头,将$1重写成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 html/blog;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
    }
}
[root@localhost blog]# /usr/local/nginx/sbin/nginx -s reload
                      

用户浏览器的图片解析
用户通过PC上网,当输入blog.yunjisuan.com时,PC机向Web服务器发起请求;访问浏览器首页index.html,web浏览器读取index.html文件的内容,发给用户浏览器;用户浏览器开始解析,解析到文字时,文字就会出现在浏览器上;解析到图片时,用户浏览器将图片的地址读取出来,再次向Web服务器发起请求,Web服务器收到请求后,将图片读取出来再次发给用户浏览器,用户浏览器再把返还的内容显示到浏览器的页面上,此时图片就解析出来了。
上传图片时,图片存在存储里(挂载存储NFS),再把图片放在存储里的位置写到数据库里;
设计上传时,网页目录html由两个子目录:static(读的)和upload(上传的),NFS存储既要挂载在static目录上,还要挂载在upload目录上;静态网页读取时经过static目录,php动态网页读取时也要经过static目录,但是,PHP还要经过upload目录;PHP经过upload目录后,图片上传进upload后,还要将图片的具体位置写入MySQL数据库;这样,在下次访问index.php,SQL语句直接到MySQL中查找。

PHP有两个作用:
(1)读动态网页(读取时可以不挂载存储NFS,直接从MySQL里把URL读出来反馈给用户,用户浏览器解析的图片会再发次请求,找static读取图片)
(2)上传数据,向服务器写入数据

[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
   server {
        listen  80;
        server_name  xxx.yunjisuan.com;
        location / {
                root html/xxx;
                index index.html index.htm;
                }

        }
[root@lnmp ~]# cd /usr/local/nginx/html/
[root@lnmp html]# mkdir xxx
[root@lnmp html]#  vim xxx/index.html
welcome
[root@lnmp html]# chown -R www.www xxx/
[root@lnmp html]# /usr/local/nginx/sbin/nginx -s reload
[root@lnmp html]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.214.163 LNMP blog.yunjisuan.com xxx.yunjisuan.com
[root@lnmp html]# curl xxx.yunjisuan.com
welcome


两种表达方式

[root@lnmp html]# mv 111.jpg xxx/
[root@lnmp html]# ls xxx/
111.jpg  index.html
[root@lnmp html]# cd xxx
[root@lnmp xxx]# echo "welcome" > index.html 
[root@lnmp xxx]# vim index.html 
welcome
<img src="http://192.168.214.163/111.jpg" />

猜你喜欢

转载自blog.csdn.net/weixin_43304804/article/details/84554202