09 Nginx 构建动态网站架构 lnmp


安装 LNMP 架构

1
yum安装 nginx php7.2 mysql5.7

安装 Nginx

1
2
# 基础软件
yum install -y net-tools wget vim lrzsz tree screen lsof tupdump nc mtr nmap gcc glibc gcc-c++ make
1
2
3
# 1. 使用Nginx官方提供的rpm包
http://nginx.org/en/download.html
http://nginx.org/en/linux_packages.html#RHEL-CentOS
1
2
3
4
5
6
7
[root@linux-node1 ~]# vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
1
2
3
4
5
# 安装启动
[root@linux-node1 ~]# yum install -y nginx
[root@linux-node1 ~]# systemctl start nginx
[root@linux-node1 ~]# systemctl enable nginx
[root@linux-node1 ~]# systemctl status nginx

安装 php7.2

1
2
# 移除旧版php 
[root@linux-node1 ~]# yum remove php-mysql-5.4 php php-fpm php-common
1
2
3
# 1. 安装扩展源
[root@linux-node1 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@linux-node1 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
1
2
3
# php72w-fpm php72w-mysqlnd php72w-opcache 重要必须装
[root@linux-node1 yum.repos.d]# yum -y install php72w php72w-cli php72w-common php72w-devel \
php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache
1
2
3
4
# 启动php
[root@linux-node1 yum.repos.d]# systemctl start php-fpm
[root@linux-node1 yum.repos.d]# systemctl enable php-fpm
[root@linux-node1 yum.repos.d]# systemctl status php-fpm

安装 MySQL5.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 下载官方扩展源,扩展源集成mysql5.6、5.7、8.0,仅5.7仓库是开启
# http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/

[root@linux-node1 ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
[root@linux-node1 ~]# yum install mysql-community-server -y
[root@linux-node1 ~]# systemctl start mysqld
[root@linux-node1 ~]# systemctl enable mysqld

# 查看安装的初始密码
[root@linux-node1 ~]# grep 'temporary password' /var/log/mysqld.log
2019-10-23T08:33:30.968573Z 1 [Note] A temporary password is generated for root@localhost: k&ruTxuGO03#

# 登录修改密码
[root@linux-node1 ~]# mysql -uroot -p'k&ruTxuGO03#'
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass1!';

Nginx vim 语法高亮

1
2
3
4
5
6
7
8
9
10
11
# 创建目录 下载配置文件
[root@linux-node1 syntax]# mkdir -p ~/.vim/syntax
[root@linux-node1 syntax]# cd ~/.vim/syntax
[root@linux-node1 syntax]# wget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vim

# 添加
[root@linux-node1 syntax]# vi ~/.vim/filetype.vi
au BufRead,BufNewFile /etc/nginx* set ft=nginx

# 查看
vim /etc/nginx/nginx.conf

配置 LNMP 架构

配置Nginx 实现动态请求转发至php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@linux-node1 conf.d]# vim php.conf 

server {
server_name 10.0.0.156;
listen 80;
root /soft/code;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /soft/code$fastcgi_script_name;
include fastcgi_params;
}
}

添加 php 测试页面

1
2
3
4
5
6
7
[root@linux-node1 code]# vim /soft/code/info.php

<?php
phpinfo();
?>

[root@linux-node1 code]# echo "<h1>hello</h1>" >> index.html
1
2
3
4
5
# 测试访问
[root@linux-node1 conf.d]# nginx -t
[root@linux-node1 conf.d]# nginx -s reload

http://10.0.0.156/info.php

mysqli模块测试连接 mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@linux-node1 code]# vim /soft/code/mysqli.php

<?php
$servername = "localhost";
$username = "root";
$password = "MyNewPass1!";

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

// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>

http://10.0.0.156/mysqli.php

pdo模块测试连接 mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@linux-node1 code]# vim /soft/code/mysqlpdo.php

<?php
$servername = "localhost";
$username = "root";
$password = "MyNewPass1!";

try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

http://10.0.0.156/mysqlpdo.php

Nginx 与 PHP 原理

Nginx FastCGI的运行原理

PHP 配置文件优化

php-ini 优化

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
31
32
1. php-ini php懂得配置文件,调整php解析器

# 打开php的安全模式,控制php执行危险函数, 默认是Off,改为On
sql.safe_mode = Off
# 关闭php头部信息, 隐藏版本号, 默认是On,该为Off
expose_php = On
# 错误信息输出控制
display_error = Off
error_reporting = E_WARNING & E_ERROR
# 记录错误日志至后台, 方便追溯
log_errors = On
error_log = /var/log/php_error.log
# 每个脚本时间最大内存
memory_limit = 128M
# 上传文件最大许可,默认2M, 建议调整为16,32M
upload_max_filesize = 2M
# 禁止远程执行phpshell,默认On, 建议Off
allow_url_fopen = On
# 时区调整,默认PRC, 建议调整为Asia/Shanghai
date.timezone = PRC


# 整体优化后配置文件
sql.safe_mode = Off
expose_php = Off
display_error = Off
error_reporting = E_WARNING & E_ERROR
log_errors = On
error_log = /var/log/php_error.log
upload_max_filesize = 50M
allow_url_fopen = Off
date.timezone = Asia/Shanghai

php-fpm 优化

1
2
3
4
5
6
7
8
9
10
1. php-fpm 监听9000端口

[root@linux-node1 conf.d]# ps -ef|grep php
root 940 1 0 16:34 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)

[root@linux-node1 conf.d]# vim /etc/php-fpm.conf
[root@linux-node1 conf.d]# include=/etc/php-fpm.d/*.conf # 引入了文件
[root@linux-node1 conf.d]# cd /etc/php-fpm.d/
[root@linux-node1 php-fpm.d]# ls
www.conf
1
2
3
4
5
6
7
8
9
1. 优化该配置文件 
https://www.xuliangwei.com/bgx/1024.html

2. 备份
[root@linux-node1 php-fpm.d]# cd /etc/php-fpm.d/
[root@linux-node1 php-fpm.d]# mv www.conf www.conf.bak

3. 建立日志目录
[root@linux-node1 php-fpm.d]# mkdir -p /var/log/php/
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
31
32
33
34
35
36
37
38
39
40
41
4. 更新配置文件
# PHP-FPM配置文件 4核16G、8核16G

[root@linux-node1 php-fpm.d]# vim www.conf

[global]
pid = /var/run/php-fpm.pid
# php-fpm程序错误日志
error_log = /var/log/php/php-fpm.log
log_level = warning
rlimit_files = 655350
events.mechanism = epoll

[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660

listen.allowed_clients = 127.0.0.1
# 动态
pm = dynamic
pm.max_children = 512
# 启动 1个php-fpm进程占用至少8M内存空间
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.process_idle_timeout = 15s;

pm.max_requests = 2048

# php-www模块错误日志
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php/php-www.log
php_admin_flag[log_errors] = on

# php慢查询日志
request_slowlog_timeout = 5s
slowlog = /var/log/php/php-slow.log
1
2
3
5. 重启php 进程个数已增加为动态
[root@linux-node1 php-fpm.d]# systemctl restart php-fpm
[root@linux-node1 php-fpm.d]# systemctl status php-fpm

PHP5-FPM 配置纤细解释

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[global]
#pid设置, 记录程序启动后pid
pid = /var/run/php-fpm.pid
#php-fpm程序启动错误日志路径
error_log = /soft/log/php/php-fpm_error.log
# 错误级别. 可用级别为: alert(必须立即处理),error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
log_level = warning
#设置文件打开描述符的rlimit限制.
rlimit_files = 65535
events.mechanism = epoll

#启动进程的用户和组
[www]
user = www
group = www

# fpm监听端口
listen = 127.0.0.1:9000
# unix socket设置选项,如果使用tcp方式访问,这里注释即可。
listen.owner = www
listen.group = www
# 允许访问FastCGI进程的IP,any不限制
listen.allowed_clients = 127.0.0.1

# pm设置动态调度
pm = dynamic
# 同一时刻最大的php-fpm子进程数量
pm.max_children = 200
# 动态方式下的起始php-fpm进程数量
pm.start_servers = 20
# 动态方式下服务器空闲时最小php-fpm进程数量
pm.min_spare_servers = 10
# 动态方式下服务器空闲时最大php-fpm进程数量
pm.max_spare_servers = 30
# 最大请求
pm.max_requests = 1024
pm.process_idle_timeout = 15s;

# FPM状态页面,用于监控php-fpm状态使用
pm.status_path = /status
# 错误日志
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www_error.log
php_admin_flag[log_errors] = on

# 配置php慢查询, 以及慢查询记录日志位置
request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log

# 启动 1个php-fpm 进程占用至少8M内存空间