赞
踩
1.LNMP架构概述
2.安装LNMP架构
3.检测LNMP架构
4.部署博客产品 Wordpress
5.部署知乎产品 Wecenter
6.部署网校产品EduSoho
7.迁移数据至独立服务器
8.迁移图片至独立服务器
9.扩展相同的Web服务器
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 之间的协议。
1.浏览器输入域名,浏览器会拿着域名取DNS服务器解析 2.DNS服务器会将域名解析成IP 3.浏览器会去与IP对应服务器建立TCP\IP连接 4.连接建立完成,会向服务器发起请求,请求nginx 5.nginx会判断请求是动态的还是静态的 #静态请求 location \.jpg$ { root /code; }#动态php请求 location \.php$ { fastcgi_pass 127.0.0.1:9000; ...... }#动态jsp请求 location \.jsp$ { proxy_pass 127.0.0.1:8080; ...... } 6.如果是静态请求,nginx去code目录获取,直接返回 7.如果是动态请求,nginx会通过fastcgi协议连接PHP服务的php-fpm管理进程 8.php-fpm管理进程会下发工作给 wrapper工作进程 9.wrapper工作进程判断是不是简单的php内容 10.如果只是php内容则使用php解析器解析后直接返回 11.如果还需要读取数据库,wrapper工作进程会去数据库读取数据,再返回数据 12.数据流转过程: 1)请求:浏览器 > 负载均衡 > nginx > php-fpm > wrapper > mysql 2)响应:mysql > wrapper > php-fpm > nginx > 负载均衡 > 浏览器
yum安装 nginx1.14 php7.1 mysql5.7
#1.使用Nginx官方提供的rpm包 [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.执行yum安装 [root@nginx ~]# yum install nginx -y #3.修改程序用户 [root@nginx ~]# groupadd www -g 666 [root@nginx ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M #修改nginx配置文件 [root@nginx ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf #4.启动并加入开机自启动 [root@nginx ~]# systemctl start nginx [root@nginx ~]# systemctl enable nginx
#1.移除旧版php [root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common -y #2.安装扩展源 # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm #配置第三方源 [root@nginx ~]# vim /etc/yum.repos.d/php.repo [php-webtatic] name = PHP Repository baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/ gpgcheck = 0 #3.安装php7.1版本 [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 #4.替换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 #5.启动php-fpm管理进程, 并加入开机自启 [root@nginx ~]# systemctl start php-fpm [root@nginx ~]# systemctl enable php-fpm
#1.下载MySQL官方扩展源 [root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7- community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm #2.安装mysql5.7, 文件过大可能会导致下载缓慢 [root@nginx ~]# yum install mysql-community-server -y #3.启动数据库, 并加入开机自启动 [root@nginx ~]# systemctl start mysqld [root@nginx ~]# systemctl enable mysqld #4.由于mysql5.7默认配置了默认密码, 需要过滤temporary password关键字查看对应登陆数据库密码 [root@nginx ~]# grep "temporary password" /var/log/mysqld.log #5.登陆mysql数据库[password中填写上一步过滤的密码] [root@nginx ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log) #6.重新修改数据库密码 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '数据库密码'; # 或直接用命令修改 mysqladmin -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log) password “xxxx'
#1.下载MySQL官方扩展源 [root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6- community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm #2.安装mysql5.6, 文件过大可能会导致下载缓慢 [root@nginx ~]# yum install mysql-community-server -y #3.启动数据库, 并加入开机自启动 [root@nginx ~]# systemctl start mysqld [root@nginx ~]# systemctl enable mysqld #4.由于mysql5.6默认管理员root的密码为空,服务器启动后,可以直接登录 [root@web01 ~]# mysql -uroot …… mysql> #5.为了安全,必须给root设置密码 [root@nginx ~]# mysqladmin -u root password 数据库密码 #6.使用密码登陆mysql [root@web01 ~]# mysql -uroot -p数据库密码 …… mysql>
在将Nginx与PHP集成过程中,需要先了解Fastcgi代理配置语法
Syntax: fastcgi_pass address;
Default: —
Context: location, if in location
#语法示例
fastcgi_pass localhost:9000;
fastcgi_pass unix:/tmp/fastcgi.socket;
Syntax: fastcgi_index name;
Default: —
Context: http, server, location
Syntax: fastcgi_param parameter value [if_not_empty];
Default: —
Context: http, server, location
#语法示例
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /code$fastcgi_script_name;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XCFPsPQM-1666542593758)(…/…/图片/image-20221011203900243.png)]
[root@nginx ~]# cat /etc/nginx/conf.d/php.conf
server {
server_name www.tf.com;
listen 80;
root /code/www;
index index.php index.html;
location ~ \.php$ {
root /code/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@nginx ~]# cat /code/www/info.php
<?php
phpinfo();
?>
[root@nginx ~]# cat /code/www/mysqli.php
<?php
$servername = "localhost";
$username = "root";
$password = "数据库密码";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>
1.通过浏览器访问info.php文件。如果出现连接成功表示nginx和php能正常工作
2.访问mysql.php验证php-mysqli模块是否正常工作
3.总结LNMP架构访问流程
4.实现上传大文件。需要如下配置
1.修改nginx的配置文件
# vim /etc/nginx/nginx.conf
http {
#nginx默认请求文件大小为1M,也可以写在server{}或location{}中
client_max_body_size 10m;
}2.修改php的配置文件
vim /etc/php.ini
#文件上传功能,默认开启
file_uploads = On
#上传最大文件大小,默认2M
upload_max_filesize = 200M
#文件最大尺寸,默认2M
post_max_size = 200M
#一个请求可以上传的最大文件数 ,默认20
max_file_uploads = 20
#1.nginx具体配置信息 [root@nginx ~]# cat /etc/nginx/conf.d/wordpress.conf server { listen 80; server_name blog.tf.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; } }
[root@nginx ~]# systemctl restart nginx
#1.获取wordpress代码
[root@nginx code]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz
#永远不要下载最新版
[root@nginx code]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
#2.解压网站源码文件,拷贝至对应站点目录,并授权站点目录
[root@nginx ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@nginx ~]# cp -r wordpress /code/
[root@nginx ~]# chown -R www.www /code/wordpress/
#1.登陆数据库
[root@nginx ~]# mysql -uroot -p数据库密码
#2.创建wordpress数据库
mysql> create database wordpress;
mysql> exit
#1.nginx具体配置信息 [root@nginx ~]# cat /etc/nginx/conf.d/zh.conf server { listen 80; server_name zh.tf.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@nginx ~]# systemctl restart nginx
[root@nginx ~]# wget http://ahdx.down.chinaz.com/201605/WeCenter_v3.2.1.zip
[root@nginx ~]# unzip WeCenter_3-2-1.zip
[root@nginx ~]# mv WeCenter_3-2-1/ /code/zh
[root@nginx ~]# chown -R www.www /code/zh/
#1.登陆数据库
[root@nginx ~]# mysql -uroot -p数据库密码
#2.创建wordpress数据库
mysql> create database zh;
mysql> exit
#1.nginx具体配置信息 [root@nginx ~]# cat /etc/nginx/conf.d/edu.conf server { listen 80; server_name edu.zmj.com; root /code/edu/web; location / { index app.php; try_files $uri @rewriteapp; } location @rewriteapp { rewrite ^(.*)$ /app.php/$1 last; } location ~ ^/udisk { internal; root /code/edu/app/data/; } location ~ ^/(app|app_dev)\.php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect; fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edu/app/data/udisk; fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; } # 配置设置图片格式文件 location ~* \.(jpg|jpeg|gif|png|ico|swf)$ { # 过期时间为3年 expires 3y; # 关闭日志记录 access_log off; # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。 gzip off; } # 配置css/js文件 location ~* \.(css|js)$ { access_log off; expires 3y; } # 禁止用户上传目录下所有.php文件的访问,提高安全性 location ~ ^/files/.*\.(php|php5)$ { deny all; } # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; } } #2.重启nginx服务 [root@nginx ~]# systemctl restart nginx
//获取edusoho代码
[root@nginx ~]# cd /soft/
[root@nginx soft]# wget http://download.edusoho.com/edusoho-8.2.17.tar.gz
//解压软件网站源码文件, 并授权站点目录,不然会导致无法安装
[root@nginx soft]# tar xf edusoho-8.2.17.tar.gz
[root@nginx soft]# mv edusoho /code/edu
[root@nginx soft]# chown -R www.www /code/edu/
[root@nginx soft]# chmod -R 777 /code/edu/{app,web}
//由于edusohu会自动创建数据库, 所以无需创建数据库
为什么要进行数据库的拆分
由于单台服务器允许LNMP架构会导致网站访问缓慢,当内存被占满时,很容易导致系统出现oom,自动关闭(kill)MySQL数据库,所以要讲web和数据库进行独立部署。
数据库拆分后解决了什么问题
1.缓解web网站的压力
2.增强数据库读写性能
3.提高用户访问速度
指定导出对应的数据库文件。
[root@web01 ~]# mysqldump -uroot -p数据库密码 -A > $(date +%F)-mysql-all.sql
拷贝备份数据库文件至新的数据库服务器上
[root@web01 zh]# scp 2018-08-09-mysql-all.sql root@10.0.0.51:~
导入数据库
[root@db01 ~]# mysql -uroot -p数据库密码 < 2018-08-09-mysql-all.sql
登录数据库
[root@db01 ~]# mysql -uroot -p数据库密码
检查数据库是否所有的库都被成功导入
mysql> show databases;
在新数据库上授权, 允许所有网段, 通过webadm账户连接数据库
#授权所有权限 grant all privileges
#授权所有库所有表 *.*
#将授权赋予给哪个用户,这个用户只能通过哪个网段过来(%所有) 'webadm'@'%'
#授权该用户登录的密码 identified by
mysql> grant all on *.* to webadm@'%' identified by '数据库密码';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
停止web本地数据库
[root@web01 ~]# systemctl stop mysqld
[root@web01 ~]# systemctl disable mysqld
修改Wordpress产品代码连接数据库的配置文件
[root@web01 ~]# vim /code/wordpress/wp-config.php
# 数据库名称
define('DB_NAME', 'wordpress');
# 数据库用户
define('DB_USER', 'webadm');
# 数据库密码
define('DB_PASSWORD', '数据库密码');
# 数据库地址
define('DB_HOST', '10.0.0.51');
修改wecenter产品代码连接数据库的配置文件
[root@web01 zh]# grep -iR "123.com"|grep -v cache
system/config/database.php: 'password' => '123.com',
[root@web01 zh]# vim /code/zh/system/config/database.php
'host' => '10.0.0.51',
'username' => 'webadm',
'password' => 'Tl123.com',
'dbname' => 'zh',
修改edusoho产品代码连接数据库的配置文件
[root@web01 edu]# grep -iR "123.com"|grep -v cache
app/config/parameters.yml: database_password: '123.com'
[root@web01 edu]# vim /code/edu/app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 10.0.0.51
database_port: 3306
database_name: edu
database_user: webadm
database_password: '123.com'
#必须清理缓存
[root@web01 edu]# rm -rf /code/edu/app/cache/*
1. 配置nfs共享的目录
[root@nfs ~]# cat /etc/exports
/data/blog 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/edu 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
2.创建对应共享的目录
[root@nfs ~]# mkdir /data/{blog,edu,zh} -p
[root@nfs ~]# chown -R www.www /data/
[root@nfs ~]# systemctl restart nfs-server
1.WEB客户端验证NFS是否安装成功
[root@web01 ~]# yum install nfs-utils -y
[root@web01 ~]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data/zh 10.0.0.0/24
/data/edu 10.0.0.0/24
/data/blog 10.0.0.0/24
2.获取Wordpress产品的附件和图片存放的位置
浏览器->右键->检查->Network->选择按钮->点击一下图片
3.备份web服务器上的Wordpress图片和附件
[root@web01 ~]# cd /code/wordpress
[root@web01 wordpress]# cp -rp wp-content/ wp-content_bak
4.客户端执行挂载操作[Wordpress]
[root@web01 wordpress]# mount -t nfs 10.0.0.31:/data/blog /code/wordpress/wp-content/
[root@web01 wordpress]# cp -rp uploads_bak/* uploads/
5.将挂载信息加入开机自启
[root@web01 wordpress]# tail -1 /etc/fstab
10.0.0.31:/data/blog /code/wordpress/wp-content nfs defaults 0 0
[root@web01 wordpress]# mount -a
1.挂载nfs存储
[root@web02 ~]# mount -t nfs 10.0.0.31:/data/blog /code/wordpress/wp-content
2.将挂载信息加入开机自启
[root@web02 ~]# tail -1 /etc/fstab
10.0.0.31:/data/blog /code/wordpress/wp-content nfs defaults 0 0
[root@web02 wp-content]# mount -a
快速的扩展一台相同的web服务器, 数据库一模一样,图片、附件都一样, 准备一台新的服务器
10.0.0.9
[root@web03 ~]# groupadd -g666 www
[root@web03 ~]# useradd -u666 -g666 www
[root@web03 ~]# scp -rp root@10.0.0.7:/etc/yum.repos.d/* /etc/yum.repos.d/
[root@web03 ~]# scp -rp root@10.0.0.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
[root@web03 ~]# scp -rp root@10.0.0.7:/etc/nginx/* /etc/nginx/
[root@web03 ~]# scp -rp root@10.0.0.7:/etc/php-fpm.d/* /etc/php-fpm.d/
[root@web01 ~]# tar czf code.tar.gz /code/
#在web3上面拉取web1打包好的内容
[root@web03 ~]# scp root@10.0.0.7:/root/code.tar.gz ~
#在web3上面解压即可
[root@web03 ~]# tar xf code.tar.gz -C /
[root@web03 ~]# systemctl start nginx php-fpm
[root@web03 ~]# systemctl enable nginx php-fpm
[root@web02 ~]# mount -t nfs 10.0.0.31:/data/blog /code/wordpress/wp-content
# 所有的挂载都应该加入开机自启动
odb
### 3.将web01的nginx配置文件导入到web03
[root@web03 ~]# scp -rp root@10.0.0.7:/etc/nginx/* /etc/nginx/
### 4.将web01的php配置文件导入到web03
[root@web03 ~]# scp -rp root@10.0.0.7:/etc/php-fpm.d/* /etc/php-fpm.d/
### 5.将web01的产品代码导到web03,在web1上线进行打包操作
[root@web01 ~]# tar czf code.tar.gz /code/
#在web3上面拉取web1打包好的内容
[root@web03 ~]# scp root@10.0.0.7:/root/code.tar.gz ~
#在web3上面解压即可
[root@web03 ~]# tar xf code.tar.gz -C /
### 6.启动相关的服务
[root@web03 ~]# systemctl start nginx php-fpm
[root@web03 ~]# systemctl enable nginx php-fpm
### 7.在web02上进行挂载
[root@web02 ~]# mount -t nfs 10.0.0.31:/data/blog /code/wordpress/wp-content
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。