LNMP架构 第1章 LNMP架构概述 1.1 什么是LNMP LNMP
是一套技术的组合, L=Linux、 N=Nginx、 M=MySQL、 P=PHP
1.2 LNMP架构是如何工作的 首先 Nginx 服务是不能处理动态请求,那么当用户发起动态请求时, Nginx 又是如何进行处理的。 当用户发起 http 请求,请求会被 Nginx 处理,如果是静态资源请求 Nginx 则直接返回,如果是动态请求 Nginx 则通过 fastcgi 协议转交给后端的 PHP 程序处理,具体如下图所示
1.3 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安装部署 2.1 创建www统一用户 1 2 3 4 [root@web01 ~] [root@web01 ~] [root@web01 ~] uid=666(www) gid=666(www) 组=666(www)
2.2 使用官方仓库安装nginx 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@web01 ~] [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 [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 [root@web01 ~] [root@web01 /etc/yum.repos.d] root 1291 1 0 15:05 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf www 1292 1291 0 15:05 ? 00:00:00 nginx: worker process root 1697 1517 0 15:20 pts/0 00:00:00 grep --color=auto nginx
2.3 启动Nginx并加入开机自启动 1 2 [root@web01 ~] [root@web01 ~]
2.4 使用第三方拓展源安装php7.1 1 2 3 4 5 [root@web01 ~] [root@web01 ~] [root@web01 /etc/yum.repos.d] [root@web01 /etc/yum.repos.d] [root@web01 ~]
2.5 配置php-fpm用户与nginx用户保持一致 1 2 [root@web01 ~] [root@web01 ~]
2.6 启动php-fpm并加入开机自启动 1 2 [root@web01 ~] [root@web01 ~]
2.7 安装Mariadb数据库
2.8 启动Mariadb数据库并加入开机自启动 1 2 [root@web01 ~] [root@web01 ~]
2.9 配置Mariadb账号密码 1 2 [root@web01 ~] [root@web01 ~]
2.10 mysql基础命令 #登录mysql
#查看当前mysql有哪些用户
1 2 3 4 5 6 7 8 9 10 11 12 MariaDB [(none)]> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | web01 | | root | web01 | +------+-----------+ 6 rows in set (0.01 sec)
#使用指定IP登录
#查看当前有哪些数据库
1 2 3 4 5 6 7 8 9 10 11 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | wordpress | +--------------------+ 5 rows in set (0.10 sec)
#查看库里面有哪些表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 MariaDB [(none)]> show tables from mysql; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 24 rows in set (0.00 sec)
#退出
第3章 LNMP环境配置 3.1 Fastcgi语法 设置 fastcgi 服务器的地址,该地址可以指定为域名或 IP 地址,以及端口
1 2 3 4 5 6 7 Syntax: fastcgi_pass address; Default: — Context: location, if in location fastcgi_pass localhost:9000; fastcgi_pass unix:/tmp/fastcgi.socket;
设置 fastcgi 默认的首页文件,需要结合 fastcgi_param 一起设置
1 2 3 Syntax: fastcgi_index name; Default: — Context: http, server, location
通过 fastcgi_param 设置变量,并将设置的变量传递到后端的 fastcgi 服务器
1 2 3 4 5 6 7 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 ;
3.2 最终Nginx连接FastCGI服务器配置如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@web01 ~] server { server_name php.oldboy.com; listen 80; root /code; index index.php index.html; access_log /var/log /nginx/php.access.log main; 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; } }
3.3 在/code目录下创建index.php文件并访问测试 1 2 3 4 5 6 [root@web01 ~] [root@web01 ~] [root@web01 /code] <?php phpinfo(); ?>
3.4 检查nginx语法并重加载 1 2 3 4 [root@web01 /code] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /code]
3.5 绑定hosts然后在浏览器访问
3.6 测试php和数据库访问是否正常 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@web01 /code] <?php $servername = "localhost" ; $username = "root" ; $password = "oldboy" ; // 创建连接 $conn = mysqli_connect($servername , $username , $password ); // // 检测连接 if (!$conn ) { die("Connection failed: " . mysqli_connect_error()); } echo "php 连接 MySQL 数据库成功" ; ?>
3.7 通过浏览器访问数据库页面
第4章 部署博客Wordpress 4.1 配置Nginx虚拟主机站点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@web01 /etc/nginx/conf.d] server { listen 80; server_name blog.oldboy.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; } }
4.2 重启nginx 1 2 3 4 [root@web01 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /etc/nginx/conf.d]
4.3 下载解压wordpress到代码目录 1 2 3 4 5 [root@web01 ~] [root@web01 ~] [root@web01 /code] [root@web01 /code] [root@web01 /code]
4.4 创建wordpress数据库 1 2 3 [root@web01 /code] > create database wordpress CHARSET utf8mb4; > exit
4.5 浏览器访问wordpress并部署
第5章 部署问答网站Wecenter 5.1 配置Nginx虚拟站点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@web01 /etc/nginx/conf.d] server { listen 80; server_name zh.oldboy.com; root /code/zh; index index.php index.html; access_log /var/log /nginx/zh.access.log main; 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; } } [root@web01 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 /etc/nginx/conf.d]
5.2 下载解压wecenter并授权 1 2 3 4 5 [root@web01 /code] [root@web01 /code] -rw-r--r-- 1 root root 10393660 Aug 1 11:57 WeCenter_3-3-2.zip [root@web01 /code] [root@web01 /code]
5.3 创建zh数据库 1 2 3 [root@web01 ~] > create database zh; > exit ;
5.4 浏览器访问
5.5 清除首页安装文件