LNMP架构原理及基础运用
LNMP数据传输原理
- 用户通过http协议发起请求,请求会先抵达LNMP架构中的Nginx
- Nginx根据用户的请求进行判断,该判断由Location进行完成(静态请求走本地,动态请求交给后端)。
- 判断用户请求为静态页面,Nginx直接进行处理。
- 判断用户请求的是动态页面,Nginx会将该请求交给fastcgi协议进行下发
- fastcgi协议将请求提交给php-fpm管理进程,php-fpm管理进程接收到后悔生成具体的针对该动态请求的线程warrap。
- 由warrap线程操作php解析器进行解析
- 若请求中设计查询数据库的操作,则由php连接数据库(用户名 密码),然后发起查询操作
- 最终将解析结果返还给用户
搭建LNMP架构
安装Nginx
要使用官方Nginx源,无需手动编译避免繁琐操作。
手动配置yum源
[root@nginx~]vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
执行yum安装
[root@nginx ~]# yum install nginx -y
启动nginx并加入开机自启
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
安装php(7.1版)
移除旧版php
[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common -y
配置扩展源
[root@nginx nginx]# yum localinstall -y http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装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
统一系统权限,修改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
启动php-fpm管理进程,并加入开机自启
root@nginx ~]# systemctl start php-fpm
[root@nginx ~]# systemctl enable php-fpm
配置Nginx连接php,并进行测试
[root@nginx conf.d]# cat /etc/nginx/conf.d/blog.michaelxia.conf
server {
listen 80;
server_name test.michaelxia.com;
location {
root /code/test;
index index.php index.html;
}
location ~ \.php$ {
root /code/test;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
创建对应站点目录
[root@Nginx conf.d]# mkdir /code/test
[root@Nginx conf.d]# vim /code/test/index.php
######该文件内容为测试php成功连接
<?
phpinfo();
?>
检查Nginx配置
[root@nginx ~]# nginx -t
重载Nginx
[root@nginx ~]# systemctl reload nginx
hosts解析
10.0.0.8 test.michaelxia.com
测试
浏览器访问:http://test.michaelxia.com
安装Mysql(5.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
yum安装
[root@nginx ~]# yum install mysql-community-server -y
启动Mysql,并加入开机自启
[root@nginx ~]# systemctl start mysqld
[root@nginx ~]# systemctl enable mysqld
使用Mysq初始密码登录数据库
[root@nginx ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
修改数据库密码
修改默认密码规则
数据库默认密码规则必须携带大小写字母、特殊符号,字符长度大于8否则会报错。
mysql> set password for root@localhost = password('123456');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
因此设定较为简单的密码时需要首先修改set global validate_password_policy参数值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
1.此时密码限制已取消,判断密码的规则至基于密码的长度了,默认长度为8,由validate_password_length参数所决定
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 8 |
+----------------------------+
1 row in set (0.00 sec)
修改其最小值
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
此时密码长度最小值为4
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)
修改密码
mysql> set password for root@localhost = password('6256133');
Query OK, 0 rows affected, 1 warning (0.00 sec)
测试php能否连接Mysql
[root@nginx ~]# vim /code/test/mysqli.php
<?php
$servername = "localhost";
$username = "root";
$password = "6256133";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>
部署workpress
下载workpress数据包
[root@nginx ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
解压到站点目录
[root@nginx ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /code/test
修改权限
[root@nginx ~]# chown -R www.www /code/test/
创建workpress数据库
[root@http-server ~]# mysql -uroot -p123456
mysql> create database wordpress;
mysql> exit
浏览器访问域名进行workpress安装部署
部署wecenter
前期站点部署与workpress一致:
- 配置站点配置文件
- 创建站点目录
- 修改权限
- 下载数据包并解压
- 创建数据库
浏览器访问域名对wecenter进行安装部署
部署edusudo网校系统
站点配置文件必须安装管网要求修改
server {
listen 80;
server_name edu.michaelxia.com;
root /code/edusoho/web;
location / {
index app.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/udisk {
internal;
root /code/edusoho/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/edusoho/app/data/udisk;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
}
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;
}
}
其他站点目录配置不变
浏览器访问域名并部署安装