LNMP部署实验

LNMP部署实验

文章目录

  • LNMP部署实验
    • [LNMP 工作原理](#LNMP 工作原理)
    • ALL-IN-ONE
    • Standalone
    • 分离部署
      • [部署 nfs 服务器](#部署 nfs 服务器)
      • [部署 db 服务器](#部署 db 服务器)
      • [部署 Nginx 服务器](#部署 Nginx 服务器)
      • [部署 PHP 服务器](#部署 PHP 服务器)
  • [php 程序测试](#php 程序测试)

LNMP 工作原理

  1. 客户端通过HTTP协议请求web服务器资源

  2. web 服务器根据资源类型进行处理:

    • 静态资源:web直接把资源返回至客户端。

    • 动态资源:通过指定的通讯方式将脚本网页交给后端程序执行。如果运算期间需要连接mysql数据库,则通过mysql连接器连接mysql。后端程序将运算结果返回给web服务。

  3. web服务将结果返回给客户端。

ALL-IN-ONE

WordPress

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

bash 复制代码
# 主机名blog.demo.cloud IP地址10.1.8.10

# 安装服务端
[root@server ~ 09:01:55]# yum install -y mariadb-server

# 启用并启动服务
[root@server ~ 09:33:32]# systemctl enable mariadb.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

# 加固 MariaDB
[root@server ~ 09:33:40]# mysql_secure_installation


#部署 Nginx 服务
[root@server ~ 09:34:10]# yum install -y nginx

[root@server ~ 09:34:24]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@server ~ 09:34:38]# echo hello world from nginx > /usr/share/nginx/html/index.html 
[root@server ~ 09:35:12]# curl 10.1.8.10
hello world from nginx
[root@server ~ 09:35:18]# yum install -y php php-fpm php-mysqlnd

#部署 php 服务

# php-fpm 进程默认以 apache 用户身份运行,修改运行用户为 nginx,并重启服务
[root@server ~ 09:35:30]# vim /etc/php-fpm.d/www.conf
[root@server ~ 09:36:00]# systemctl enable php-fpm.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@server ~ 09:36:12]# vim /etc/nginx/nginx.conf
[root@server ~ 09:38:02]# systemctl restart nginx.service

root@server ~ 09:03:59]# vim /etc/nginx/default.d/php.conf
[root@server ~ 09:36:54]# cat /etc/nginx/default.d/php.conf
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#准备数据库
[root@server ~ 09:39:41]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, 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)]> create user wordpress identified by '123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on wordpress.* to wordpress;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

# 准备 test-mysql.php
[root@server ~ 09:41:49]# cat > /usr/share/nginx/html/test-mysql.php <<'EOF'
> <?php
>   $link=mysqli_connect('10.1.8.10','wordpress','wordpress');
>   if($link)
>     echo "<h1>Connect Mysql Success !</h1>\n";
>   else
>     echo "<h1>Connect Mysql Failed !</h1>\n";
>   $link->close();
> ?>
> EOF

[root@server html 09:44:27]# vim /etc/nginx/nginx.conf
[root@server html 09:45:36]# systemctl restart nginx.service
[root@server html 09:45:43]# curl http://10.1.8.10/
<h1>Hello World !</h1>
[root@server html 09:45:45]# curl http://10.1.8.10/test-mysql.php
<h1>Connect Mysql Success !</h1>

#部署 wordpress 应用
[root@server html 09:45:50]# yum install -y wget

[root@server ~ 09:46:36]# ls
anaconda-ks.cfg  wordpress-4.9.4-zh_CN.zip
[root@server ~ 09:46:38]# unzip wordpress-4.9.4-zh_CN.zip

[root@server ~ 09:47:05]# ls
anaconda-ks.cfg  wordpress  wordpress-4.9.4-zh_CN.zip
[root@server ~ 09:47:09]# mv wordpress /usr/share/nginx/html/
[root@server ~ 09:47:21]# chown -R nginx:nginx /usr/share/nginx/html/
[root@server ~ 09:47:40]# cd /usr/share/nginx/html/

#配置文件server块下中指定主页

[root@server html 17:27:18]# cat /etc/nginx/nginx.conf

# Settings for a TLS enabled server.
#
    server {
        ...
        root         /usr/share/nginx/html;
        index index.php index.html;
...

Standalone

bash 复制代码
#初始化设置主机名和IP地址

# NFS 服务器

10.1.8.21 www.demo.cloud www
10.1.8.22 php.demo.cloud php
10.1.8.23 db.demo.cloud db
10.1.8.24 nfs.demo.cloud nfs

hostnamectl set-hostname nfs.demo.cloud
nmcli connection modify ens32 ipv4.addresses 10.1.8.24/24
nmcli connection up ens32 


# DB 服务器

10.1.8.21 www.demo.cloud www
10.1.8.22 php.demo.cloud php
10.1.8.23 db.demo.cloud db
10.1.8.24 nfs.demo.cloud nfs

hostnamectl set-hostname db.demo.cloud
nmcli connection modify ens32 ipv4.addresses 10.1.8.23/24
nmcli connection up ens32 

# php 服务器

10.1.8.21 www.demo.cloud www
10.1.8.22 php.demo.cloud php
10.1.8.23 db.demo.cloud db
10.1.8.24 nfs.demo.cloud nfs

hostnamectl set-hostname php.demo.cloud
nmcli connection modify ens32 ipv4.addresses 10.1.8.22/24
nmcli connection up ens32 


# www 服务器

10.1.8.21 www.demo.cloud www
10.1.8.22 php.demo.cloud php
10.1.8.23 db.demo.cloud db
10.1.8.24 nfs.demo.cloud nfs

hostnamectl set-hostname www.demo.cloud
nmcli connection modify ens32 ipv4.addresses 10.1.8.21/24
nmcli connection up ens32 

#cat /etc/hosts 和 hostname 测验

分离部署

部署 nfs 服务器

由于静态文件和动态文件没有分开,所以 Web 服务器和 PHP 服务器都要存一份。实验环境通过NFS共享提供wordpress应用。下载 wordpress,上传到家目录。

bash 复制代码
[root@nfs ~ 11:01:53]# yum install -y nfs-utils

[root@nfs ~ 11:26:08]# echo '/www 10.1.8.0/24(rw)' > /etc/exports
[root@nfs ~ 11:26:55]# vim /etc/exports
[root@nfs ~ 11:27:02]# systemctl enable nfs-server.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

# 准备 wordpress资源
[root@nfs ~ 11:27:11]# wget http://192.168.46.88/01.softwares/wordpress-4.9.4-zh_CN.zip

[root@nfs ~ 11:27:27]# ls
anaconda-ks.cfg  wordpress-4.9.4-zh_CN.zip
[root@nfs ~ 11:27:28]# ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr  www
[root@nfs ~ 11:27:36]# unzip -o wordpress-4.9.4-zh_CN.zip -d /www/

[root@nfs ~ 11:27:44]# ls /www/
wordpress

# 准备网页测试文件
[root@nfs ~ 11:27:49]# echo 'hello world !' > /www/index.html
[root@nfs ~ 11:28:34]# cat > /www/index.php <<EOF
> <?php
>   echo "<h1>Hello World !</h1>\n";
> ?>
> EOF
[root@nfs ~ 11:28:47]# cat > /www/test-mysql.php <<'EOF'
> <?php
>   $link=mysqli_connect('db.demo.cloud','wordpress','123');
>   if($link)
>     echo "<h1>Connect Mysql Success !</h1>\n";
>   else
>     echo "<h1>Connect Mysql Failed !</h1>\n";
>   $link->close();
> ?>
> EOF
[root@nfs ~ 11:29:19]# cat > /www/info.php <<EOF
> <?php
>   phpinfo();
> ?>
> EOF
[root@nfs ~ 11:29:28]# ls /www/
index.html  index.php  info.php  test-mysql.php  wordpress
[root@nfs ~ 11:29:31]# chown -R 998 /www

部署 db 服务器

bash 复制代码
[root@db ~ 10:41:00]# yum install -y mariadb-server

[root@db ~ 11:30:33]# systemctl enable mariadb --now

# 加固 MariaDB
[root@db ~ 11:30:46]# mysql_secure_installation 

# 准备wordpress数据库和用户
[root@db ~ 11:31:11]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, 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)]> create user wordpress indentified by '123';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'indentified by '123'' at line 1
MariaDB [(none)]> CREATE USER wordpress@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on wordpress.* to wordpress
    -> ;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

部署 Nginx 服务器

bash 复制代码
# 部署 Nginx 服务

[root@www ~ 10:40:59]# yum install -y nginx

[root@www ~ 11:03:18]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
[root@www ~ 11:03:27]# yum makecache 

[root@www ~ 11:09:54]# grep nginx /etc/passwd
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin

[root@www ~ 11:34:41]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

# 安装 nfs 工具
[root@www ~ 11:34:59]# yum install -y nfs-utils

# 挂载存储
[root@www ~ 11:35:30]# echo 'nfs.demo.cloud:/www /usr/share/nginx/html nfs defaults 0 0' >> /etc/fstab
[root@www ~ 11:35:39]# vim /etc/fstab
[root@www ~ 11:35:51]# mount /usr/share/nginx/html/

[root@www ~ 11:36:18]# df -h /usr/share/nginx/html/
Filesystem           Size  Used Avail Use% Mounted on
nfs.demo.cloud:/www   50G  1.7G   49G   4% /usr/share/nginx/html
[root@www ~ 11:36:26]# ls /usr/share/nginx/html/
index.html  index.php  info.php  test-mysql.php  wordpress
[root@www ~ 11:36:32]# grep nginx /etc/passwd
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin

#配置 Nginx 对接 PHP
[root@www ~ 11:37:12]# cat > /etc/nginx/conf.d/vhost-www.conf <<'EOF'
> server {
>     listen 80;
>     server_name www.demo.cloud;
> 
>     # 静态资源处理
>     location / {
>         root /usr/share/nginx/html;
>         index index.html index.htm index.php;
>     }
> 
>     # PHP 请求处理
>     location ~ \.php$ {
>         # 配置 PHP-FPM 监听的地址和端口
>         fastcgi_pass php.demo.cloud:9000;
>         fastcgi_index index.php;
>         # 配置 php 服务器上 wordpress 应用所在位置
>         fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
>         include fastcgi_params;
>     }
> }
> EOF
[root@www ~ 11:43:29]# vim /etc/nginx/conf.d/vhost-www.conf 

# 重启服务
[root@www ~ 11:43:46]# systemctl restart nginx

部署 PHP 服务器

bash 复制代码
[root@php ~ 10:40:59]# yum makecache

#部署 php 服务

[root@php ~ 11:36:43]# echo 'nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin' >> /etc/passwd
[root@php ~ 11:37:44]# tail -n1 /etc/passwd
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@php ~ 11:37:54]# groupadd -g 996 nginx
[root@php ~ 11:38:09]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
[root@php ~ 11:38:19]# yum install -y php php-fpm php-mysqlnd

[root@php ~ 11:40:09]# vim /etc/php-fpm.d/www.conf

#使用;号注释掉原有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 ~ 11:41:33]# systemctl enable php-fpm.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

# 安装 nfs 工具
[root@php ~ 11:41:41]# yum install -y nfs-utils

# 挂载存储
[root@php ~ 11:41:53]# echo 'nfs.demo.cloud:/www /www nfs defaults 0 0' >> /etc/fstab
[root@php ~ 11:42:04]# 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.demo.cloud www
10.1.8.22 php.demo.cloud php
10.1.8.23 db.demo.cloud db
10.1.8.24 nfs.demo.cloud nfs

[root@php ~ 11:42:10]# mkdir /www
[root@php ~ 11:42:22]# mount /www
[root@php ~ 11:42:26]# df -h /www
Filesystem           Size  Used Avail Use% Mounted on
nfs.demo.cloud:/www   50G  1.7G   49G   4% /www
[root@php ~ 11:42:33]# ls /www
index.html  index.php  info.php  test-mysql.php  wordpress

# php 程序测试
[root@php ~ 11:42:37]# php /www/index.php 
<h1>Hello World !</h1>
[root@php ~ 11:42:55]# php /www/test-mysql.php 
<h1>Connect Mysql Success !</h1>

#windows配置ip地址解析
#C:\Windows\System32\drivers\etc\hosts

10.1.8.21 www.demo.cloud

#测试www.demo.cloud/wordpress

#登陆后注意连接db.demo.cloud

ww 50G 1.7G 49G 4% /www

root@php \~ 11:42:33\]# ls /www index.html index.php info.php test-mysql.php wordpress ## php 程序测试 \[root@php \~ 11:42:37\]# php /www/index.php ## Hello World ! \[root@php \~ 11:42:55\]# php /www/test-mysql.php ## Connect Mysql Success ! #windows配置ip地址解析 #C:\\Windows\\System32\\drivers\\etc\\hosts 10.1.8.21 www.demo.cloud #测试www.demo.cloud/wordpress #登陆后注意连接db.demo.cloud ``` ```

相关推荐
SweerItTer43 分钟前
RK3566 泰山派 IMX415驱动移植+设备树修改+iq文件复制
linux·csdn·泰山派·imx415·rk356x·驱动移植
i***t91943 分钟前
Nginx 之Rewrite 使用详解
运维·nginx
Bigan(安)44 分钟前
【奶茶Beta专项】【LVGL9.4源码分析】03-显示框架-图层管理
linux·c语言·mcu·arm·unix
代码游侠1 小时前
数据结构——线性表
linux·c语言·数据结构·学习·算法
c***87191 小时前
Nginx 缓存清理
运维·nginx·缓存
蒋士峰DBA修行之路1 小时前
红帽练习环境介绍
linux·开发语言·bash
。TAT。1 小时前
进程间通信-对匿名管道的学习
linux·学习
执笔论英雄1 小时前
【RL】async原理
java·服务器·前端
2301_807583231 小时前
Linux-虚拟化技术概述及KVM虚拟机环境部署
linux·运维·服务器