一:LNMP是如何工作的
当用户发起http请求时,请求会被nginx处理,如果是静态资源请求.nginx会直接返回;如果是动态请求,nginx则通过fastcgi协议转发给后端的php程序处理,如下图所示
1.用户通过http协议发送请求,请求会先抵达LNMP架构中的nginx
2.nginx会根据用户请求进行判断,这个判断由location进行完成
3.如果判断用户请求的是静态页面,nginx直接处理
4.如果判断用户请求的是动态页面,nginx会将该请求交给fastcgi协议
5.fastcgi会将请求交给php-fpm管理进程,php-fpm管理进程接收到会调用具体的工作进程warrap
6.warrap进程会调用php程序进行解析,如果只是解析代码php直接访问
7.如果有查询数据库操作,则由php连接数据库发起查询的操作
8.最终数据在由mysql->php->php-fpm->fastcgi->nginx->http-->用户
二:部署LNMP
2.1 安装nginx
2.1.1 创建www同一用户
[root@web01-7 ~]# groupadd www -g 666
[root@web01-7 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01-7 ~]# id www
uid=666(www) gid=666(www) 组=666(www)
2.1.2 安装nginx
#更新nginx最新官网源
[root@web01-7 /]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
#安装依赖包
[root@web01-7 ~]# yum install openssl-devel pcre-devel -y
#安装nginx
[root@web01-7 /]# yum install -y nginx
2.1.3 修改nginx进程用户和组,并启动服务
[root@web01-7 ~]# cat /etc/nginx/nginx.conf
user www www; #启动进程用户和组为www
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
[root@web01-7 ~]# systemctl start nginx
[root@web01-7 ~]# systemctl enable nginx
2.2 安装php
2.2.1 使用扩展源安装php7.1
[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common
[root@web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@web01 ~]# 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
2.2.3 修改配置php-fpm用户与nginx一致
[root@web01-7 php-fpm.d]#sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web01-7 php-fpm.d]#sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
2.2.4 启动服务php-fpm
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
2.3 安装mysql
2.3.1 mysql官网下载5.6.38的二进制安装包
2.3.2 将安装包解压到/usr/local/
[root@web01-7 code]# cd /usr/local/
[root@web01-7 local]# tar zvxf mysql-5.6.38-linux-glibc2.12-x86_64
[root@web01-7 local]# ln -s mysql-5.6.38-linux-glibc2.12-x86_64 mysql
2.3.3 创建数据目录
[root@web01-7 local]# mkdir -p /data/mysql/data
2.3.4 创建用户
[root@web01-7 local]#useradd mysql
2.3.5 修改相关目录的所属用户
[root@web01-7 local]#chown -R mysql.mysql /usr/local/mysql /data
2.3.6 修改环境变量
[root@web01-7 local]#echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@web01-7 local]#source /etc/profile
2.3.7 查看mysql版本
[root@web01-7 local]# mysql -V
mysql Ver 14.14 Distrib 5.6.38, for linux-glibc2.12 (x86_64) using EditLine wrapper
2.3.8 初始化数据
[root@web01-7 /]# ./usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
2.3.9 创建配置文件
[root@web01-7 /]# rm -rf /etc/my.cnf
[root@web01-7 /]# cat > /etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
EOF
2.3.10 启动mysql
创建mysql的systemctl启动文件
[root@web01-7 /]#cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
[root@web01-7 local]# systemctl start mysqld.service
[root@web01-7 local]# systemctl enable mysqld.service
2.3.11 修改mysql密码
[root@web01-7 local]# mysqladmin -uroot -p password 123456
2.4 查看php是否连通
2.4.1 配置nginx子配置文件
[root@web01-7 ~]# vim /etc/nginx/conf.d/php.conf
server {
server_name www.cxy.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.4.2 创建info.php文件验证php是否正常
[root@web01-7 ~]# mkdir /code
[root@web01-7 ~]# chown -R www:www /code/
[root@web01-7 ~]# cat /code/info.php
<?php
phpinfo();
?>
2.4.3 重载nginx复位
[root@web01-7 local]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01-7 local]# systemctl reload nginx
2.4.4 修改window hosts文件,访问www.cxy.com/info.php
2.5 检查mysql数据库是否连通
2.5.1 创建mysql.php文件
[root@web01 ~]# cat /code/mysql.php
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// // 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "php 连接 MySQL 数据库成功";
?>
2.5.2 重载nginx
[root@web01-7 local]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01-7 local]# systemctl reload nginx
2.5.3 web访问www.cxy.com/mysql.php
三:搭建wordpress博客
3.1 创建wordpress站点配置文件
[root@web01-7 code]# cd /etc/nginx/conf.d/
[root@web01-7 conf.d]# cat wordpress.conf
server {
listen 80;
server_name blog.cxy1.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;
}
}
3.2 将wordpress站点文件解压/code目录下
[root@web01-7 code]# cd /code
[root@web01-7 code]# tar xf wordpress-4.9.4-zh_CN.tar.gz
修改目录用户权限
[root@web01-7 code]# chown -R www:www wordpress/
3.3 重载nginx
[root@web01-7 local]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01-7 local]# systemctl reload nginx
3.4 创建wordpress数据库
mysql> create database wordpress;
3.5 修改window的hosts,通过blog.cxy1.com浏览器设置wordpress
四:LNMP数据库分离
4.1 为什么要拆分数据库
现在我们的LNMP全部在web01-7这台服务器上,这样会导致网站访问速度缓慢,当内存被吃满时,很容易导致系统出现溢出,从而杀掉数据库进行,所以需要将web和数据库进行独立部署
4.2 准备一台虚拟机做独立数据库db01-51
安装步骤省略,和2.3一样
4.3 在web01上的数据库备份出来
[root@web01-7 tmp]# mysqldump -uroo -p'123456' -A --single-transaction > mysql-all.sql
4.4 将数据库备份文件scp到db01的/tmp目录下
[root@web01-7 tmp]# scp mysql-all.sql 10.0.0.51:/tmp
4.5 将数据库备份文件导入到db01数据库里
[root@db01-51 ~]#mysql -uroot -p123456 < /tmp/mysql-all.sql
[root@db01-51 ~]# mysql -uroot -p123456 -e "show databases;"
4.6 授权数据库,让web服务器能正常连接到db01上
[root@db01 ~]# mysql -uroot -p'123456'
> grant all privileges on *.* to 'all'@'%' identified by '123456';
> flush privileges;
>exit;
grant all privilegs 授权所有权限
*.*授权所有库所有表.
授权给all这个用户,这个用户可以通过哪些网段连接过来,%代表所有
授权该用户登录的密码123456
4.7 在web01上测试使用能使用all这个用户连接到db01数据库
[root@web01-7 ~]# mysql -uall -p123456 -h10.0.0.51
4.8 在web01上修改wordpress配置文件里的数据库信息,让网站能正常连接到db01上
[root@web01-7 wordpress]# vim wp-config.php
define('DB_USER', 'all');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', '10.0.0.51');
4.9 停掉web01上的数据库,访问web01上的wordpress,正常访问
五:拓展多台web服务器节点
为后面做负载均衡做准备,3台web服务器web01,web02,web03,同用一个数据库db01-51数据库
5.1 搭建nginx和PHP,步骤和之前一样
5.2 web02和web03上部署wordpress
和之前一样,但是需要注意在web界面设置wordpress数据库时,ip为10.0.0.51,用户名为all
5.3 测试访问blog.cxy1.com
修改window hosts文件,注释掉web01,添加web02的域名解析,访问正常
六:NFS挂载
6.1 为什么要使用nfs挂载
当多台web服务器同时工作时,用户访问到web01上传了静态资料,如图片等,当web01停止工作了,用户访问到web02上时,图片就会看不到,所以使用nfs进行共享挂载
6.2 启用nfs服务器nfs-31
[root@nfs01 ~]# yum install nfs-utils -y
6.3 修改nfs配置文件并且创建挂载目录
#因为nginx和php启动用户都是www,所以配置文件里面也要标明用户
[root@nfs-31 uploads]# cat /etc/exports
/data/wordpress/wp-content/uploads 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
#创建目录
[root@nfs-31 /]# mkdir -p /data/wordpress/wp-content/uploads/
#修改目录权限
[root@nfs-31 /]# chown -R www:www /data/
6.4 启动nfs服务
[root@nfs-31 uploads]# systemctl restart rpcbind nfs
#查看挂载信息
[root@nfs-31 03]# showmount -e
Export list for nfs-31:
/data/wordpress/uploads 10.0.0.0/24
6.5 在3台web01,02.03服务器上进行挂载;只演示挂载web01
6.5.1 安装nfs服务,启动rpcbind
[root@web01-7 08]# yum install nfs-utils -y
启动服务,客户端只需要启动rpcbind
[root@web01-7 08]# systemctl start rpcbind
[root@web01-7 08]# systemctl enable rpcbin
查看挂载信息
[root@web01-7 03]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data/wordpress/uploads 10.0.0.0/24
6.5.2 先将web01上的源图片进行备份,防止挂载后,图片丢失
[root@web01-7 ~]# cd /code/wordpress/wp-content/
[root@web01-7 ~]#cp -a uploads/ uploads_bak/
6.5.3 挂载nfs
[root@web01-7 ~]#mount 10.0.0.31:/data/wordpress/uploads /code/wordpress/wp-content/uploads/
#挂载之后,目录里面的是没有之前的图片的,将备份目录下的图片复制过去
[root@web01-7 ~]#cp /code/wordpress/wp-content/uploads_bak/* /code/wordpress/wp-content/uploads
设置开机自动挂载
[root@web01-7 wp-content]# cat /etc/fstab
10.0.0.31:/data/wordpress/uploads /code/wordpress/wp-content/uploads nfs defaults 0 0