当前位置: 首页 > 编程文章 >

Ubuntu 20.04 源码编译搭建 LNMP (Nginx+MariaDB+PHP) 环境详解

Ubuntu 20.04 源码编译搭建 LNMP (Nginx+MariaDB+PHP) 环境详解

作者:叶澄时间:2026-05-31 09:10:20浏览:800

本文记录在 Ubuntu 20.04 系统上通过源码编译方式搭建 LNMP(Nginx、MariaDB、PHP)开发环境的完整流程。针对 CentOS 用户迁移至 Ubuntu 时的差异,详细说明了 Nginx、MariaDB 和 PHP 7.3.18 的安装步骤及 systemd 服务配置。重点解决了 PHP 编译过程中因 libfreetype6-dev 版本更新导致 freetype-config 缺失的错误,提供使用 pkg-config 替代的解决方案,并补充了 Xdebug 和 APCu 扩展的安装方法。

本文目录展开

Ubuntu 20.04 搭建 LNMP 环境概述

简介

此前曾在 CentOS 7 上部署过相关环境,近期将个人开发环境切换至 Ubuntu 20.04。鉴于两者在包管理及配置细节上存在差异,特此记录在 Ubuntu 下搭建该环境的具体步骤,以供学习参考。

前期准备

所需软件版本如下: PHP:7.3.18 Nginx:1.18.0 MariaDB:10.5.4

解压下载的软件包文件:


tar zxf php-7.3.18.tar.gz
tar zxf mariadb-10.5.4.tar.gz
tar zxf nginx-1.18.0.tar.gz

安装 Nginx


sudo groupadd -r nginx && sudo useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx nginx
sudo apt install -y libpcre3-dev zlib1g-dev
cd /home/allen/下载/nginx-1.18.0
./configure --user=nginx --group=nginx
make -j 4 && sudo make install
sudo /usr/local/nginx/sbin/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
sudo mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back
sudo vim /usr/local/nginx/conf/nginx.conf

user nginx;
worker_processes 4;
events {
  worker_connections 1024;
}
http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile   on;
  keepalive_timeout 65;
  log_format main '$remote_addr || $remote_user || $time_local || $request || $status || $body_bytes_sent || $http_referer || $http_user_agent || $http_x_forwarded_for';
  include /data/www/*/*.conf;
} 

sudo chown -R nginx:nginx /usr/local/nginx

sudo vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx
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 quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target


sudo systemctl enable nginx

#Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.

reboot

重启系统后检查 Nginx 是否成功启动

安装 MariaDB


sudo rm -rf /etc/mysql
sudo apt remove -y mysql-common
sudo apt autoremove -y
sudo apt install -y cmake libncurses5-dev libgnutls28-dev
sudo groupadd -r mysql && sudo useradd -r -g mysql -s /sbin/nologin -d /usr/local/mariadb mysql
sudo mkdir -p /data/db /var/log/mariadb
cd /home/allen/下载/mariadb-10.5.4/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb -DMYSQL_DATADIR=/data/db -DSYSCONFDIR=/etc -DWITHOUT_TOKUDB=1 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci
make -j 4 && sudo make install
sudo /usr/local/mariadb/scripts/mysql_install_db --user=mysql --datadir=/data/db
sudo vim /etc/my.cnf

[mysqld]
datadir = /data/db
socket = /tmp/mysql.sock
# 建议禁用符号链接,防止各类安全风险
symbolic-links = 0
collation-server = utf8mb4_general_ci
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4

[mysql]
default-character-set = utf8mb4

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysqld_safe]
log-error = /var/log/mariadb/mariadb.log
pid-file = /var/run/mariadb/mariadb.pid

sudo cp /usr/local/mariadb/support-files/mysql.server /etc/init.d/mariadb

sudo vim /etc/profile.d/mariadb.sh
export PATH=$PATH:/usr/local/mariadb/bin/

sudo chmod 0777 /etc/profile.d/mariadb.sh
source /etc/profile.d/mariadb.sh

sudo /etc/init.d/mariadb start
#Starting mariadb (via systemctl): mariadb.service.

sudo /usr/local/mariadb/bin/mysql_secure_installation 	 

#NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
#   SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

#In order to log into MariaDB to secure it, we'll need the current
#password for the root user. If you've just installed MariaDB, and
#haven't set the root password yet, you should just press enter here.

#Enter current password for root (enter for none): 
#OK, successfully used password, moving on...

#Setting the root password or using the unix_socket ensures that nobody
#can log into the MariaDB root user without the proper authorisation.

#You already have your root account protected, so you can safely answer 'n'.

#Switch to unix_socket authentication [Y/n] y
#Enabled successfully!
#Reloading privilege tables..
# ... Success!


#You already have your root account protected, so you can safely answer 'n'.

#Change the root password? [Y/n] y
#New password: 
#Re-enter new password: 
#Password updated successfully!
#Reloading privilege tables..
# ... Success!


#By default, a MariaDB installation has an anonymous user, allowing anyone
#to log into MariaDB without having to have a user account created for
#them. This is intended only for testing, and to make the installation
#go a bit smoother. You should remove them before moving into a
#production environment.

#Remove anonymous users? [Y/n] y
# ... Success!

#Normally, root should only be allowed to connect from 'localhost'. This
#ensures that someone cannot guess at the root password from the network.

#Disallow root login remotely? [Y/n] n
# ... skipping.

#By default, MariaDB comes with a database named 'test' that anyone can
#access. This is also intended only for testing, and should be removed
#before moving into a production environment.

#Remove test database and access to it? [Y/n] y
# - Dropping test database...
# ... Success!
# - Removing privileges on test database...
# ... Success!

#Reloading the privilege tables will ensure that all changes made so far
#will take effect immediately.

#Reload privilege tables now? [Y/n] y
# ... Success!

#Cleaning up...

#All done! If you've completed all of the above steps, your MariaDB
#installation should now be secure.

#Thanks for using MariaDB!

sudo systemctl enable mariadb 

sudo chown mysql:mysql -R /usr/local/mariadb /data/db /var/log/mariadb

reboot

重启系统后检查 MariaDB 是否成功启动

安装 PHP


sudo apt install -y libxml2-dev libssl-dev libbz2-dev libcurl4-gnutls-dev libjpeg-dev libpng-dev pkg-config libxslt1-dev libzip-dev libfreetype6-dev libfontconfig1-dev autoconf
sudo groupadd -r php && sudo useradd -r -g php -s /sbin/nologin -d /usr/local/php php
sudo vim /etc/sudoers
php   ALL=(ALL:ALL) ALL
cd /home/allen/下载/php-7.3.18/


./configure --prefix=/usr/local/php \--exec-prefix=/usr/local/php --with-fpm-user=php --with-fpm-group=php --enable-zip --with-curl --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-bcmath --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-fpm --with-freetype-dir --with-gd --with-libxml-dir --with-pcre-regex --enable-libxml --enable-zip --with-png-dir --with-jpeg-dir

编译过程中遇到一个错误:

configure: error: freetype-config not found.

解决方法: 参考链接:https://www.doopsky.com/ops/981.html

该问题源于 Ubuntu 19.04 中 apt-get 安装的 libfreetype6-dev 版本为 2.9.1-3。 http://changelogs.ubuntu.com/changelogs/pool/main/f/freetype/freetype_2.9.1-3/changelog Changelog 中注明:

The `freetype-config' script is no longer installed by default (Closes: #871470, #886461). All packages depending on libfreetype6-dev should use pkg-config to find the relevant CFLAGS and libraries. freetype-config 已被 pkg-config 取代,新版本依赖 pkg-config 来管理 CFLAGS 和库文件。

因此,解决思路是用 pkg-config 替代 freetype-config。系统中已安装 pkg-config,具体操作如下:


cat ./configure | grep "freetype-config" -n
34847:   if test -f "$i/bin/freetype-config"; then
34849:    FREETYPE2_CONFIG="$i/bin/freetype-config"
34855:   as_fn_error $? "freetype-config not found." "$LINENO" 5
36568:   if test -f "$i/bin/freetype-config"; then
36570:    FREETYPE2_CONFIG="$i/bin/freetype-config"
36576:   as_fn_error $? "freetype-config not found." "$LINENO" 5

sed -i "s/freetype-config/pkg-config/g" ./configure

cat ./configure | grep "FREETYPE2_CONFIG --cflags" -n
34858:  FREETYPE2_CFLAGS=`$FREETYPE2_CONFIG --cflags`
36579:  FREETYPE2_CFLAGS=`$FREETYPE2_CONFIG --cflags`

sed -i "s/FREETYPE2_CONFIG --cflags/FREETYPE2_CONFIG freetype2 --cflags/g" ./configure

cat ./configure | grep "FREETYPE2_CONFIG --libs" -n 
34859:  FREETYPE2_LIBS=`$FREETYPE2_CONFIG --libs`
36580:  FREETYPE2_LIBS=`$FREETYPE2_CONFIG --libs`

sed -i "s/FREETYPE2_CONFIG --libs/FREETYPE2_CONFIG freetype2 --libs/g" ./configure

cat ./ext/gd/config.m4 | grep "freetype-config" -n
188:   if test -f "$i/bin/freetype-config"; then
190:    FREETYPE2_CONFIG="$i/bin/freetype-config"
196:   AC_MSG_ERROR([freetype-config not found.])

sed -i "s/freetype-config/pkg-config/g" ./ext/gd/config.m4

重新执行编译安装即可成功


Thank you for using PHP.

make -j 4 && sudo make install

sudo cp php.ini-production /usr/local/php/lib/php.ini

sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

vim /usr/local/php/etc/php-fpm.d/www.conf
listen.mode = 0666
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 10000
slowlog = log/$pool.log.slow
rlimit_files = 1024

sudo vim /etc/profile.d/php.sh
export PATH=$PATH:/usr/local/php/bin/

sudo chmod 0777 /etc/profile.d/php.sh && source /etc/profile.d/php.sh

sudo cp sapi/fpm/php-fpm.service /etc/systemd/system/php.service

安装 Xdebug

下载 Xdebug http://pecl.php.net/get/xdebug-2.9.6.tgz


cd /home/allen/下载
tar zxf xdebug-2.9.6.tgz
cd xdebug-2.9.6
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make -j 4 && sudo make install

安装 APCu

下载 APCu http://pecl.php.net/get/apcu-5.1.18.tgz


cd /home/allen/下载
tar zxf apcu-5.1.18.tgz
cd apcu-5.1.18
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make -j 4 && sudo make install

编辑 PHP.INI 配置文件


sudo 
vim /usr/local/php/lib/php.ini

date.timezone = Asia/Shanghai
expose_php = off
max_execution_time = 0
memory_limit = 4096M
display_errors = On
cgi.fix_pathinfo=0
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20260731/"
extension=pgsql
extension=apcu

zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20260731/xdebug.so
[xdebug]
xdebug.var_display_max_children=10240
xdebug.var_display_max_data=20480
xdebug.var_display_max_depth=50

启动服务


sudo systemctl enable php-fpm

sudo chown -R mysql:mysql /usr/local/mariadb
sudo chown -R nginx:nginx /usr/local/nginx
sudo chown -R php:php /usr/local/php
reboot 

至此,关于在 Ubuntu 20.04 上搭建 LNMP 环境的方法步骤介绍完毕。如需了解更多相关内容,请搜索本站历史文章或继续阅读后续内容,感谢支持!

上一篇
西软FOXHIS数据库增量备份与恢复方案详解

针对西软FOXHIS酒店管理系统,本文介绍一种基于Sybase数据库的增量备份与恢复方案。通过配置sp_dboption参数、编写Shell调用SQL脚本以及设置Crontab定时任务,实现每12小时全库备份、每小时差异备份的策略。该方案可将数据丢失风险控制在1小时以内,相比传统每日两次全备更安全,且无需高昂的高可用集群成本,适合大多数酒店IT人员实施。

下一篇
LuManager 国产 Linux/Unix 服务器管理面板推荐

LuManager 是一款由国人开发的 Linux/Unix 平台服务器管理面板,提供全中文直观操作界面。相比国外收费面板,它拥有免费版和商业版两种选择,且自发布以来未发生过安全漏洞。该面板内存占用较低,约 70-100M,适合 256-512M 内存的 VPS。通过一行命令即可快速完成安装,支持编译与快速两种模式,是个人用户和主机商管理服务器的实用工具。

相关文章

更多
PHP编译参数详解与最佳实践指南

本文详细解析了PHP源码编译过程中常用的configure参数,涵盖Apache整合、数据库连接、图形处理及CGI/FPM模式等核心配置。同时指出PDO优于mysqli的性能优势,强调在产品环境中关闭magic-quotes等不安全选项,并根据服务器负载选择合适的OPCode缓存方案以实现最佳性能。

2025.12.19 957
编译安装PHP 5.2.14配合FPM与Memcached扩展详解

本文详细记录了在Linux环境下编译安装PHP 5.2.14并启用FPM支持的过程,同时整合了Memcache客户端扩展及Memcached服务端组件。通过配置libevent库、应用fpm补丁以及调整php.ini,实现了高性能的PHP-FPM运行环境与分布式缓存功能的完整搭建。

2026.02.13 946
Nginx实现动静分离的完整部署方案

本文介绍基于Redhat8和CentOS7环境的Nginx动静分离部署方法。通过在三台主机上分别配置Nginx反向代理、LNMP动态服务集群以及HTTPD静态资源服务器,实现请求的智能分发。关闭防火墙并配置SELinux后,利用Nginx的location规则将PHP请求转发至后端LNMP节点,静态文件由HTTPD节点处理,从而提升系统性能与扩展性。

2025.05.29 938
CentOS LNMP 服务器部署标准指南

本文介绍在 CentOS 系统上从零开始部署 LNMP(Linux, Nginx, MySQL, PHP)环境的完整流程。内容涵盖系统初始化、依赖包安装、Nginx 编译配置、PHP-FPM 集成及 Memcached 扩展安装,旨在为新手提供一份可操作的标准化部署方案。

2025.03.26 928
LNMP环境下phpinfo无法显示的排查与修复

本文详细记录了在CentOS 7 LNMP环境中遇到phpinfo页面无法显示的问题排查过程。通过验证Nginx与PHP-FPM服务状态,重点分析了Nginx主机配置中关于index.php默认首页及fastcgi_pass参数设置,并检查了PHP-FPM监听端口配置。最终通过修正配置并重启服务成功解决该问题,为类似环境提供参考。

2025.08.29 876
LNMP环境下php-fpm CPU占用100%的排查与解决

在LNMP架构中,php-fpm进程突发导致服务器CPU占用率飙升至100%,严重影响服务稳定性。通过top命令定位高占用进程后,初步调整php-fpm进程数配置未能彻底解决问题。进一步排查发现eAccelerator缓存组件异常占用大量CPU资源。通过修改php.ini禁用该扩展并重启LNMP服务,系统负载逐渐恢复正常。本文记录了从监控报警到定位根因及实施优化的完整过程。

2025.03.22 857

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。