Standalone
新建4个虚拟机
实验环境
| 主机名 | IP 地址 | 角色 |
|---|---|---|
| www.tongxi66.top | 10.1.8.21 | apache |
| php.tongxi66.top | 10.1.8.22 | php |
| db.tongxi66.top | 10.1.8.23 | mariadb |
| nfs.tongxi66.top | 10.1.8.24 | nfs |
所有节点关闭防火墙和SELinux。
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
systemctl disable firewalld --now
预配置
所有节点配置名称解析:
bash
[root@all-node ~ ]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.21 www.tongxi66.top www
10.1.8.22 php.tongxi66.top php
10.1.8.23 db.tongxi66.top db
10.1.8.24 nfs.tongxi66.top nfs
Windows配置域名解析
txt
C:\Windows\System32\drivers\etc\hosts
10.1.8.21 www.tongxi66.top
部署 nfs 服务器
由于静态文件和动态文件没有分开,所以 Web 服务器和 PHP 服务器都要存一份。实验环境通过NFS共享提供wordpress应用。
下载 wordpress,上传到家目录。
bash
[root@nfs ~]# yum install -y nfs-utils
[root@nfs ~]# mkdir /www
[root@nfs ~]# echo '/www 10.1.8.0/24(rw)' > /etc/exports
[root@nfs ~]# systemctl enable nfs-server.service --now
#提前下载好的wordpress-4.9.4-zh_CN.zip 拉入
# 在准备解压 wordpress资源
[root@nfs ~]# unzip -o wordpress-4.9.4-zh_CN.zip -d /www/
# 准备网页测试文件
[root@nfs ~]# echo 'Hello World !' > /www/index.html
[root@nfs ~]# cat > /www/index.php <<EOF
<?php
echo "<h1>Hello World !</h1>\n";
?>
EOF
[root@nfs ~]# cat > /www/test-mysql.php <<'EOF'
<?php
$link=mysqli_connect('db.tongxi66.top','wordpress','123');
if($link)
echo "<h1>Connect Mysql Success !</h1>\n";
else
echo "<h1>Connect Mysql Failed !</h1>\n";
$link->close();
?>
EOF
[root@nfs ~]# cat > /www/info.php <<EOF
<?php
phpinfo();
?>
EOF
# 通过查询www服务器,获知nginx账户信息
[root@www ~ ]# grep nginx /etc/passwd
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@nfs ~]# chown -R 998 /www
部署 db 服务器
sql
[root@db ~]# yum install -y mariadb-server
[root@db ~]# systemctl enable mariadb --now
# 加固 MariaDB
[root@db ~]# mysql_secure_installation
# 交互式提示您进行更改,包括:
# - 为root帐户设置密码,例如redhat。
# - 禁止root帐户从本地主机外部访问数据库。
# - 删除匿名用户帐户。
# - 删除用于演示的test数据库。
# 准备wordpress数据库和用户
[root@db ~]# mysql -uroot -p123
mysql> CREATE DATABASE wordpress;
mysql> CREATE USER wordpress@'%' identified by '123';
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%';
mysql> FLUSH PRIVILEGES;
mysql> exit
部署 Nginx 服务器
bash
# 部署 Nginx 服务
[root@www ~]# yum install -y nginx
[root@www ~]# systemctl enable nginx --now
# 安装 nfs 工具
[root@www ~]# yum install -y nfs-utils
# 挂载存储
[root@www ~]# echo 'nfs.tongxi66.top:/www /usr/share/nginx/html nfs defaults 0 0' >> /etc/fstab
[root@www ~]# mount /usr/share/nginx/html/
[root@www ~]# df -h/usr/share/nginx/html/
Filesystem Size Used Avail Use% Mounted on
nfs.tongxi66.top:/www 64G 3.2G 61G 5% /usr/share/nginx/html
[root@www ~]# ls /usr/share/nginx/html/
index.html index.php info.php test-mysql.php wordpress
部署 PHP 服务器
部署 php 服务
bash
[root@php ~]# echo 'nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin' >> /etc/passwd
[root@php ~]# groupadd -g 996 nginx
[root@php ~]# yum install -y php php-fpm php-mysqlnd
[root@php ~]# vim /etc/php-fpm.d/www.conf
bash
#使用;号注释掉原有listen行
;listen = 127.0.0.1:9000
# 新增listen 监听所有ip的9000端口
listen = 9000
# 支持监听特定ip的9000端口,例如listen = 10.1.8.22:9000
# 使用;号注释掉原有 listen.allowed_clients 行
# 允许所有客户端访问
;listen.allowed_clients = 127.0.0.1
# 设置运行用户
user = nginx
group = nginx
[root@php ~]# systemctl enable php-fpm.service --now
挂载存储
bash
# 安装 nfs 工具
[root@php ~]# yum install -y nfs-utils
# 挂载存储
[root@php ~]# echo 'nfs.tongxi66.top:/www /www nfs defaults 0 0' >> /etc/fstab
[root@php ~]# mkdir /www
[root@php ~]# mount /www
[root@php ~]# df -h /www
Filesystem Size Used Avail Use% Mounted on
nfs.tongxi66.top:/www 64G 3.2G 61G 5% /www
[root@php ~]# ls /www
index.html index.php info.php test-mysql.php wordpress
php 程序测试
bash
[root@php ~]# php /www/index.php
<h1>Hello World !</h1>
[root@php ~]# php /www/test-mysql.php
<h1>Connect Mysql Success !</h1>
# 执行 info.php 的输出内容太多,这里省略。
[root@php ~]# php /www/info.php
配置 Nginx 对接 PHP
bash
[root@www ~]# cat > /etc/nginx/conf.d/vhost-www.conf <<'EOF'
server {
listen 80;
server_name www.tongxi66.top;
# 静态资源处理
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
# PHP 请求处理
location ~ \.php$ {
# 配置 PHP-FPM 监听的地址和端口
fastcgi_pass php.tongxi66.top:9000;
fastcgi_index index.php;
# 配置 php 服务器上 wordpress 应用所在位置
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
# 重启服务
[root@www ~]# systemctl restart nginx
测试应用
客户端配置 www.tongxi66.top 名称解析。访问http://www.tongxi66.top/wordpress/。