04.LNMP网站架构
- 1.LNMP架构概述
- 2.安装LNMP架构
- 2.配置LNMP架构
- 4.部署博客产品Wordpress
- 5.部署知乎产品Wecenter
- 6.部署网校产品Edusohu
- 7.迁移数据至独立服务器
- 8.迁移图片至独立服务器
- 9.扩展相同的Web服务器
徐亮伟, 江湖人称标杆徐。多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作。
个人博客"徐亮伟架构师之路"累计受益数万人。老男孩Linux云计算运维QQ交流群: 384467551 226199307
老男孩教育官网 http://www.oldboyedu.com
1.LNMP架构概述
LNMP
就是Linux+Nginx+MySQL+PHP
,Linux
作为服务器的操作系统,Nginx
作为Web服务器、PHP
作为解析动态脚本语言、MySQL
即为数据库。
Linux作为服务器的操作系统。
Nginx作为WebServer服务器。
PHP 作为动态解析服务(php)。
MySQL作为后端存储数据库服务。
Nginx
服务本身不能处理PHP
的请求,那么当用户发起PHP动态请求, Nginx又是如何进行处理的。
用户-->http
协议-->Nginx
-->fastcgi
协议-->php-fpm
注意: fatcgi
是nginx
连接php-fpm
之间的协议。
Nginx
与Fast-CGI
详细工作流程如下:
1.用户通过
http
协议发起请求,请求会先抵达LNMP
架构中的Nginx
2.Nginx
会根据用户的请求进行判断,这个判断是有Location
进行完成
3.判断用户请求的是静态页面,Nginx
直接进行处理
4.判断用户请求的是动态页面,Nginx
会将该请求交给fastcgi
协议下发
5.fastgi
会将请求交给php-fpm
管理进程,php-fpm
管理进程接收到后会调用具体的工作线程warrap
6.warrap
线程会调用php
进行解析,如果只是解析代码php
直接返回
7.如果有查询数据库操作,则由php
连接数据库(用户 密码 IP)然后发起查询的操作
8.最终数据由mysql->php->php-fpm->fastcgi->nginx->http->user
2.安装LNMP架构
1) 使用官方仓库安装Nginx
[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2) 安装Nginx
并启动Nginx
加入开机自启
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
3) 使用第三方扩展源安装php7.1
[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common
[root@nginx ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@nginx ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
3) 配置php-fpm
启动php-fpm
并加入开机自启
[root@nginx ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@nginx ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
[root@nginx ~]# systemctl start php-fpm
[root@nginx ~]# systemctl enable php-fpm
4) 使用MySQL
官方仓库安装MySQL5.7
数据库
[root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
[root@nginx ~]# yum install mysql-community-server -y
5) 启动数据库, 并加入开机自动
[root@nginx ~]# systemctl start mysqld
[root@nginx ~]# systemctl enable mysqld
6) mysql5.7默认配置了密码, 需要提取temporary password中的密码, 登陆对应数据库
[root@nginx ~]# grep "temporary password" /var/log/mysqld.log
[root@nginx ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
7) 重新初始化 MySQL 的密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bgx123.com';
2.配置LNMP架构
1) 为了验证Nginx
能否正常解析php
动态请求, 需配置如下locaiton
[root@nginx ~]# cat /etc/nginx/conf.d/php.conf
server {
server_name www.bgx.com;
listen 80;
root /code;
index index.php index.html;
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2) 新增.php后缀的文件名, 使用phpinfo函数测试php能否正常解析
[root@nginx ~]# cat /code/info.php
<?php
phpinfo();
?>
3) 验证php
是否能正常连接mysql
数据库服务
[root@nginx ~]# cat /code/mysqli.php
<?php
$servername = "localhost";
$username = "root";
$password = "Bgx123.com";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php连接MySQL数据库成功";
?>
4) 通过浏览器访问info.php
文件, 如能出现php
相关的信息, 则表示nginx
与php
能正常工作
5) 访问mysqli.php
验证php-mysqli
模块是否正常工作
4.部署博客产品Wordpress
1) 配置Nginx
虚拟主机站点,域名为blog.bgx.com
#1.nginx具体配置信息
[root@nginx ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name blog.bgx.com;
root /code/wordpress;
index index.php index.html;
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2) 重启nginx服务
[root@nginx ~]# systemctl restart nginx
3) 获取wordpress
产品,解压并部署wordress
[root@nginx ~]# mkdir /code
[root@nginx ~]# cd /code
[root@nginx code]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@nginx ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@nginx ~]# chown -R www.www /code/wordpress/
4) 由于wordpress
产品需要依赖数据库, 所以需要手动建立数据库
[root@nginx ~]# mysql -uroot -pBgx123.com
mysql> create database wordpress;
mysql> exit
5) 通过浏览器访问wordpress, 并部署该产品
5.部署知乎产品Wecenter
1.配置Nginx
虚拟主机站点,域名为zh.bgx.com
#1.nginx具体配置信息
[root@http-server ~]# cat /etc/nginx/conf.d/zh.conf
server {
listen 80;
server_name zh.bgx.com;
root /code/zh;
index index.php index.html;
location ~ \.php$ {
root /code/zh;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#2.重启nginx服务
[root@http-server ~]# systemctl restart nginx
2.下载Wecenter
产品,部署Wecenter
并授权
[root@web02 ~]# wget http://ahdx.down.chinaz.com/201605/WeCenter_v3.2.1.zip
[root@web02 ~]# unzip WeCenter_v3.1.9.zip
[root@web02 ~]# mv UPLOAD/ /code/zh
[root@web02 ~]# chown -R www.www /code/zh/
3.由于wecenter
产品需要依赖数据库, 所以需要手动建立数据库
#1.登陆数据库
[root@http-server ~]# mysql -uroot -pBgx123.com
#2.创建wordpress数据库
MariaDB [(none)]> create database zh;
MariaDB [(none)]> exit
3.通过浏览器访问网站
6.部署网校产品Edusohu
1.配置Nginx
虚拟主机站点,域名为edu.bgx.com
#1.nginx具体配置信息
[root@http-server ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name edu.bgx.com;
root /code/edu;
index index.php index.html;
location ~ \.php$ {
root /code/edu;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#2.重启nginx服务
[root@http-server ~]# systemctl restart nginx
2.下载edusohu
产品,部署edusohu
并授权
//获取wordpress代码
[root@http-server ~]# cd /soft/src/
[root@http-server /soft/src]# wget http://download.edusoho.com/edusoho-8.2.17.tar.gz
//解压软件网站源码文件, 并授权站点目录,不然会导致无法安装
[root@http-server /soft/src]# tar xf edusoho-8.2.17.tar.gz
[root@http-server /soft/src]# cp -r edusoho /code/edu
[root@http-server ~]# chown -R www.www /code/edu/
[root@http-server ~]# chmod -R 777 /code/edu/{app,web}
//由于edusohu会自动创建数据库, 所以无需创建数据库
3.通过浏览器访问网站
7.迁移数据至独立服务器
拆分LNMP
的数据库到独立的数据库服务器步骤
1.老服务器操作
1) 指定导出对应的数据库文件(Bgx123.com是数据库密码)
[root@web02 ~]# mysqldump -uroot -p'Bgx123.com' --all-databases --single-transaction > `date +%F%H`-mysql-all.sql
2) 拷贝备份数据库文件至新的数据库服务器上
[root@web02 zh]# scp 2018-08-0909-mysql-all.sql root@10.0.0.51:~
2.新服务器操作
1) 导入旧的数据库至新的数据库中
[root@db01 ~]# mysql -uroot -p'Bgx123.com' < 2018-08-0909-mysql-all.sql
2) 登录新的数据库, 并检查数据库已被导入成功
[root@db01 ~]# mysql -uroot -pBgx123.com
mysql> show databases;
3) 在新数据库上授权, 允许所有网段, 通过all账户连接数据库
#授权所有权限 grant all privileges
#授权所有库所有表 *.*
#将授权赋予给哪个用户,这个用户只能通过哪个网段过来(%所有) 'all'@'%'
#授权该用户登录的密码 identified by
mysql> grant all on *.* to all@'%' identified by 'Bgx123.com';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4) 修改Wordpress
产品代码连接数据库的配置文件
[root@web01 ~]# vim /code/wordpress/wp-config.php
# 数据库名称
define('DB_NAME', 'wordpress');
# 数据库用户
define('DB_USER', 'all');
# 数据库密码
define('DB_PASSWORD', 'Bgx123.com');
# 数据库地址
define('DB_HOST', '172.16.1.51');
5) 修改wecenter
产品代码连接数据库的配置文件
[root@web01 zh]# grep -iR "Bgx123.com"|grep -v cache
system/config/database.php: 'password' => 'Bgx123.com',
[root@web01 zh]# vim /code/zh/system/config/database.php
'host' => '172.16.1.51',
'username' => 'all',
'password' => 'Bgx123.com',
'dbname' => 'zh',
6) 修改edusoho
产品代码连接数据库的配置文件
[root@web01 edu]# grep -iR "Bgx123.com"|grep -v cache
app/config/parameters.yml: database_password: 'Bgx123.com'
[root@web01 edu]# vim /code/edu/app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 172.16.1.51
database_port: 3306
database_name: edu
database_user: all
database_password: 'Bgx123.com'
#必须清理缓存
[root@web01 edu]# rm -rf /code/edu/app/cache/*
8.迁移图片至独立服务器
1.nfs-server服务端操作
1) 配置nfs共享的目录
[root@nfs01 ~]# cat /etc/exports
/data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/edu 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
2) 创建对应共享的目录
[root@nfs01 ~]# mkdir /data/{blog,edu,zh} -p
[root@nfs01 ~]# chown -R www.www /data/
[root@nfs01 ~]# systemctl restart nfs-server
2.web01端操作
1) WEB客户端验证NFS是否安装成功
[root@web01 ~]# yum install nfs-utils -y
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zh 172.16.1.0/24
/data/edu 172.16.1.0/24
/data/blog 172.16.1.0/24
2) 获取Wordpress产品的附件和图片存放的位置
浏览器->右键->检查->Network->选择按钮->点击一下图片
3) 备份web服务器上的Wordpress图片和附件
[root@web01 wp-content]# cd /code/wordpress/wp-content
[root@web01 wp-content]# cp uploads/ uploads_bak/
4) 客户端执行挂载操作[Wordpress]
[root@web01 wp-content]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/
[root@web01 wp-content]# cp -rp uploads_bak/* uploads/
5) 将挂载信息加入开机自启
[root@web01 wp-content]# tail -1 /etc/fstab
172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0
[root@web01 wp-content]# mount -a
3.web02端操作
1) 客户端挂载nfs服务端存储
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/
2) 将挂载信息加入开机自启
[root@web02 ~]# tail -1 /etc/fstab
172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0
[root@web02 wp-content]# mount -a
9.扩展相同的Web服务器
快速的扩展一台web服务器10.0.0.9
, 数据库使用10.0.0.51
,图片与附件使用10.0.0.31
1) 创建www用户
[root@web03 ~]# groupadd -g666 www
[root@web03 ~]# useradd -u666 -g666 www
2) 安装LNP
[root@web03 ~]# scp -rp root@172.16.1.7:/etc/yum.repos.d/* /etc/yum.repos.d/
[root@web03 ~]# scp -rp root@172.16.1.7:/etc/pki/rpm-gpg/* /etc/pki/rpm-gpg/
[root@web03 ~]# yum install nginx -y
[root@web03 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
3) 将web01的nginx配置文件导入到web03
[root@web03 ~]# scp -rp root@172.16.1.7:/etc/nginx/* /etc/nginx/
4) 将web01的php配置文件导入到web03
[root@web03 ~]# scp -rp root@172.16.1.7:/etc/php-fpm.d/* /etc/php-fpm.d/
5) 将web01的产品代码导到web03,在web1上线进行打包操作
[root@web01 ~]# tar czf code.tar.gz /oldboy_code4/
#在web3上面拉取web1打包好的内容
[root@web03 ~]# scp root@172.16.1.7:/root/code.tar.gz ~
#在web3上面解压即可
[root@web03 ~]# tar xf code.tar.gz -C /
6) 启动nginx
与php-fpm
服务
[root@web03 ~]# systemctl start nginx php-fpm
[root@web03 ~]# systemctl enable nginx php-fpm
7) 在web03
上进行挂载
[root@web03 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content
# 所有的挂载都应该加入开机自启动