LNMP部署及应用

LNMP部署及应用

概念

LNMP 架构概述

  • LNMP 由 Linux(系统底层)、Nginx(Web 服务器)、MySQL(关系型数据库)、PHP-FPM(动态页面解析)组成,适用于独立主机搭建生产环境。
  • Nginx 优势:资源占用少、并发支持强,可作为 Web 服务器、负载均衡服务器或邮件代理服务器,支持平滑升级与配置重载。
  • MySQL 特点:体积小、速度快、开源免费,采用关系型存储,适合中小型网站数据库需求。
  • PHP-FPM 作用:处理 Nginx 无法解析的动态页面请求,实现动态内容交互。

部署步骤

安装 Nginx 服务
  • 先安装 pcre-devel、zlib-devel 等依赖包,创建专用 nginx 运行用户(禁止 Shell 登录)。
  • 编译安装 Nginx 至 /usr/local/nginx,配置运行用户、状态统计模块,优化路径并创建软链接。
  • 检查配置文件有效性,启动服务并验证监听状态,添加系统服务支持 systemctl 管理。
安装 MySQL 服务
  • 安装 ncurses、cmake 等依赖包,创建 mysql 运行用户。
  • 编译安装 MySQL 至 /usr/local/mysql,调整数据库目录权限,配置 /etc/my.cnf 文件(指定端口、字符集、数据目录等)。
  • 设置环境变量,初始化数据库,配置系统服务并设置开机自启,最后修改 root 密码并登录验证。
安装配置 PHP 解析环境
  • 安装 libxml2-devel、openssl-devel 等大量依赖包及 epel 源,编译安装 PHP 至 /usr/local/php。
  • 配置 php.ini 核心文件(指定 MySQL 套接字、时区),优化 PHP-FPM 配置(复制默认配置文件、设置 PID)。
  • 配置 Nginx 支持 PHP,修改 nginx.conf 文件关联 PHP-FPM 端口,创建测试页面验证解析及数据库连接功能。
部署 Discuz!社区论坛
  • 解压论坛安装包至 Nginx 网站根目录,调整相关目录权限(config、data 等目录设为 777 权限)。
  • 通过浏览器访问安装页面,填写数据库信息(库名 bbs、用户名 bbsuser)及管理员账号,完成安装。
  • 安装后删除或锁定 install 目录,通过前台地址访问论坛、后台地址管理论坛。

LNMP部署

环境准备

nginx配置
bash 复制代码
[root@localhost ~]# yum -y install pcre-devel zlib-devel

#创建程序用户
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# id nginx
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)

#编译安装nginx
[root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# tar xzvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2/

[root@localhost nginx-1.12.2]# 
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

[root@localhost nginx-1.12.2]# make && make install


#路径优化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

#查看配置文件语法ok
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

#查看有哪些文件
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp


[root@localhost sbin]# vim /lib/systemd/system/nginx.service
[root@localhost sbin]# 
[root@localhost sbin]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost sbin]# cat /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target




[root@localhost sbin]# systemctl start nginx.service
[root@localhost sbin]# systemctl status nginx.service 
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2025-11-07 11:08:40 CST; 6s ago
  Process: 1953 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 1954 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1954 nginx: master process /usr/local/nginx/sbin/nginx
           └─1955 nginx: worker process

Nov 07 11:08:40 localhost.localdomain systemd[1]: Starting nginx...
Nov 07 11:08:40 localhost.localdomain systemd[1]: Started nginx.
[root@localhost sbin]# systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost sbin]# netstat -anutp |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1954/nginx: master  


#去网站测试nginx
mysql配置
bash 复制代码
[root@localhost ~]# 
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake


#程序用户
[root@localhost sbin]# useradd -s /sbin/nologin  mysql
[root@localhost sbin]# id mysql
uid=1002(mysql) gid=1002(mysql) groups=1002(mysql)



[root@localhost opt]# tar xzvf mysql-boost-5.7.20.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  Downloads             mysql-5.7.20               nginx-1.12.2.tar.gz  Templates
Desktop          initial-setup-ks.cfg  mysql-boost-5.7.20.tar.gz  Pictures             Videos
Documents        Music                 nginx-1.12.2               Public


#初始化数据库
[root@localhost mysql-5.7.20]# 
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1


[root@localhost mysql-5.7.20]# make -j4
[root@localhost mysql-5.7.20]# make install



#数据库目录进行权限调整
[root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.7.20]# vim /etc/my.cnf
[root@localhost mysql-5.7.20]# 
[root@localhost mysql-5.7.20]# 
[root@localhost mysql-5.7.20]# cat /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost mysql-5.7.20]# chown mysql:mysql /etc/my.cnf



#设置环境变量
[root@localhost mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql-5.7.20]# echo 'export PATH' >> /etc/profile
[root@localhost mysql-5.7.20]# source /etc/profile



#初始化
[root@localhost mysql-5.7.20]# cd /usr/local/mysql/
[root@localhost mysql]# bin/mysqld \
> --initialize-insecure \
> --user=mysql \
> --basedir=/usr/local/mysql \
> --datadir=/usr/local/mysql/data
2025-11-07T05:37:43.778469Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2025-11-07T05:37:44.104945Z 0 [Warning] InnoDB: New log files created, LSN=45790
2025-11-07T05:37:44.155343Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2025-11-07T05:37:44.211495Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e274c902-bb9b-11f0-acbe-000c29fff886.
2025-11-07T05:37:44.212358Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2025-11-07T05:37:44.213156Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.



#复制执行脚本
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/


[root@localhost mysql]# systemctl enable mysqld --now
[root@localhost mysql]# netstat -anpt | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      22194/mysqld     



#设置密码
[root@localhost mysql]# mysqladmin -u root -p password
Enter password: 
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.




#登录
[root@localhost mysql]# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
PHP配置
bash 复制代码
#安装环境包
[root@localhost ~]# 
yum -y install \
libxml2 \
libxml2-devel \
openssl \
openssl-devel \
bzip2 \
bzip2-devel \
libcurl \
libcurl-devel \
libjpeg \
libjpeg-devel \
libpng \
libpng-devel \
freetype \
freetype-devel \
gmp \
gmp-devel \
libmcrypt \
libmcrypt-devel \
readline \
readline-devel \
libxslt \
libxslt-devel \
zlib \
zlib-devel \
glibc \
glibc-devel \
glib2 \
glib2-devel \
ncurses \
curl \
gdbm-devel \
db4-devel \
libXpm-devel \
libX11-devel \
gd-devel \
gmp-devel \
expat-devel \
xmlrpc-c \
xmlrpc-c-devel \
libicu-devel \
libmcrypt-devel \
libmemcached-devel




#安装epel源
yum install -y epel-release

yum -y install libmcrypt \
libmcrypt-devel


#编译安装
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg       nginx-1.12.2         Pictures
Desktop          Music                      nginx-1.12.2.tar.gz  Public
Documents        mysql-5.7.20               php-7.1.10           Templates
Downloads        mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2   Videos
[root@localhost ~]# cd php-7.1.10/


[root@localhost php-7.1.10]# 
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache


[root@localhost php-7.1.10]# make -j4
[root@localhost php-7.1.10]# make install


[root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini 
/1170 mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai

#验证安装的模块
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
json
libxml
mbstring
mcrypt
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib

[Zend Modules]


#配置及优化FPM模块
[root@localhost php-7.1.10]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@localhost etc]# cp  php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# cd /usr/local/php/etc/
[root@localhost etc]# vim php-fpm.conf
pid = run/php-fpm.pid



#启动
[root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
[root@localhost etc]# netstat -anpt | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      46300/php-fpm: mast 

[root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/


#查看正在进行的进程
[root@localhost etc]# ps aux | grep -c "php-fpm"
4

配置nginx支持PHP

bash 复制代码
[root@localhost etc]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name; ####注意目录名称
            include        fastcgi_params;
}



[root@localhost etc]# vim /usr/local/nginx/html/index.php
[root@localhost etc]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>


# 在网页测试"http://192.168.100.41/index.php"

测试数据库

bash 复制代码
[root@localhost etc]# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database bbs;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> grant all on bbs.* to 'bbsuser'@'%' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on bbs.* to 'bbsuser'@'localhost' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

测试

bash 复制代码
[root@localhost etc]# vim /usr/local/nginx/html/index.php
[root@localhost etc]# cat /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('192.168.100.68','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
[root@localhost etc]# systemctl restart nginx

#去网站访问测试

LNMP应用部署

安装论坛

bash 复制代码
#把所需文件复制到根目录下面
[root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# ls
anaconda-ks.cfg          initial-setup-ks.cfg       nginx-1.24.0.tar.gz  Templates
Desktop                  Music                      php-7.1.10           Videos
Discuz_X3.4_SC_UTF8.zip  mysql-5.7.20               php-7.1.10.tar.bz2
Documents                mysql-boost-5.7.20.tar.gz  Pictures
Downloads                nginx-1.24.0               Public

[root@localhost ~]# unzip Discuz_X3.4_SC_UTF8.zip  -d /tmp

[root@localhost ~]# cd /tmp/dir_SC_UTF8/
[root@localhost dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs/
ata/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
[root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/bbs/
[root@localhost bbs]# chown -R root:nginx ./config/
[root@localhost bbs]# chown -R root:nginx ./data/
[root@localhost bbs]# chown -R root:nginx ./uc_client/
[root@localhost bbs]# chown -R root:nginx ./uc_server/
[root@localhost bbs]# 
[root@localhost bbs]# chmod -R 777 ./config/
[root@localhost bbs]# chmod -R 777 ./data/
[root@localhost bbs]# chmod -R 777 ./uc_client/
[root@localhost bbs]# chmod -R 777 ./uc_server/

#去浏览器安装







相关推荐
Mark_Hide2 小时前
学习笔记7
笔记·学习
省四收割者2 小时前
GitHub Action工作流语法
笔记·github
d111111111d3 小时前
STM32外设学习--TIM定时器--编码器接口(程序)
笔记·stm32·嵌入式硬件·学习
Anesthesia丶3 小时前
UV工具学习笔记
笔记·学习·uv
x_lrong3 小时前
Linux虚拟机配置jupyter环境并在宿主机访问
linux·运维·笔记·jupyter·虚拟机
蒙奇D索大3 小时前
【11408学习记录】考研数学核心考点精讲:二维随机变量(离散与连续)全面解析
笔记·学习·考研·概率论·改行学it
爱奥尼欧3 小时前
【Linux笔记】网络部分——数据链路层mac-arp
linux·网络·笔记
清风wxy4 小时前
Duilib_CEF桌面软件实战之Duilib编译与第一个界面程序
c++·笔记·ui·mfc
nenchoumi31194 小时前
ROS2 Humble 笔记(七)标准与自定义 Interface 接口
笔记·机器人·ros2