LNMP环境

有些网站需要靠服务器端计算每次响应的页面,这就是动态网站。一般由后端的 PHP、Java、GoLang、Python等编写。不过实际情况会是混合网站,既有动态资源,也有静态资源。

一、介绍

1.1 目标

中文wordpress

主要使用 LNMP 部署一个 WordPress

  • Nginx 1.26.1
  • WordPress 6.9
  • MariaDB 10.11(LTS)
  • PHP8.3

1.2 常见的几种架构

名称 组件 开发语言
LNMP(LEMP) Linux + Nginx + MySQL + PHP PHP
LAMP Linux + Apache + MySQL + PHP PHP
WNMP Windows + Nginx + MySQL + PHP PHP
WAMP Windows + Apache + MySQL + PHP PHP
LNMT Linux + Nginx + MySQL + Tomcat Java
LNMP Linux + Nginx + MySQL + Python Python
LNMG Linux + Nginx + MySQL + GoLang GoLang
  • LNMP 又叫 LEMP,因为 Nginx 之前叫 Engine X
  • M 有时候也代表 MySQL 的开源版 MariaDB

LNMP(LEMP)主要原理是 Nginx 收到动态请求后,通过 fastcgi 协议转发给后端的 php-fpm 来处理,之后获取响应后发送给客户端。

二、下载

使用 MariaDB 是因为没用过,对 MySQL8.0 的安装已经足够熟悉了。

WordPress下载Wordpress中文版下载

2.1 MariaDB

官方yum源生成

  1. 新建 MariaDB的yum源
1
vi /etc/yum.repos.d/MariaDB.repo
1
2
3
4
5
6
7
8
9
10
11
# MariaDB 10.11 RedHatEnterpriseLinux repository list - created 2026-01-08 01:55 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/10.11/rhel/$releasever/$basearch
baseurl = https://mirrors.xtom.com/mariadb/yum/10.11/rhel/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://mirrors.xtom.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
  1. 替换URL
1
http://yum.mariadb.org
1
2
https://mirrors.aliyun.com/mariadb/yum
https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum
  1. 安装
1
sudo yum install MariaDB-server MariaDB-client
  1. 启动并开机自启
1
systemctl enable --now mariadb
  1. 运行安全脚本,都默认选项即可。
1
mariadb-secure-installation
  • Enter current password for root (enter for none): (直接回车,因为刚装好没密码)
  • Switch to unix_socket authentication [Y/n] (输入 Y)
  • Change the root password? [Y/n] (输入 Y,然后设置你的新 Root 密码)
  • Remove anonymous users? [Y/n] (输入 Y,移除匿名用户)
  • Disallow root login remotely? [Y/n] (输入 Y,建议禁止 Root 远程登录,后面我们会创建单独用户)
  • Remove test database and access to it? [Y/n] (输入 Y,移除测试库)
  • Reload privilege tables now? [Y/n] (输入 Y,刷新权限)
  1. 修改编码集
1
vi /etc/my.cnf.d/server.cnf
1
2
3
4
5
6
7
8
9
10
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# 让新建库、新建表默认继承
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake

# 可选,但强烈建议
max_connections = 500
1
vi /etc/my.cnf.d/client.cnf
1
2
[client]
default-character-set = utf8mb4
1
vi /etc/my.cnf.d/mysql-clients.cnf
1
2
[mysql]
default-character-set = utf8mb4
1
systemctl restart mysqld
1
mysql -uroot -p'Root21..'
1
2
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

2.2 PHP

  1. 安装Remi 仓库
1
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
  1. 确认仓库存在,查看php可用版本
1
ls /etc/yum.repos.d/ | grep remi
  1. 启用 php8.3
1
yum install -y yum-utils
1
yum-config-manager --enable remi-php83
  1. 安装PHP8.3及常用扩展
1
2
3
4
yum install -y \
php php-fpm php-cli php-common \
php-mysqlnd php-opcache \
php-gd php-mbstring php-xml php-json php-bcmath php-intl php-zip php-curl
  1. 验证
1
php -v
  1. 禁用无关的库
1
2
3
4
5
6
7
8
9
10
11
yum-config-manager --disable remi-php54
yum-config-manager --disable remi-php55
yum-config-manager --disable remi-php56
yum-config-manager --disable remi-php70
yum-config-manager --disable remi-php71
yum-config-manager --disable remi-php72
yum-config-manager --disable remi-php73
yum-config-manager --disable remi-php74
yum-config-manager --disable remi-php80
yum-config-manager --disable remi-php81
yum-config-manager --disable remi-php82

2.3 Nginx

CentOS7安装Nginx

三、初始化

3.1 MariaDB

  1. 登录
1
mysql -uroot -p'Root21..'
  1. 创建数据库
1
2
3
CREATE DATABASE wordpress
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
  1. 创建普通用户
1
2
CREATE USER 'wpuser'@'localhost'
IDENTIFIED BY 'Wpuser21..';
  1. 授权
1
2
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
  1. 验证授权
1
SHOW GRANTS FOR 'wpuser'@'localhost';

3.2 Nginx

1
vim /etc/nginx/conf.d/wordpress.conf
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
server {
listen 80;
server_name wp.bravexist.cn;
root /www/wwwroot/wordpress;
index index.php index.html;

access_log /var/log/nginx/wordpress.access.log;
error_log /var/log/nginx/wordpress.error.log;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* \.(jpg|jpeg|gif|png|css|js|ico|svg|woff|woff2|ttf|eot)$ {
expires 30d;
access_log off;
}

location ~ /\. {
deny all;
}
}

3.3 PHP-FPM

  1. PHP-FPM 运行用户和 Nginx 一致
1
vi /etc/php-fpm.d/www.conf

​ 关键配置

1
2
3
4
5
6
7
user = nginx
group = nginx

listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
  1. PHP 基础参数
1
vi /etc/php.ini
1
2
3
4
5
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
date.timezone = Asia/Shanghai
  1. 重启 php-fpm
1
2
systemctl restart php-fpm
systemctl status php-fpm

3.4 Wordpress

1
wget https://wordpress.org/wordpress-6.9.tar.gz

或 中文包

1
wget https://cn.wordpress.org/wordpress-6.9-zh_CN.tar.gz
1
mkdir -pv /www/wwwroot
1
tar xf wordpress-6.9.tar.gz -C /www/wwwroot
1
chown -R nginx.nginx /www/wwwroot/wordpress

四、启动

1
systemctl start nginx
1
nginx -t && nginx -s reload

设置 hosts 解析后访问

1
http://wp.bravexist.cn