WordPress

个人博客系统分布式构建

一、案例目标

1、总目标

通过利用所学知识以及现有的资源包,以LNMP环境作为基础应用环境,分布式部署nginx、数据库和PHP环境,将数据库进行读写分离;来部署wordpress个人博客系统。

2、分目标

(1)了解数据库服务的安装。

(2)了解主从数据库集群的配置架构。

(3)了解Nginx服务的架构与使用。

(4)了解Nginx服务的编译安装与配置。

(5)了解PHP环境的使用场景。

(6)了解PHP环境的编译安装与配置。

(7)了解分布式部署WordPress的架构。

(8)了解分布式部署WordPress应用的配置与操作。

二、案例分析

1、理论知识

Mysql主从

MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理。

       数据库是一种用于存储数据集合的独立应用程序。每种数据库都会有一个或多个独特的 API,用来创建、访问、管理、搜索或复制数据库中保存的数据。除了数据库之外,也可以使用其他一些数据存储方式,比如说利用文件系统来存储文件,或者利用内存中的大型散列表,但这些系统均无法快速便利地提取或写入数据。

       因此,现在业界一般采用关系型数据库管理系统(RDBMS)来存储并管理海量数据。之所以称其为关系型数据库,是因为所有数据都存储在不同的表中,表之间的关系是建立在主键或其他键(被称为外键)的基础之上的。

Nginx服务

Nginx 是一个高性能的HTTP和反向代理web服务器。Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。处理静态文件,索引文件以及自动索引;打开文件描述符缓冲。无缓存的反向代理加速,简单的负载均衡和容错。

PHP环境

PHP(外文名:Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,利于学习,使用广泛,主要适用于 Web 开发领域。PHP 独特的语法混合了C、Java、Perl 以及 PHP 自创的语法。

       它可以比 CGI 或者 Perl 更快速地执行动态网页。用 PHP 做出的动态页面与其他的编程语言相比,PHP 是将程序嵌入到 HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成 HTML 标记的 CGI 要高许多;PHP 还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。

Wordpress

WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把 WordPress当作一个内容管理系统(CMS)来使用。

WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的,用户可以在支持 PHP 和 MySQL数据库的服务器上使用自己的博客。

LNMP

LNMP代表的是Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有Debian、CentOS、Ubuntu、Fedora、Gentoo等。

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

MySQL是一个小型关系型数据库管理系统。

PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。

这4种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

是基于lnmp环境搭建服务wrdpress来实现一款个人博客平台。

2、基础准备

主机准备

主机名

节点

mysql1

数据库主节点

mysql2

数据库从节点

nginx

Nginx服务节点

php

PHP环境节点

资源准备

VMWare Workstation软件

CentOS-7-x86_64-DVD-1511.iso镜像

nginx-1.12.2.tar.gz压缩包

libmcrypt-2.5.8.tar.gz压缩包

php-5.6.27.tar.gz压缩包

wordpress-4.7.3-zh_CN.zip压缩包

其他准备

关闭所有主机的防火墙

[root@mysql2 ~]# setenforce 0

[root@mysql2 ~]# systemctl stop firewalld

三、案例实施

1、部署mysql主从数据库

准备

主机名

节点

mysql1

主数据库节点

mysql2

从数据库节点

两台Centos 7主机

实施

使用远程连接工具CRT连接两台虚拟机

1、基础环境配置

(1)修改主机名

mysql1节点:

[root@localhost ~]# hostnamectl set-hostname mysql1

mysql2节点:

[root@localhost ~]# hostnamectl set-hostname mysql2

重新登录会刷新

(2)配置hosts文件

[root@localhost ~]#vi /etc/hosts

增加

Ip地址  mysql1

IP地址  mysql2

两个节点都要配置

(3)安装数据库服务

下载安装

# yum install -y mariadb mariadb-server

启动服务并设置开机自启

# systemctl start mariadb

# systemctl enable mariadb

两个节点都要配置

2、初始化数据库并配置主从服务

(1)初始化数据库

两个节点初始化数据库,配置数据库root密码为000000,命令如下:

[root@mysql1 ~]# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

 

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.

 

Enter current password for root (enter for none):                   #默认按回车

OK, successfully used password, moving on...

 

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

 

Set root password? [Y/n] y

New password:                                 #输入数据库root密码000000

Re-enter new password:                          #再次输入密码000000

Password updated successfully!

Reloading privilege tables..

 ... Success!

 

 

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

 

Remove anonymous users? [Y/n] y

 ... Success!

 

Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.

 

Disallow root login remotely? [Y/n] n

 ... skipping.

 

By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.

 

Remove test database and access to it? [Y/n] y

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

 

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

 

Reload privilege tables now? [Y/n] y

 ... Success!

 

Cleaning up...

 

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

 

Thanks for using MariaDB!

(2)配置mysql1主节点

修改mysql1节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容。

[root@mysql1 ~]# cat /etc/my.cnf

[mysqld]

log_bin = mysql-bin                       #记录操作日志

binlog_ignore_db = mysql                  #不同步mysql系统数据库

server_id = 30                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

 

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid

 

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

Mysql2的该配置文件也要修改,sever_id需要改,其他一致

重启数据库服务,并进入数据库,命令如下:

[root@mysql1 ~]# systemctl restart mariadb

[root@mysql1 ~]# mysql -uroot -p000000

在mysql1节点,授权在任何客户端机器上可以以root用户登录到数据库

MariaDB [(none)]> grant all privileges  on *.* to root@'%' identified by "000000";

在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限

MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000';

在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下:

[root@mysql2 ~]# systemctl restart mariadb

[root@mysql2 ~]# mysql -uroot -p000000

MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='000000';

 

使用show slave statusG命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。

可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。

MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave statusG

3、验证数据库主从服务

(1)主节点创建数据库

先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据

[root@mysql1 ~]# mysql -uroot -p000000

MariaDB [(none)]> create database test;

MariaDB [(none)]> use test;

MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));

MariaDB [test]> insert into company values(1,"alibaba","china");

MariaDB [test]> select * from company;

(2)从节点验证复制功能

登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能

[root@mysql2 ~]# mysql -uroot -p000000

MariaDB [(none)]> show databases;

MariaDB [(none)]> use test;

MariaDB [test]> show tables;

MariaDB [test]> select * from company;

2、部署Nginx服务

准备

主机名

节点

nginx

Nginx节点

nginx-1.12.2.tar.gz压缩包

一台Centos 7主机

实施

使用远程连接工具CRT连接两台虚拟机

1、基础环境配置

(1)修改主机名

[root@localhost ~]# hostnamectl set-hostname set-hostname nginx

重新登录会刷新

(2)安装基础环境并配置

[root@nginx ~]# yum install gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel -y

创建指定用户,这个nginx用户要和PHP服务器上创建的nginx两者id一致

[root@nginx ~]# groupadd -g 1001 nginx 

[root@nginx ~]#useradd -u 900 nginx -g nginx -s /sbin/nologin 

[root@nginx ~]# tail -1 /etc/passwd 

2、安装配置Nginx服务

利用ftp服务器,将提供的nginx-1.12.2.tar.gz压缩包上传至nginx节点的/usr/local/src/目录下,并解压到当前目录,命令如下:

[root@nginx src]# tar -zxvf nginx-1.12.2.tar.gz

进入nginx-1.12.2目录,编译并安装,命令如下:

[root@nginx src]# cd nginx-1.12.2/

[root@nginx-1.12.2]#./configure --prefix=/usr/local/nginx --with-http_dav_module

--with-http_stub_status_module --with-http_addition_module

--with-http_sub_module --with-http_flv_module --with-http_mp4_module

--with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx

如果没有报错提示,请进行下一步安装,命令如下:

[root@nginx-1.12.2]# make && make install

编译安装完毕后,创建软连接并启动测试

[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 

[root@nginx nginx-1.12.2]# nginx -t 

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 

[root@nginx nginx-1.12.2]# nginx

[root@nginx nginx-1.12.2]# netstat –ntpl

如果发现80端口启动,则表示Nginx服务启动成功。可以在浏览器访问地址虚拟机IP地址来查看是否出现.Nginx的欢迎页面。

3、安装PHP环境

准备

主机名

节点

php

PHP环境节点

一台Centos 7主机

libmcrypt-2.5.8.tar.gz压缩包

php-5.6.27.tar.gz压缩包

实施

1、基础环境配置

(1)修改主机名

[root@localhost ~]# hostnamectl set-hostname set-hostname php

重新登录会刷新

(2)安装基础环境并配置

[root@php ~]# yum -y install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel

使用远程传输工具,将提供的libmcrypt-2.5.8.tar.gz压缩包上传至php节点的/usr/local/src目录下,解压该压缩包,进入解压后目录,编译安装该服务,命令如下:

[root@php src]# tar -zxvf libmcrypt-2.5.8.tar.gz  

[root@php src]# cd libmcrypt-2.5.8/ 

[root@php libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt && make && make install 

2、安装PHP环境

使用远程传输工具,将提供的php-5.6.27.tar.gz压缩包上传至php节点的/usr/local/src目录下,解压该压缩包,进入解压后的目录,编译安装PHP服务,命令如下:

[root@php src]# tar -zxvf php-5.6.27.tar.gz  

[root@php src]# cd php-5.6.27/ 

[root@php php-5.6.27]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd

--with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm

--enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir

--with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash

--with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc

--with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts

如果没有报错提示,则进行下一步安装,命令如下:

[root@php php-5.6.27]# make && make install

在等待10分钟左右的时间,编译安装完毕。

3、创建用户ID

创建用户ID,注意这个nginx的id号要和nginx主机上的保持一致。命令如下:

[root@php php-5.6.27]# groupadd -g 1001 nginx 

[root@php php-5.6.27]# useradd -u 900 nginx -g nginx -s /sbin/nologin  

[root@php php-5.6.27]# tail -1 /etc/passwd 

nginx:x:900:1001::/home/nginx:/sbin/nologin 

4、配置PHP环境

PHP压缩包中提供了PHP环境需要用到的模板文件,需要对文件进行改名后才能使用,复制文件并改名,命令如下:

[root@php php-5.6.27]# cp php.ini-production /etc/php.ini 

[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm   

赋予文件执行权限,命令如下:

[root@php php-5.6.27]# chmod +x /etc/init.d/php-fpm

添加PHP服务到启动列表,并设置开机启动,命令如下:

[root@php php-5.6.27]# chkconfig --add php-fpm

[root@php php-5.6.27]# chkconfig php-fpm on

修改PHP的主配置文件php-fpm.conf,命令如下:

[root@php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf

[root@php php-5.6.27]# vi /usr/local/php5.6/etc/php-fpm.conf

[root@php ~]# grep -n '^'[a-Z] /usr/local/php5.6/etc/php-fpm.conf

25:pid = run/php-fpm.pid

149:user = nginx

150:group = nginx

164:listen = 192.168.200.60:9000

224:pm = dynamic

235:pm.max_children = 50

240:pm.start_servers = 5

245:pm.min_spare_servers = 5

250:pm.max_spare_servers = 35

找到配置文件中的相应参数并修改,修改成上述配置。

5、启动PHP服务

PHP服务,并检查是否启动成功

[root@localhost php-5.6.27]# service php-fpm start

 [root@php ~]# netstat –ntpl

如果发现9000端口已启动,则说明PHP环境安装完毕。

4、分布式部署LNMP+WordPress

准备

主机名

节点

mysql1

数据库主节点

mysql2

数据库从节点

nginx

Nginx服务节点

php

PHP环境节点

前面部署好的四台节点主机

实施

1、分布式LNMP环境的调试

(1)配置Nginx服务支持PHP环境

使用远程连接工具CRT连接到192.168.200.50虚拟机(nginx节点),并进行修改配置文件的操作。命令如下:

[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf

…省略…

location / {

            root   /www;                              #更改网页目录

            index  index.php index.html index.htm;         #添加index.php

        }

…省略…

location ~ .php$ {                                   #去掉location{}前的注释符

            root           /www;                   #更改目录为/www

            fastcgi_pass   172.16.51.60:9000;       #注意:在这里添加PHP主机IP地址

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }

…省略…

接着在/usr/local/nginx/conf/fastcgi_params添加配置,命令如下:

[root@nginx ~]# vi /usr/local/nginx/conf/fastcgi_params

document_root$fastcgi_script_name; #添加这行代码

(2)创建目录

在nginx和php节点,创建/www目录,并修改用户和用户组

nginx节点:

[root@nginx ~]# mkdir /www 

[root@nginx ~]# chown nginx:nginx /www/ 

php节点:

[root@php ~]# mkdir /www 

[root@php ~]# chown nginx:nginx /www/ 

(3)部署WordPress

两个节点,使用远程传输工具,将提供的wordpress-4.7.3-zh_CN.zip压缩包上传至nginx节点和php节点的/root目录下并解压,将解压后的文件复制到/www目录

nginx节点:

[root@nginx ~]# unzip wordpress-4.7.3-zh_CN.zip  

[root@nginx ~]# mv wordpress/* /www/

php节点:

[root@php ~]# unzip wordpress-4.7.3-zh_CN.zip  

[root@php ~]# mv wordpress/* /www/

在nginx节点,修改WordPress应用的配置文件,WordPress应用提供了wp-config-sample.php模版文件,将模板文件复制为wp-config.php,并修改

[root@nginx ~]# cp /www/wp-config-sample.php /www/wp-config.php

[root@nginx ~]# vi /www/wp-config.php

…省略…

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //

/** WordPress数据库的名称 */

define('DB_NAME', 'wordpress');

 

/** MySQL数据库用户名 */

define('DB_USER', 'root');

 

/** MySQL数据库密码 */

define('DB_PASSWORD', '000000');

 

/** MySQL主机 */

define('DB_HOST', '192.168.200.30');

 

/** 创建数据表时默认的文字编码 */

define('DB_CHARSET', 'utf8');

 

/** 数据库整理类型。如不确定请勿更改 */

define('DB_COLLATE', '');

 

…省略…

按照上面修改配置文件,保存退出后,将该配置文件scp至php节点的/www目录下,命令如下:

[root@nginx ~]# scp /www/wp-config.php root@192.168.200.60:/www/

2、创建WordPress数据库

在mysql1节点,登录数据库,使用命令创建WordPress数据库,命令如下:

[root@mysql1 ~]# mysql -uroot -p000000

MariaDB [(none)]> create database wordpress;

3、验证WordPress应用

在Nginx节点重启Nginx服务,命令如下:

[root@nginx ~]# nginx -s reload

在浏览器中输入nginx地址进行访问,会出现WordPress五分钟安装程序,填写必要的信息,然后单击左下角“安装WordPress”按钮,进行安装

 

 

安装完毕后,进入WordPress后台界面

 

单击页面左上角的“myblog”图标,进入WordPress首页。

 

至此,分布式部署LNMP+WordPress应用已完成。


 

原文地址:https://www.cnblogs.com/315z/p/13143738.html