发布时间:2025-06-24 20:13:52 作者:北方职教升学中心 阅读量:238
接着在/usr/local/nginx/conf/fastcgi_params 添加配置,命令如下:
[root@nginx ~]# vi /usr/local/nginx/conf/fastcgi_paramsfastcgi_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;21fastcgi_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-redirectfastcgi_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');/** 数据库整理类型。命令如下:MariaDB [(none)]> start slave;MariaDB [(none)]> show slave status\G*** 1. row ***Slave_IO_State: Waiting for master to send eventMaster_Host: mysql1Master_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 245Relay_Log_File: mariadb-relay-bin.000005Relay_Log_Pos: 529Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes8Slave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 245Relay_Log_Space: 1256Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 301 row in set (0.00 sec)9可以看到 Slave_IO_Running 和 Slave_SQL_Running 的状态都是 Yes,配置数据库主从集群成功。[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 就写 30datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-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.logpid-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 -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 2Server version: 5.5.44-MariaDB-log MariaDB ServerCopyright (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,并赋予从节点同步主节点数据库的权限。
[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 就写 40datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-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.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d
在从节点 mysql2 上登录 MariaDB 数据库,配置从节点连接主节点的连接信息。命令如下:
mysql1 节点:[root@localhost ~]# hostnamectl set-hostname mysql1[root@localhost ~]# logout[root@mysql1 ~]# hostnamectlStatic hostname: mysql1Icon name: computer-vmChassis: vmMachine ID: 179f6c8f2e7942ef81b0f5565a6883faBoot ID: 69ad020d53e54892b9005f82e182c140Virtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-327.el7.x86_64Architecture: x86-64mysql2 节点:[root@localhost ~]# hostnamectl set-hostname mysql2[root@localhost ~]# logout[root@mysql2 ~]# hostnamectlStatic hostname: mysql2Icon name: computer-vmChassis: vmMachine ID: 179f6c8f2e7942ef81b0f5565a6883faBoot ID: 816b270a1275496caa3254300fc359c4Virtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-327.el7.x86_64Architecture: x86-64
(2)关闭防火墙及 SELinux 服务
两个节点关闭防火墙 firewalld 及 SELinux 服务,命令如下:
# setenforce 0# systemctl stop firewalld
(3)配置 hosts 文件
两个节点配置/etc/hosts 文件,修改为如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain42::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.200.30 mysql1192.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 mariadbCreated 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 foundNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALLMariaDBSERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andyou 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...3Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] yNew password: #输入数据库 root 密码 000000Re-enter new password: #再次输入密码 000000Password updated successfully!Reloading privilege tables..... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures 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 canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.45Remove 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!
(2)配置 mysql1 主节点
修改 mysql1 节点的数据库配置文件,在配置文件/etc/my.cnf 中的[mysqld]增添如下内容。4.3.2 案例分析
1.规划节点Linux 操作系统的单节点规划,见表 4-3-1。4.2.3 案例实施
1.基础环境安装(1)修改主机名使用远程连接工具 CRT 连接到 192.168.200.50 虚拟机,并进行修改主机名的操作,将192.168.200.50 主机名修改为 nginx。命令如下:[root@localhost ~]# hostnamectl set-hostname php[root@localhost ~]# logout[root@php ~]# hostnamectlStatic hostname: phpIcon name: computer-vmChassis: vmMachine ID: 179f6c8f2e7942ef81b0f5565a6883faBoot ID: e48eac1312ca40beae638f6f43e0c31aVirtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-327.el7.x86_64Architecture: 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-develbzip2-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 && makeinstall(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-zts17如果没有报错提示,则进行下一步安装,命令如下:[root@php php-5.6.27]# make && make install在等待 10 分钟左右的时间,编译安装完毕。4.1.3 案例实施
1.基础环境安装(1)修改主机名使用远程连接工具 CRT 连接到 192.168.200.30、表、第四章 应用系统分布式构建运维
4.1 实战案例——部署主从数据库
4.1.1 案例目标
(1)了解数据库服务的安装。如不确定请勿更改 */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 -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.23Your MariaDB connection id is 5Server version: 5.5.44-MariaDB-log MariaDB ServerCopyright (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-3-1 节点规划IP 192.168.200.60主机名 php节点 PHP 环境节点2.基础准备使用本地 PC 环境的 VMWare Workstation 软件进行实操练习,镜像使用提供的CentOS-7-x86_64-DVD-1511.iso。图 4-6-1 WordPress 安装界面24稍等片刻,安装完毕后,进入 WordPress 后台界面,如图 4-6-2 所示。19204.4 实战案例——分布式部署 LNMP+WordPress
4.4.1 案例目标
(1)了解分布式部署 WordPress 的架构。使用 show slave status\G命令,并查看从节点服务状态,如果 Slave_IO_Running 和 Slave_SQL_Running 的状态都为YES,则从节点服务开启成功。4.2.2 案例分析
1.规划节点Linux 操作系统的单节点规划,见表 4-2-1。虚拟机配置为 1 核/2G 内存/20G 硬盘。4.1.3 案例实施
1.分布式 LNMP 环境的调试(1)配置 Nginx 服务支持 PHP 环境使用远程连接工具 CRT 连接到 192.168.200.50 虚拟机(nginx 节点),并进行修改配置文件的操作。表 4-2-1 节点规划IP 192.168.200.50主机名 nginx节点 Nginx 节点2.基础准备使用本地 PC 环境的 VMWare Workstation 软件进行实操练习,镜像使用提供的CentOS-7-x86_64-DVD-1511.iso。(2)了解分布式部署 WordPress 应用的配置与操作。4.3.3 案例实施
1.基础环境安装(1)修改主机名使用远程连接工具 CRT 连接到 192.168.200.60 虚拟机,并进行修改主机名的操作,将192.168.200.60 主机名修改为 php。命令如下:MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";Query OK, 0 rows affected (0.00 sec)67MariaDB [(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@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/passwdnginx: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.conf25:pid = run/php-fpm.pid149:user = nginx150:group = nginx164:listen = 192.168.200.60:9000224:pm = dynamic18235:pm.max_children = 50240:pm.start_servers = 5245:pm.min_spare_servers = 5250:pm.max_spare_servers = 35找到配置文件中的相应参数并修改,修改成上述配置。Nginx 服务的安装、PHP 环境的安装,本实战案例将进行分布式LNMP 环境的调试及 WordPress 应用的部署。命令如下:[root@localhost ~]# hostnamectl set-hostname nginx[root@localhost ~]# logout[root@nginx ~]# hostnamectlStatic hostname: nginxIcon name: computer-vmChassis: vmMachine ID: 179f6c8f2e7942ef81b0f5565a6883faBoot ID: 9ac638fec1c64c94aab96d0acfa4f542Virtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-327.el7.x86_64Architecture: 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/passwdnginx: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 install1415编译安装完毕后,创建软连接并启动测试,命令如下:(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 -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: 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 -ntplActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address StatePID/Program nametcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN5726/nginx: mastertcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN1400/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN2274/mastertcp6 0 0 :::22 :::* LISTEN1400/sshdtcp6 0 0 ::1:25 :::* LISTEN2274/master如果发现 80 端口启动,则表示 Nginx 服务启动成功。表 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-6-2 WordPress 后台界面单击图 4-6-2 页面左上角的“blog”图标,进入 WordPress 首页,如图 4-6-3 所示。命令如下:[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; #更改目录为/wwwfastcgi_pass 192.168.200.60:9000; #注意:在这里添加 PHP 主机IP 地址fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi_params;}…省略…修改完毕后,保存退出。信息,验证从数据库的复制功能成功。可以在浏览器访问地址192.168.200.50 来查看是否出现 Nginx 的欢迎页面。(2)了解主从数据库集群的配置架构。(7)启动 PHP 服务在完成上述配置并保存退出之后,就可以启动 PHP 服务,并检查是否启动成功。134.2 实战案例——部署 Nginx 服务
4.2.1 案例目标
(1)了解 Nginx 服务的架构与使用。4.4.2 案例分析
1.规划节点Linux 操作系统的单节点规划,见表 4-4-1。在实战案例 4.1-4.3 中,已经完成了主从数据库的安装配置、(2)了解 PHP 环境的编译安装与配置。192.168.200.40 这两台虚拟机,并对这两台虚拟机进行修改主机名的操作,192.168.200.30 主机名修改为 mysql1,192.168.200.40主机名修改为 mysql2。4.1.2 案例分析
1.规划节点Linux 操作系统的单节点规划,见表 4-1-1。4-6-3 WordPress 首页
至此,分布式部署 LNMP+WordPress 应用已完成。(2)了解 Nginx 服务的编译安装与配置。表 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。master_host 为主节点主机名 mysql1,master_user 为上一步中创建的用户 user,命令如下:[root@mysql2 ~]# systemctl restart mariadb[root@mysql2 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 10Server version: 5.5.44-MariaDB MariaDB ServerCopyright (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 tomaster_host='mysql1',master_user='user',master_password='000000';Query OK, 0 rows affected (0.01 sec)配置完毕主从数据库之间的连接信息之后,开启从节点服务。找到 test 数据库,查询表,并查询内容验证从数据库的复制功能,命令如下:[root@mysql2 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 5Server version: 5.5.44-MariaDB-log MariaDB ServerCopyright (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 namesYou can turn off this feature to get a quicker startup with -A1112Database changedMariaDB [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)可以查看到主数据库中刚刚创建的库、虚拟机配置为 1 核/2G 内存/20G 硬盘。164.3 实战案例——安装 PHP 环境
4.3.1 案例目标
(1)了解 PHP 环境的使用场景。虚拟机配置为 1 核/2G 内存/20G 硬盘。(5)创建用户 ID创建用户 ID,注意这个 nginx 的 id 号要和 nginx 主机(192.168.200.50)上的保持一致。命令如下:(netstat 命令无法使用时,请自行使用 YUM 源安装 net-tools 工具)[root@localhost php-5.6.27]# service php-fpm startStarting php-fpm done[root@php ~]# netstat -ntplActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address StatePID/Program nametcp 0 0 192.168.200.60:9000 0.0.0.0:* LISTEN123948/php-fpm: mastcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN1403/sshdtcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN2516/mastertcp6 0 0 :::22 :::* LISTEN1403/sshdtcp6 0 0 ::1:25 :::* LISTEN2516/master如果发现 9000 端口已启动,则说明 PHP 环境安装完毕。3.验证数据库主从服务(1)主节点创建数据库先在主节点 mysql1 中创建库 test,并在库 test 中创建表 company,插入表数据,创建完成后,查看表 company 数据,命令如下:[root@mysql1 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 4Server version: 5.5.44-MariaDB-log MariaDB ServerCopyright (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 changedMariaDB [test]> create table company(id int not null primary key,name varchar(50),addrvarchar(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 节点的数据库,查看数据库列表。