云计算学习/第四章,应用系统分布式构建运维/部署主从数据库/部署Nginx服务/安装PHP环境/分布式部署LNMP+WordPress

第四章 应用系统分布式构建运维

4.1 实战案例——部署主从数据库

4.1.1 案例目标

1 )了解数据库服务的安装。
2 )了解主从数据库集群的配置架构。

4.1.2 案例分析

1. 规划节点
Linux 操作系统的单节点规划,见表 4-1-1。
4-1-1 节点规划
IP 192.168.200.30 192.168.200.40
主机名 mysql1 mysql2
节点 主数据库节点  从数据库节点
2. 基础准备
使用本地 PC 环境的 VMWare Workstation 软件进行实操练习,镜像使用提供的
CentOS-7-x86_64-DVD-1511.iso 。虚拟机配置为 1 /2G 内存 /20G 硬盘。

4.1.3 案例实施

1. 基础环境安装
1 )修改主机名
使用远程连接工具 CRT 连接到 192.168.200.30 192.168.200.40 这两台虚拟机,并对这
两台虚拟机进行修改主机名的操作, 192.168.200.30 主机名修改为 mysql1 192.168.200.40
主机名修改为 mysql2 。命令如下:
mysql1 节点:
[root@localhost ~]# hostnamectl set-hostname mysql1
[root@localhost ~]# logout
[root@mysql1 ~]# hostnamectl
Static hostname: mysql1 Icon name: computer-vm
Chassis: vm
Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa
Boot ID: 69ad020d53e54892b9005f82e182c140
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.el7.x86_64
Architecture: x86-64
mysql2 节点:
[root@localhost ~]# hostnamectl set-hostname mysql2
[root@localhost ~]# logout
[root@mysql2 ~]# hostnamectl
Static hostname: mysql2
Icon name: computer-vm
Chassis: vm
Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa
Boot ID: 816b270a1275496caa3254300fc359c4
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.el7.x86_64
Architecture: x86-64
2 )关闭防火墙及 SELinux 服务
两个节点关闭防火墙 firewalld SELinux 服务,命令如下:
# setenforce 0
# systemctl stop firewalld
3 )配置 hosts 文件
两个节点配置 /etc/hosts 文件,修改为如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.30 mysql1
192.168.200.40 mysql2
4 )配置 YUM 源并安装数据库服务
使用项目 3-Linux 系统与服务构建运维中的方法,挂载 CentOS-7-x86_64-DVD-1511.iso
镜像并自行配置 YUM 源,配置完毕后,两个节点安装数据库服务,命令如下:
# yum install -y mariadb mariadb-server
两个节点启动数据库服务并设置开机自启,命令如下:
# systemctl start mariadb
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to
/usr/lib/systemd/system/mariadb.service.
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...
3 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.
4 5
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 地址的最后段的数字,例如 192.168.200.30 server_id 就写 30
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
重启数据库服务,并进入数据库,命令如下:
[root@mysql1 ~]# systemctl restart mariadb
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
mysql1 节点,授权在任何客户端机器上可以以 root 用户登录到数据库,然后在主节
点上创建一个 user 用户连接节点 mysql2 ,并赋予从节点同步主节点数据库的权限。命令如
下:
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
Query OK, 0 rows affected (0.00 sec)
6 7
MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000';
Query OK, 0 rows affected (0.00 sec)
3 )配置 mysql2 从节点
修改 mysql2 节点的数据库配置文件,在配置文件 /etc/my.cnf 中的 [mysqld] 增添如下内容。
[root@mysql2 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin # 记录操作日志
binlog_ignore_db = mysql # 不同步 mysql 系统数据库
server_id = 40 # 数据库集群中的每个节点 id 都要不同,
一般使用 IP 地址的最后段的数字,例如 192.168.200.40 server_id 就写 40
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 上登录 MariaDB 数据库,配置从节点连接主节点的连接信息。
master_host 为主节点主机名 mysql1 master_user 为上一步中创建的用户 user ,命令如下: [root@mysql2 ~]# systemctl restart mariadb
[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> change master to
master_host='mysql1',master_user='user',master_password='000000';
Query OK, 0 rows affected (0.01 sec)
配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用 show slave status\G
命令,并查看从节点服务状态,如果 Slave_IO_Running Slave_SQL_Running 的状态都为
YES ,则从节点服务开启成功。命令如下:
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql1
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000005
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
8 Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 1256
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 30
1 row in set (0.00 sec)
9 可以看到 Slave_IO_Running Slave_SQL_Running 的状态都是 Yes ,配置数据库主从集
群成功。
3. 验证数据库主从服务
1 )主节点创建数据库
先在主节点 mysql1 中创建库 test ,并在库 test 中创建表 company ,插入表数据,创建完
成后,查看表 company 数据,命令如下:
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.44-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use test;
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr
varchar(255));
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> insert into company values(1,"alibaba","china");
Query OK, 1 row affected (0.01 sec)
MariaDB [test]> select * from company;
+----+---------+-------+
| id | name | addr |
10 +----+---------+-------+
| 1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)
2 )从节点验证复制功能
登录 mysql2 节点的数据库,查看数据库列表。找到 test 数据库,查询表,并查询内容
验证从数据库的复制功能,命令如下:
[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
11 12
Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company |
+----------------+
1 row in set (0.00 sec)
MariaDB [test]> select * from company;
+----+---------+-------+
| id | name | addr |
+----+---------+-------+
| 1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)
可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。 13

4.2 实战案例——部署 Nginx 服务

4.2.1 案例目标

1 )了解 Nginx 服务的架构与使用。
2 )了解 Nginx 服务的编译安装与配置。

4.2.2 案例分析

1. 规划节点
Linux 操作系统的单节点规划,见表 4-2-1。
4-2-1 节点规划
IP 192.168.200.50
主机名 nginx
节点 Nginx 节点
2. 基础准备
使用本地 PC 环境的 VMWare Workstation 软件进行实操练习,镜像使用提供的
CentOS-7-x86_64-DVD-1511.iso 。虚拟机配置为 1 /2G 内存 /20G 硬盘。

4.2.3 案例实施

1. 基础环境安装
1 )修改主机名
使用远程连接工具 CRT 连接到 192.168.200.50 虚拟机,并进行修改主机名的操作,将
192.168.200.50 主机名修改为 nginx 。命令如下:
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# logout
[root@nginx ~]# hostnamectl
Static hostname: nginx
Icon name: computer-vm
Chassis: vm
Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa
Boot ID: 9ac638fec1c64c94aab96d0acfa4f542 Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.el7.x86_64
Architecture: x86-64
2 )关闭防火墙及 SELinux 服务
关闭防火墙 firewalld SELinux 服务,命令如下:
# setenforce 0
# systemctl stop firewalld
3 )安装配置基础服务
使用 CentOS-7-x86_64-DVD-1511.iso 文件自行配置本地 YUM 源,编译安装基础环境,
命令如下:
[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
nginx:x:900:1001::/home/nginx:/sbin/nologin
4 )安装配置 Nginx 服务
使用远程传输工具,将提供的 nginx-1.12.2.tar.gz 压缩包上传至 nginx 节点的 /usr/local/src/
目录下,并解压到当前目录,命令如下:
[root@nginx src]# tar -zxvf nginx-1.12.2
进入 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
14 15
编译安装完毕后,创建软连接并启动测试,命令如下:( netstat 命令无法使用时,请自
行使用 YUM 源安装 net-tools 工具)
[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
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
5726/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
1400/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
2274/master
tcp6 0 0 :::22 :::* LISTEN
1400/sshd
tcp6 0 0 ::1:25 :::* LISTEN
2274/master
如果发现 80 端口启动,则表示 Nginx 服务启动成功。可以在浏览器访问地址
192.168.200.50 来查看是否出现 Nginx 的欢迎页面。 16

4.3 实战案例——安装 PHP 环境

4.3.1 案例目标

1 )了解 PHP 环境的使用场景。
2 )了解 PHP 环境的编译安装与配置。

4.3.2 案例分析

1. 规划节点
Linux 操作系统的单节点规划,见表 4-3-1。
4-3-1 节点规划
IP 192.168.200.60
主机名 php
节点 PHP 环境节点
2. 基础准备
使用本地 PC 环境的 VMWare Workstation 软件进行实操练习,镜像使用提供的
CentOS-7-x86_64-DVD-1511.iso 。虚拟机配置为 1 /2G 内存 /20G 硬盘。

4.3.3 案例实施

1. 基础环境安装
1 )修改主机名
使用远程连接工具 CRT 连接到 192.168.200.60 虚拟机,并进行修改主机名的操作,将
192.168.200.60 主机名修改为 php 。命令如下:
[root@localhost ~]# hostnamectl set-hostname php
[root@localhost ~]# logout
[root@php ~]# hostnamectl
Static hostname: php
Icon name: computer-vm
Chassis: vm
Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa
Boot ID: e48eac1312ca40beae638f6f43e0c31a Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-327.el7.x86_64
Architecture: x86-64
2 )关闭防火墙及 SELinux 服务
关闭防火墙 firewalld SELinux 服务,命令如下:
# setenforce 0
# systemctl stop firewalld
3 )安装配置基础服务
使用 CentOS-7-x86_64-DVD-1511.iso 文件自行配置本地 YUM 源,编译安装基础环境,
命令如下:
[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
4 )安装 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
17 如果没有报错提示,则进行下一步安装,命令如下:
[root@php php-5.6.27]# make && make install
在等待 10 分钟左右的时间,编译安装完毕。
5 )创建用户 ID
创建用户 ID ,注意这个 nginx id 号要和 nginx 主机( 192.168.200.50 )上的保持一致。
命令如下:
[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
6 )配置 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]# vim /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
18 235:pm.max_children = 50
240:pm.start_servers = 5
245:pm.min_spare_servers = 5
250:pm.max_spare_servers = 35
找到配置文件中的相应参数并修改,修改成上述配置。
7 )启动 PHP 服务
在完成上述配置并保存退出之后,就可以启动 PHP 服务,并检查是否启动成功。命令
如下:( netstat 命令无法使用时,请自行使用 YUM 源安装 net-tools 工具)
[root@localhost php-5.6.27]# service php-fpm start
Starting php-fpm done
[root@php ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 192.168.200.60:9000 0.0.0.0:* LISTEN
123948/php-fpm: mas
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
1403/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
2516/master
tcp6 0 0 :::22 :::* LISTEN
1403/sshd
tcp6 0 0 ::1:25 :::* LISTEN
2516/master
如果发现 9000 端口已启动,则说明 PHP 环境安装完毕。
19 20

4.4 实战案例——分布式部署 LNMP+WordPress

4.4.1 案例目标

1 )了解分布式部署 WordPress 的架构。
2 )了解分布式部署 WordPress 应用的配置与操作。

4.4.2 案例分析

1. 规划节点
Linux 操作系统的单节点规划,见表 4-4-1。
4-4-1 节点规划
IP 192.168.200.30 192.168.200.40 192.168.200.50 192.168.200.60
主机名 mysql1 mysql2 nginx php
节点 数据库主节点 数据库从节点 Nginx 服务节点 PHP 环境节点
2. 基础准备
使用实战案例 4.1-4.3 创建的虚拟机完成本次案例。在实战案例 4.1-4.3 中,已经完成了
主从数据库的安装配置、 Nginx 服务的安装、 PHP 环境的安装,本实战案例将进行分布式
LNMP 环境的调试及 WordPress 应用的部署。

4.1.3 案例实施

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 192.168.200.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
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 添加这
行代码
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
21 fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
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 目录,命令如下:( unzip
命令不能使用时,请自行使用 YUM 源安装 unzip 工具)
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 ,并修改,命令如下:
22 [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/
4 )创建 WordPress 数据库
mysql1 节点,登录数据库,使用命令创建 WordPress 数据库,命令如下:
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
23 Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB-log MariaDB Server
Copyright (c) 2000, 2015, 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)]> Ctrl-C -- exit!
Aborted
5 )验证 WordPress 应用
在浏览器中输入 192.168.200.50 地址进行访问,会出现著名的 WordPress 五分钟安装程
序,填写必要的信息,然后单击左下角“安装 WordPress ”按钮,进行 WordPress 应用的安
装,如图 4-6-1 所示。
4-6-1 WordPress 安装界面
24 稍等片刻,安装完毕后,进入 WordPress 后台界面,如图 4-6-2 所示。
4-6-2 WordPress 后台界面
单击图 4-6-2 页面左上角的“ blog ”图标,进入 WordPress 首页,如图 4-6-3 所示。
4-6-3 WordPress 首页
至此,分布式部署 LNMP+WordPress 应用已完成。
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茶杯盖儿~

你的鼓励是小茶杯学习的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值