[实验] LNMP 平台的搭建 (CentOS Linux 8 版)

步骤一:LNMP 简介

LNMP 是一个实现网站服务的方法,它由 4 样东西组成:
1) Linux 系统
2) Nginx 网页服务
3) MariaDB 数据库
4) PHP 网页程序

步骤二:系统环境要求

1) 服务器的系统需要是 CentOS Linux 8 版本
2) 服务器要关闭防火墙
3) 服务器要关闭 SELinux
4) 服务器系统要配置好可用的软件源

步骤三:搭建 LNMP

3.1 Nginx 网页服务

3.1.1 安装 Nginx 网页服务
# yum -y install nginx
3.1.2 配置 Nginx 网页服务的配置文件
3.1.2.1 删除原有的 Nginx 服务的配置文件
# rm /etc/nginx/nginx.conf
3.1.2.2 创建新的 Nginx 网页服务的配置文件
# cp /etc/nginx/nginx.conf.default /etc/nginx.conf
3.1.2.3 配置 Nginx 网页服务的配置文件
# vi /etc/nginx/nginx.conf

将其中的:

......
        location / {
            root   html;
            index  index.html index.htm;
        }
......
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
......

修改为:

......
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
......
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
......

(补充:这里以让 Nginx 将对于 PHP 的请求传递到本机的 9000 端口为例)

3.1.3 启动 nginx 网页服务
# systemctl start nginx

3.2 MariaDB 数据库

3.2.1 安装 MariaDB 数据库
# yum -y install mariadb mariadb-server
3.2.2 启动 MariaDB 数据库
# systemctl start mariadb

3.3 PHP 环境和连接服务

3.3.1 安装 PHP 环境和连接服务
# yum -y install php php-fpm php-mysqlnd php-gd php-mbstring php-opcache php-json php-xml
3.3.2 创建提供 PHP 连接服务的用户
# useradd php-fpm -s /sbin/nologin
3.3.3 配置 PHP 连接服务的配置文件
# vi /etc/php-fpm.conf

添加以下内容:

......
[www]
user = php-fpm
group = php-fpm
listen = 127.0.0.1:9000

(补充:这里以让 php-fpm 监听本地 9000 端口为例)

3.3.4 启动 PHP 连接服务
# systemctl start php-fpm

步骤四:后续工作

1) 给 MariaDB 数据库设置用于存储网页数据的用户和密码
2) 将 PHP 网页程序放到 Nginx 的网页目录下
3) 给 PHP 网页程序设置用于连接 MariaDB 数据库的用户和密码

步骤五:测试 LNMP 平台

使用浏览器访问服务器 IP 地址就可以看到对应 PHP 网页了

[步骤] Linux Nginx 源码安装包的管理 (通过 systemd 实现)

注意:

在通过 systemd 管理源码安装的软件或自制 rpm 包安装的软件(以 Nginx 为例)之前,先要源码安装 Nginx 或者自制 rpm 包安装 Nginx

正文:

步骤一:创建 Nginx 的 systemd 文件

# vim /etc/systemd/system/nginx.service 

创建以下内容:

[Unit]

Description=nginx server daemon

Documentation=man:nginx(8)

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

步骤二:导入新创建的 Nginx systemd 配置文件

# systemctl daemon-reload

步骤三:使用 systemd 管理 Nginx

3.1 使用 systemd 启动 Nginx

# systemctl start nginx.service

3.2 使用 systemd 开机自启 Nginx

# systemctl enable nginx.service

3.3 使用 systemd 显示 Nginx 的状态

# systemctl status nginx.service

[步骤] Nginx 日志的切割

注意:

在设置 Nginx 自动化日志切割并保存之前要先安装 Nginx

正文:

内容一:切割 Nginx 日志的原理

# mv access.log access2.log
# kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

内容二:切割 Nginx 日志的 Shell 脚本

# mkdir -p /usr/local/nginx/
# vim /usr/local/nginx/nginxlog.sh

创建以下内容:

#!/bin/bash
date=$(date +%Y%m%d)
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log   $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)

内容三:设置 Nginx 日志切割的自动化任务

# crontab -e
03 03 * * 5 /usr/local/nginx/nginxlog.sh

(补充:这里以每周五的 3 点 3 分执行 /usr/local/nginx/nginxlog.sh 命令为例)

[实验] Nginx 模块的设置 (监控模块)

软件准备:

在 Nginx 官网上下载搭建集群所需软件 Nginx:

http://nginx.org/en/download.html

正文:

步骤一:系统环境要求

1) 所有服务器的系统都需要是 CentOS 7 版本
2) 所有服务器系统都需要有 yum 源

步骤二:安装带有状态信息监控模块的 Nginx

# yum -y install gcc pcre-devel openssl-devel
# tar -xvf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# ./configure \
>--with-http_stub_status_module
# make && make install

(补充:这里以安装 nginx-1.16.1 为例)

步骤三:修改 Nginx 的配置文件

# vi /usr/local/nginx/conf/nginx.conf

将部分内容修改如下:

......
location /status {
stub_status on;
#allow IP address;
#deny IP address;
}
......

步骤四:启动 Nginx

# /usr/local/nginx/bin/nginx

步骤五:显示监控模块

通过浏览器访问以下网址:

http://127.0.0.1/status

[步骤] Nginx 网页的设置 (404 网页)

注意:

在设置 Nginx 404 报错页面之前要先安装 Nginx

正文:

步骤一:修改 Nginx 的配置文件

# vi /usr/local/nginx/conf/nginx.conf

将部分内容修改如下:

server {
......
error_page 404 /40x.html;
......
}

或者:

server {
......
error_page 404 /40x.html;
Error_page 404 /40.xhtml;
......
}

步骤二:自定义 404 报错网页

# vi /usr/local/nginx/html/40.xhtml