0%

Nginx-LNMP架构搭建

Nginx-LNMP架构搭建

Nginx和fastcgi之间的故事

1566179108056

1)发起一个!1566179108056](C:\Users\86152\Desktop\typora-user-images\1566179108056.png)动态请求,连接到服务器找到nginx

2)nginx来判断请求,找到相应location

1
2
3
location ~ \.php$ {
fastcgi_pass 127.0.0.1;
}

3)通过fastcgiphp-fpm管理进程建立连接

4)php-fpm调用wrapper对动态请求进行解析

php-fpm.conf:配置php进程相关的

php.ini:配置php工作请求相关的

5)如果需要用到数据库,php代码连接 数据库

nginx->fastcgi->php-fpm->wrapper->php.ini->php解析器->mysql

部署LNMP

部署nginx

1)更换nginx的官方源

1
2
3
4
5
6
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2)安装nginx

1
[root@web01 ~]# yum install -y nginx

3)创建nginx启动用户

1
2
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

4)修改nginx的启动用户

1
2
3
#修改nginx的配置文件
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

5)启动nginx并加入开机自启

1
2
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

6)打开浏览器查看nginx是否启动成功

http://10.0.0.7

1566181245191

部署PHP

1)更改PHP源

1
2
3
4
5
[root@web02 ~]# vim /etc/yum.repos.d/php.repo 
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

2)安装PHP

1
[root@web02 ~]# 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

3)修改PHP的启动用户

1
2
3
[root@web01 conf.d]# vim /etc/php-fpm.d/www.conf
user = www
group = www

4)启动php加入开机自启

1
2
[root@web01 conf.d]# systemctl start php-fpm
[root@web01 conf.d]# systemctl enable php-fpm

5)配置nginx通过fastcgi连接php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@web01 conf.d]# vim php.conf
server {
listen 80;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

6)打开浏览器访问

1566182781238

部署数据库

1)安装mariadb

1
[root@web01 ~]# yum install -y mariadb-server

2)启动并加入开机自启

1
2
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb

3)设置数据库密码

1
[root@web01 ~]# mysqladmin -uroot password 'Zls123.com'

4)连接数据库

1
[root@web01 ~]# mysql -uroot -pZls123.com

5)测试php连接数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$servername = "localhost";
$username = "root";
$password = "Zls123.com";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?>

<img style='width:100%;height:100%;' src=https://www.driverzeng.com/zenglaoshi/php_mysql.png>

1566186558517

6)连接数据库,创建库

1
2
3
4
5
6
7
8
9
10
11
12
13
#查看有哪些数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
#创建数据库
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

1566188109395

1566188157026