LAMP/LNMP示例
部署 Mysql 服务
# 登录数据库[root@server html 14:40:10]# mysql -u root -p123#创建一个叫wordpress的数据库MariaDB [(none)]> create database wordpress;#创建一个数据库用户(相当于给仓库雇一个管理员,用户名wordpress,密码123)# %表示这个管理员可以从任何地方(比如你的电脑、手机)访问仓库MariaDB [(none)]> create user wordpress@'%' identified by '123';#给这个用户授权MariaDB [(none)]> grant all privileges on wordpress.* to wordpress@'%';#刷新权限MariaDB [(none)]> flush privileges;MariaDB [(none)]> quit
部署 Web 服务(Nginx):雇 “前台接待员”
Nginx 是前台接待员,负责直接面对用户,展示静态内容(如固定菜单)
#安装Nginx(招聘前台)[root@server ~ 14:11:07]# yum install -y nginx# 创建静态页面(写一个欢迎牌:“hello from Nginx”)[root@server ~ 14:11:07]# echo hello from Nginx > /usr/share/nginx/html/index.html [root@server ~ 14:11:50]# systemctl enable nginx --now[root@server ~ 14:11:50]# curl http://10.1.8.10hello from Nginx
部署 PHP 服务:雇 “后台厨师”
PHP 是动态厨师,负责处理需要 “现做” 的内容(如根据用户需求生成个性化页面);php-fpm 是厨师的助手,帮他高效处理订单
# 安装PHP和php-fpm(相当于雇一个"后台厨师",负责处理需要"现做"的食物)# PHP是动态网页的"厨师",能根据请求生成不同内容;php-fpm是厨师的"助手",帮他高效工作[root@server ~ 14:12:43]# yum install -y php php-mysqlnd php-fpm#启动[root@server ~ 14:17:35]# systemctl status php-fpm.service [root@server ~ 14:17:35]# systemctl enable php-fpm.service --now[root@server ~ 14:17:35]# systemctl status php-fpm.service #创建配置文件,告诉前台(Nginx):遇到.php结尾的请求,交给后台厨师(PHP)处理[root@server ~ 14:18:02]# vim /etc/nginx/conf.d/php.conflocation ~ \.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;}# 配置内容的意思:# 1. 收到.php文件的请求时,先检查文件是否存在# 2. 存在的话,转发给127.0.0.1:9000(厨师的工作台)处理# 3. 厨师处理完后,前台再把结果返回给用户# 把配置文件放到Nginx能识别的目录(相当于把"配合规则"贴在前台和厨师都能看到的地方)#php.conf 改default.d来执行[root@server ~ 14:23:14]# mv /etc/nginx/conf.d/php.conf /etc/nginx/default.d/[root@server ~ 14:24:11]# systemctl restart nginx.service #进入Nginx的网页目录[root@server ~ 14:24:19]# cd /usr/share/nginx/html/[root@server html 14:24:53]# ls404.html 50x.html en-US icons img index.html nginx-logo.png poweredby.png[root@server html 14:24:54]# pwd/usr/share/nginx/html#创建index.php(简单的动态"小菜":输出"Hello World")[root@server html 14:25:04]# cat > index.php <<EOF> <?php> echo "<h1>Hello World !</h1>\n";> ?>> EOF#创建test-mysql.php[root@server html 14:41:16]# cat > test-mysql.php <<'EOF'> <?php> ##连接数据库:主机10.1.8.10,用户名wordpress,密码123> $link = mysqli_connect('10.1.8.10', 'wordpress', '123');> if ($link) {> echo "<h1>Connect Mysql Success !</h1>\n";> $link->close();> } else {> echo "<h1>Connect Mysql Failed !</h1>\n";> echo "Error: " . mysqli_connect_error() . "\n";> }> ?>> EOF#创建info.php[root@server html 14:31:04]# cat > info.php <<EOF> <?php> phpinfo()> ?>> EOF[root@server html 14:31:27]# php -f index.php<h1>Hello World !</h1>[root@server html 14:41:27]# php -f test-mysql.php<h1>Connect Mysql Success !</h1>#浏览器输入网址http://10.1.8.10/index.phphttp://10.1.8.10/test-mysql.phphttp://10.1.8.10/info.php3]# chown -R nginx:nginx /usr/share/nginx/html/wordpress-4.9.4-zh_CN.zip
部署 wordpress 应用
WordPress 是一套现成的 “动态菜单系统”,需要把它放到网站目录,让前台和厨师配合运行
# 进入网站目录[root@server ~ 15:03:09]# cd /usr/share/nginx/html/[root@server html 14:41:31]# rz -Erz waiting to receive.[root@server html 15:03:49]# ls...... wordpress-4.9.4-zh_CN.zip[root@server html 15:03:53]# unzip wordpress-4.9.4-zh_CN.zip#授权:让前台(nginx)和厨师(php-fpm)能访问WordPress文件(给食材区开锁)[root@server html 15:05:05]# chown -R nginx:nginx /usr/share/nginx/html/wordpress# 配置厨师的工作身份(让厨师以nginx用户工作,避免权限问题)[root@server html 15:05:20]# vim /etc/php-fpm.d/www.conf ; RPM: apache Choosed to be able to access some dir as httpduser = nginx #apche改为nginx; RPM: Keep a group allowed to write in log dir.group = nginx #apche改为nginx[root@server html 15:06:57]# systemctl restart php-fpm.service #浏览器进入10.1.8.10/wordpress/index.phpserver {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;index index.php;#####添加这一行# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;#添加后浏览器不用再输入index.php
数据库名:wordpress用户名: wordpress密码: 123数据库主机:10.1.8.10表前缀: wp_
多服务器部署:从 “小店” 到 “连锁餐厅”
管理快照,选择client,链接克隆,创建www,php,db,storage四台虚拟机
www.lyk.cloud
:前台服务器(Nginx)php.lyk.cloud
:厨房服务器(PHP)db.lyk.cloud
:仓库服务器(MySQL)storage.lyk.cloud
:食材库服务器(NFS 共享存储)
# 给每台服务器设置“店名”(主机名)和“地址”(IP)# 前台服务器(www)hostnamectl set-hostname www.lyk.cloudnmcli connection modify ens33 ipv4.addresses 10.1.8.21/24baship -br a# 厨房服务器(php)hostnamectl set-hostname php.lyk.cloudnmcli connection modify ens33 ipv4.addresses 10.1.8.22/24baship -br a# 仓库服务器(db)hostnamectl set-hostname db.lyk.cloudnmcli connection modify ens33 ipv4.addresses 10.1.8.23/24baship -br a# 食材库服务器(storage)hostnamectl set-hostname storage.lyk.cloudnmcli connection modify ens33 ipv4.addresses 10.1.8.24/24baship -br a
# 同步地址簿(让所有服务器互相认识对方的店名和地址)[root@storage ~ 16:16:37]# yum install -y nfs-utils[root@storage ~ 16:16:37]# vim /etc/hosts[root@storage ~ 16:22:02]# scp /etc/hosts www:/etc/hostsWarning: Permanently added 'www,10.1.8.21' (ECDSA) to the list of known hosts.hosts 100% 277 462.0KB/s 00:00 [root@storage ~ 16:22:12]# scp /etc/hosts php:/etc/hostsWarning: Permanently added 'php,10.1.8.22' (ECDSA) to the list of known hosts.hosts 100% 277 239.7KB/s 00:00 [root@storage ~ 16:22:25]# scp /etc/hosts db:/etc/hostsWarning: Permanently added 'db,10.1.8.23' (ECDSA) to the list of known hosts.hosts 100% 277 127.3KB/s 00
部署存储服务器
# 安装NFS工具[root@storage ~ 16:23:39]# yum install -y nfs-utils# 创建共享目录(食材存放区)并设置权限(允许读写)[root@storage ~ 16:25:37]# mkdir -m 777 /www# 配置共享规则:允许10.1.8.0/24网段的服务器读写/www目录[root@storage ~ 16:25:44]# echo '/www 10.1.8.0/24(rw)' > /etc/exports[root@storage ~ 16:25:53]# systemctl enable nfs-server.service --now[root@storage ~ 16:26:51]# rz -E[root@storage ~ 16:26:51]# unzip -o wordpress-4.9.4-zh_CN.zip -d /www/# 创建测试文件(验证共享是否正常)[root@storage ~ 16:26:51]# echo 'Hello World !' > /www/index.html[root@storage ~ 16:27:12]# cat > /www/index.php <<EOF> <?php> echo "<h1>Hello World !</h1>\n";> ?>> EOF[root@storage ~ 16:27:34]# cat > /www/test-mysql.php <<'EOF'> <?php> $link=mysqli_connect('db.lyk.cloud','wordpress','123');> if($link)> echo "<h1>Connect Mysql Success !</h1>\n";> else> echo "<h1>Connect Mysql Failed !</h1>\n";> $link->close();> ?>> EOF[root@storage ~ 16:28:22]# cat > /www/info.php <<EOF> <?php> phpinfo();> ?>> EOF
部署数据库服务器
把 MySQL 单独部署,专注存储数据,更安全可靠。
# 安装MariaDB(仓库软件)[root@db ~ 16:34:19]# yum install -y mariadb-server[root@db ~ 16:35:04]# systemctl enable mariadb --now# 安全配置(给仓库上锁:设置root密码、删除匿名用户等)[root@db ~ 16:36:38]# mysql_secure_installation # 登录数据库,创建WordPress专用仓库和管理员(和单服务器步骤一致)[root@db ~ 16:36:38]# mysql -uroot -p123MariaDB [(none)]> CREATE DATABASE wordpress;MariaDB [(none)]> CREATE USER wordpress@'%' identified by '123';MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%';MariaDB [(none)]> FLUSH PRIVILEGES;MariaDB [(none)]> exit
部署 Nginx 服务器
# 安装Nginx(前台软件)[root@www ~ 16:39:45]# yum install -y nginx[root@www ~ 16:51:32]# systemctl enable nginx --now# 安装NFS工具(前台需要访问食材库的工具)[root@www ~ 16:51:52]# yum install -y nfs-utils[root@www ~ 16:52:09]# echo 'storage.lyk.cloud:/www /usr/share/nginx/html nfs defaults 0 0' >> /etc/fstab # 手动挂载测试(立即让前台货架连上食材库)[root@www ~ 16:56:55]# mount /usr/share/nginx/html/[root@www ~ 17:00:52]# df -h /usr/share/nginx/html/文件系统 容量 已用 可用 已用% 挂载点storage.lyk.cloud:/www 50G 2.2G 48G 5% /usr/share/nginx/html[root@www ~ 17:00:58]# ls /usr/share/nginx/html/index.html index.php info.php test-mysql.php wordpress
部署 PHP 服务器
# 安装PHP、php-fpm和MySQL连接工具(厨师、助手和仓库沟通工具)[root@php ~ 16:41:07]# yum install -y php php-fpm php-mysqlnd# 配置厨师的工作台(让前台能访问厨房)[root@php ~ 17:30:43]# vim /etc/php-fpm.d/www.conf ;listen = 127.0.0.1:9000#;注释listen = 9000#添加;listen.allowed_clients = 127.0.0.1#;注释[root@php ~ 17:34:29]# systemctl enable php-fpm.service --now[root@php ~ 17:40:55]# systemctl restart php-fpm# 挂载食材库的共享目录(厨师从共享食材库拿食材)[root@php ~ 17:02:23]# echo 'storage.lyk.cloud:/www /www nfs defaults 0 0' >> /etc/fstab [root@php ~ 17:02:25]# mkdir /www#挂载失败[root@php ~ 17:02:44]# mount -t /www# 安装NFS工具(访问食材库的工具)[root@php ~ 17:06:04]# yum install -y nfs-utils# 查看食材库共享了什么(输出:/www 10.1.8.0/24)[root@php ~ 17:06:47]# showmount -e storage.lyk.cloudExport list for storage.lyk.cloud:/www 10.1.8.0/24# 手动挂载[root@php ~ 17:07:12]# mount -t nfs storage.lyk.cloud:/www /www[root@php ~ 17:07:22]# df -h /www文件系统 容量 已用 可用 已用% 挂载点storage.lyk.cloud:/www 50G 2.2G 48G 5% /www[root@php ~ 17:08:12]# ls /wwwindex.html index.php info.php test-mysql.php wordpress#php 程序测试[root@php ~ 17:08:24]# php /www/index.php<h1>Hello World !</h1>[root@php ~ 17:08:37]# php /www/test-mysql.php<h1>Connect Mysql Success !</h1>
配置 Nginx 对接 PHP
# 创建Nginx配置文件(前台与厨房的配合手册)[root@www ~ 17:10:25]# cat > /etc/nginx/conf.d/vhost-www.conf <<'EOF'> server {> listen 80; # 监听80端口(用户访问的默认端口)> server_name www.lyk.cloud;> > # 静态资源处理> location / {> root /usr/share/nginx/html;> index index.html index.htm index.php;> }> > # PHP 请求处理 # PHP动态请求处理(需要厨师现做的内容)> location ~ \.php$ {> # 配置 PHP-FPM 监听的地址和端口> fastcgi_pass php.lyk.cloud:9000; # 转发给厨房服务器的9000端口> fastcgi_index index.php;> # 配置 php 服务器上 wordpress 应用所在位置> fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;> include fastcgi_params;> }> }> EOF[root@www ~ 17:11:03]# systemctl restart nginx
配置存储权限
# 在厨房服务器创建nginx用户(和前台用户一致,避免权限冲突)[root@php ~ 17:09:58]# useradd -u 997 -s /sbin/nologin nginx# 配置厨师以nginx用户工作(用统一身份访问食材)[root@php ~ 17:14:39]# vim /etc/php-fpm.d/www.conf ; RPM: apache Choosed to be able to access some dir as httpduser = nginx; RPM: Keep a group allowed to write in log dir.group = nginx[root@php ~ 17:15:45]# systemctl restart php-fpm