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.用户发起的所有请求会先抵达LNMP架构中的Nginx
2.如果用户请求的是静态内容,则Nginx直接响应并处理。
3.如果用户请求的是动态内容,则通过fastcgi协议发送至php-fpm管理进程
4.php-fpm接收到请求后,会派生对应的warrap线程,来解析用户请求的动态内容。
5.如果涉及到查询数据库操作,则需要php先连接数据库,然后进行查询操作。(php-mysql)
6.最终由mysql-->php-fpm->fastcgi->nginx->user
``
安装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 repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1#2.执行yum安装[root@nginx ~]# yum install nginx -y#3.修改Nginx的运行身份[root@web01 ~]# groupadd -g 666 www[root@web01 ~]# useradd -u666 -g666 www[root@nginx ~]# sed -i ‘/^user/c user www;‘ /etc/nginx/nginx.conf #4.启动并加入开机自启动[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 -y#2.安装扩展源[root@web01 nginx]# yum localinstall -y http://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
? ???????
3.安装MySQL5.7版本数据库
# 下载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@web02 ~]# mysql -uroot -p$(awk ‘/temporary password/{print $NF}‘ /var/log/mysqld.log)#6.重新修改数据库密码mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘RSX123.com‘;
配置LNMP架构
1.验证Nginx是否能正常解析php动态请求[root@nginx ~]# cat /etc/nginx/conf.d/php.conf server {server_name www.rsx.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.创建info.php测试php是否正常解析
[root@nginx ~]# cat /code/info.php<?phpphpinfo();?>
? ???
2.测试php是否能连接mysql数据库服务[无论是本地数据库还是远程数据库,测试方式一致]
[root@nginx ~]# cat /code/mysql.php<?php$servername = "localhost";$username = "root";$password = "RSX123.com";// 创建连接$conn = mysql_connect($servername, $username, $password);// 检测连接if (!$conn) {die("Connection failed: " . mysql_connect_error());}echo "连接成功";?>
检测LNMP架构
1.通过浏览器访问info.php文件, 如出现下图则表示nginx与php能正常工作
info.php
2.访问mysqli.php验证php-mysqli模块是否正常工作
mysql.php
? ???
部署博客系统Wordpress
1.配置Nginx虚拟主机站点,域名为blog.rsx.com
#1.nginx具体配置信息[root@nginx ~]# cat /etc/nginx/conf.d/wordpress.confserver {listen 80;server_name blog.rsx.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并授权
#1.获取wordpress代码[root@nginx ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz#2.解压网站源码文件,拷贝至对应站点目录,并授权站点目录[root@nginx ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz[root@nginx ~]# cp -r wordpress /code/[root@nginx ~]# chown -R www.www /code/wordpress/
4.由于wordpress产品需要依赖数据库, 所以需要手动建立数据库
#1.登陆数据库[root@http-server ~]# mysql -uroot -pRSX123.com#2.创建wordpress数据库MariaDB [(none)]> create database wordpress;MariaDB [(none)]> exit
? ???
5.通过浏览器访问wordpress, 并部署该产品
blog.rsx.com/wp-admin
? ???
部署知乎系统Wecenter
1.配置Nginx虚拟主机站点,域名为zh.rsx.com
#1.nginx具体配置信息[root@http-server ~]# cat /etc/nginx/conf.d/zh.confserver { ???listen 80; ???server_name zh.rsx.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 -pRSX123.com#2.创建wordpress数据库MariaDB [(none)]> create database zh;MariaDB [(none)]> exit
3.通过浏览器访问网站
zh.rsx.com
部署网校系统Edusohu
1.配置Nginx虚拟主机站点,域名为edu.rsx.com
#1.nginx具体配置信息[root@http-server ~]# cat /etc/nginx/conf.d/edu.confserver { ???listen 80; ???server_name edu.rsx.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.通过浏览器访问网站
edu.rsx.com
迁移数据至独立服务器
1.安装好MySQL数据库
#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@web02 ~]# mysql -uroot -p$(awk ‘/temporary password/{print $NF}‘ /var/log/mysqld.log)#6.重新修改数据库密码 ???mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘RSX123.com‘;
2.备份原数据库中的数据
#1.指定导出对应的数据库文件。(RSX123.com是数据库密码)[root@web02 ~]# mysqldump -uroot -p‘RSX123.com‘ --all-databases --single-transaction > `date +%F%H`-mysql-all.sql#2.拷贝原数据库备份文件至新的服务器[root@web02 zh]# scp 2018-09-2109-mysql-all.sql ?root@172.16.1.51:~#3.将数据导入进新的数据库环境中[root@db01 ~]# mysql -uroot -pRSX123.com <2018-09-2109-mysql-all.sql#4.验证是否导入成功[root@db01 ~]# mysql -uroot -pRSX123.com
#5.登录数据库后,查看当前有多少个库mysql> show databases;#6.使用客户端测试连接远端的mysql[root@web01 ~]# mysql -h172.16.1.51 -uroot -pRSX123.commysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1130 (HY000): Host ‘172.16.1.7‘ is not allowed to connect to this MySQL server
#7.新的数据库,配置允许远程用户连接 ???????#授权所有权限 ??grant all privileges ???????#授权所有库所有表 *.* ????????#将授权赋予给哪个用户,这个用户只能通过哪个网段过来(%所有) ‘all‘@‘%‘ ???????#授权该用户登录的密码 identified bymysql> grant all privileges on *.* to ‘all‘@‘%‘ identified by ‘RSX123.com‘;#8.再次使用客户端测试连接远端的mysql[root@web01 ~]# mysql -h172.16.1.51 -uall -pRSX123.com#如果其他机器想测试能否连接数据库,可以先安装mysql的客户端软件[root@web02 ~]# yum install mariadb -y[root@web02 ~]# mysql -h172.16.1.51 -uall -pRSX123.com
#9.停止web本地的数据库,刷新网页看看是否down机[root@web01 ~]# systemctl stop mysqld[root@web01 ~]# systemctl disable mysqld
#10.修改Wordpress产品代码连接数据库的配置文件[root@web01 ~]# vim /code/wordpress/wp-config.php#数据库名称define(‘DB_NAME‘, ‘wordpress‘);#数据库用户define(‘DB_USER‘, ‘all‘);#数据库密码define(‘DB_PASSWORD‘, ‘RSX123.com‘);#数据库地址define(‘DB_HOST‘, ‘172.16.1.51‘);#11.修改wecenter产品代码连接数据库的配置文件[root@web01 zh]# ?grep -iR "RSX123.com"|grep -v cachesystem/config/database.php: ?‘password‘ => ‘RSX123.com‘,[root@web01 zh]# vim /code/zh/system/config/database.php‘host‘ => ‘172.16.1.51‘,‘username‘ => ‘all‘,‘password‘ => ‘RSX123.com‘,‘dbname‘ => ‘zh‘,#12.修改edusogho产品代码连接数据库的配置文件[root@web01 edu]# ?grep -iR "RSX123.com"|grep -v cacheapp/config/parameters.yml: ???database_password: ‘RSX123.com‘[root@web01 edu]# vim /code/edu/app/config/parameters.yml parameters:database_driver: pdo_mysqldatabase_host: 172.16.1.51database_port: 3306database_name: edudatabase_user: alldatabase_password: ‘RSX123.com‘#必须清理缓存[root@web01 edu]# rm -rf /code/edu/app/cache/*
迁移图片至独立服务器
1.准备一台新的服务器安装NFS[root@nfs ~]# groupadd -g666 www[root@nfs ~]# useradd -u666 -g666 www[root@nfs ~]# yum install nfs-utils -y[root@nfs ~]# 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)创建对应的共享目录,并授权为www[root@nfs ~]# mkdir /data/{blog,zh,edu} -p [root@nfs ~]# chown -R www.www /data/重启NFS[root@nfs ~]# systemctl restart nfs-server2.WEB客户端验证NFS是否安装成功[root@web01 ~]# yum install nfs-utils -y[root@web01 ~]# 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.获取Wordpress产品的附件和图片存放的位置 ???浏览器->右键->检查->Network->选择按钮->点击一下图片4.备份web服务器上的Wordpress图片和附件[root@web01 ~]# cd /code/wordpress/[root@web01 wordpress]# cp -rp wp-content/ wp-content_back5.客户端执行挂载操作【Wordpress】[root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content6.恢复原web上存储的(图片、附件、语言、主题)[root@web01 wordpress]# cp -rp wp-content_back/* ?wp-content/
==============================================================1.获取Edu产品的附件和图片存放的位置 ???浏览器->右键->检查->Network->选择按钮->点击一下图片2.备份edu产品的视频和图片视频:/code/edu/app/data/udisk/图片:/code/edu/web/files/3.备份原有的视频和图片[root@web01 edu]# cp -rp ?web/files/ web/files_bak[root@web01 edu]# cp -rp app/data/ app/data_bak4.执行挂载操作【我仅挂载视频,其他自行解决】[root@web01 edu]# mount -t nfs 172.16.1.31:/data/edu /oldboy_code4/edu/app/data5.恢复数据至NFS存储[root@web01 edu]# \cp -rp app/data_bak/* app/data/
LNMP网站架构
原文地址:http://blog.51cto.com/13528471/2281786