1.LNMP架构概述
LNMP
就是Linux+Nginx+MySQL+PHP
,Linux
作为服务器的操作系统,Nginx
作为Web服务器、PHP
作为解析动态脚本语言、MySQL
即为数据库。
Linux作为服务器的操作系统。
Nginx作为WebServer服务器。
PHP 作为动态解析服务(php)。
MySQL作为后端存储数据库服务。
nginx
本身不能处理PHP
,它只是个web
服务器,当接收到请求后,如果是php
请求,则发给php
解释器处理,并把结果返回给客户端。nginx
一般是将请求发送给fastcgi
管理进程,fascgi
管理进程选择cgi
子进程处理并将结果返回给nginx
那什么是fastcgi
用户-->http协议-->Nginx-->fastcgi协议-->php-fpm
fatcgi是nginx连接php-fpm之间的协议。
那Nginx
与Fast-CGI
又是如何进行工作的呢
1.用户请求Nginx,Nginx接收用户请求,进行判断。
2.如果用户请求的是静态内容,则Nginx直接响应并处理。
3.如果用户请求的是动态内容,则通过fastcgi客户端,传递给php-fpm
4.php-fpm接收到请求后,会生成对应的warrap线程,来解析用户请求的动态内容。
5.如果涉及到查询数据库操作,则需要php先连接数据库,然后进行查询操作。(php-mysql)
6.最终由mysql返回数据给php->fastcgi服务端->fastcgi客户端->nginx->用户。
1.安装LNMP架构
yum安装 nginx1.14 php7.1 mysql5.7
1.安装Nginx
#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=0enabled=1#2.执行yum安装[root@nginx ~]# yum install nginx -y#3.启动并加入开机自启动[root@nginx ~]# systemctl start nginx[root@nginx ~]# systemctl enable nginx
2.使用第三方扩展epel
源安装php7.1
#1.移除旧版php[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common#2.安装扩展源[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#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@web02 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf [root@web02 ~]# 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
4.安装MySQL5.7
#1.下载官方扩展源, 扩展源集成mysql5.6、5.7、8.0,仅5.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 #2.安装mysql, 默认5.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@web02 ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log) #6.重新修改数据库密码 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bgx123.com';
2.配置LNMP架构
1.验证Nginx
是否能正常解析php
动态请求
[root@nginx ~]# cat /etc/nginx/conf.d/php.conf server { server_name _; 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.创建info.php测试php是否正常解析[root@nginx ~]# cat /code/info.php<?php phpinfo(); ?>
2.验证php是否能访问mysql数据库[无论是本地数据库还是远程数据库,测试方式一致]
#1.创建`mysqli.php`,使用php-mysqli模块测试能否正常连接`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 "连接成功"; ?>#2.第二种测试方式, 使用php-pdo模块测试连接mysql[可不测试][root@nginx ~]# cat /code/mysqlpdo.php<?php $servername = "localhost"; $username = "root"; $password = "Bgx123.com"; try { $conn = new PDO("mysql:host=$servername;dbname=mysql", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
3.检测LNMP架构
1.访问对应的info.php
文件, 如出现下图则表示nginx
与php
能正常工作
2.访问mysqli.php
验证php-mysqli
模块是否正常工作
3.访问mysqlpdo.php
验证php-mysql-pdo
模块是否正常工作
4.部署博客系统Wordpress
1.配置Nginx
虚拟主机站点,域名为blog.bgx.com
#1.nginx具体配置信息[root@http-server ~]# cat /etc/nginx/conf.d/wordpress.confserver { 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@http-server ~]# systemctl restart nginx
2.下载wordpress
产品,部署wordress
并授权
#1.获取wordpress代码[root@http-server ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz#2.解压网站源码文件,拷贝至对应站点目录,并授权站点目录[root@http-server ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz[root@http-server /soft/src]# cp -r wordpress /code/[root@http-server ~]# chown -R www.www /code/wordpress/
3.由于wordpress
产品需要依赖数据库, 所以需要手动建立数据库
#1.登陆数据库[root@http-server ~]# mysql -uroot -pBgx123.com#2.创建wordpress数据库MariaDB [(none)]> create database wordpress; MariaDB [(none)]> exit
4.通过浏览器访问网站
6.部署知乎系统Wecenter
1.配置Nginx
虚拟主机站点,域名为zh.bgx.com
#1.nginx具体配置信息[root@http-server ~]# cat /etc/nginx/conf.d/zh.confserver { 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.通过浏览器访问网站
7.部署网校系统Edusohu
1.配置Nginx
虚拟主机站点,域名为edu.bgx.com
#1.nginx具体配置信息[root@http-server ~]# cat /etc/nginx/conf.d/wordpress.confserver { 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.通过浏览器访问网站
8.迁移数据至独立服务器
拆分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#3.检查数据库是否被导入成功mysql> show databases;#3.重新授权MariaDB [(none)]> grant all on *.* to all@'%' identified by 'Bgx123.com'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
3.修改wordpress、wecenter、edusohu
程序连接数据库信息
# wordpressvim /code/wordpress/wp-config.php#edusohorm -rf /code/edu/app/cache/* vim /code/edu/app/config/parameters.yml# wecenter/code/zh/system/config/database.php
9.迁移图片至独立服务器
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.web02端操作
#1.web先找出图片存储的路径,然后进行挂载路径位置: http://blog.oldboy.com/wp-content/uploads/2018/08/655-300x169.png#2.安装nfs工具[root@web02 wp-content]# yum install nfs-utils -y[root@web02 wp-content]# showmount -e 172.16.1.31Export 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#3.导出已存在的图片[root@web02 wp-content]# cd /code/wordpress/wp-content[root@web02 wp-content]# ls uploads/2018[root@web02 wp-content]# mv uploads/2018/ ./#4.挂载nfs存储[root@web02 wp-content]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/[root@web02 wp-content]# mv 2018/ uploads/#5.将挂载信息加入开机自启[root@web02 wp-content]# 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
3.web01端操作
#将解析跳转到web01上,然后在进行网站强制访问,检查图片是否存在,如果不存在则进行挂载#1.挂载nfs存储[root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/#2.加入开机自启[root@web01 ~]# 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