服务器 LNMP 架构部署:CentOS 9 下 Nginx+MySQL 8+PHP 8.2 源码编译安装
·
以下是在 CentOS 9 上源码编译安装 LNMP 架构(Nginx + MySQL 8 + PHP 8.2)的完整指南,所有步骤均经过验证:
一、环境准备
# 更新系统
dnf update -y
# 安装基础编译工具
dnf groupinstall "Development Tools" -y
dnf install epel-release -y
dnf install cmake bison flex libtool autoconf openssl-devel pcre-devel zlib-devel \
libxml2-devel libcurl-devel gd-devel sqlite-devel oniguruma-devel -y
二、编译安装 Nginx
1. 下载源码
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
2. 编译配置
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream
3. 编译安装
make -j$(nproc)
make install
4. 创建系统服务
创建 /etc/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/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
启动服务:
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
三、编译安装 MySQL 8
1. 安装依赖
dnf install ncurses-devel openssl-devel libtirpc-devel rpcgen -y
2. 下载源码
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.33.tar.gz
tar zxvf mysql-8.0.33.tar.gz
cd mysql-8.0.33
3. 编译配置
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_SSL=system \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_0900_ai_ci \
-DENABLED_LOCAL_INFILE=1
4. 编译安装
make -j$(nproc)
make install
5. 初始化数据库
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /usr/local/mysql
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
bin/mysql_ssl_rsa_setup
6. 创建系统服务
创建 /etc/systemd/system/mysql.service:
[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
[Install]
WantedBy=multi-user.target
启动服务:
cp support-files/mysql.server /etc/init.d/mysql
systemctl daemon-reload
systemctl start mysql
systemctl enable mysql
四、编译安装 PHP 8.2
1. 下载源码
wget https://www.php.net/distributions/php-8.2.10.tar.gz
tar zxvf php-8.2.10.tar.gz
cd php-8.2.10
2. 编译配置
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-zlib \
--with-curl \
--with-gd \
--enable-mbstring \
--enable-opcache
3. 编译安装
make -j$(nproc)
make install
4. 配置文件
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
5. 创建系统服务
创建 /etc/systemd/system/php-fpm.service:
[Unit]
Description=PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
启动服务:
useradd -s /sbin/nologin nginx
systemctl daemon-reload
systemctl start php-fpm
systemctl enable php-fpm
五、配置 Nginx 支持 PHP
编辑 /usr/local/nginx/conf/nginx.conf:
server {
listen 80;
server_name localhost;
root /usr/local/nginx/html;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
测试配置并重启:
/usr/local/nginx/sbin/nginx -t
systemctl restart nginx
六、验证安装
- 创建测试文件:
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php
-
访问
http://服务器IP/info.php应显示 PHP 信息页 -
检查 MySQL 连接:
/usr/local/php/bin/php -r "new mysqli('localhost', 'root', '', 'mysql');"
常见问题解决
- 依赖缺失:根据编译错误提示安装对应开发包
- 端口冲突:确保 80(Nginx)、3306(MySQL)、9000(PHP-FPM)端口空闲
- 权限问题:所有服务使用专用用户运行(Nginx:
nginx,MySQL:mysql) - 防火墙:开放端口:
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reload
提示:生产环境建议配置 SELinux 规则和 HTTPS 加密。源码安装路径为
/usr/local/,所有二进制文件需使用完整路径调用(如/usr/local/php/bin/php)。
更多推荐


所有评论(0)