LNMP服务器快速搭建教程

CentOS Stream 9 上搭建 LNMP 环境并使用 PHP 8.3

cat /etc/os-release


一、系统准备

sudo dnf update -y
sudo dnf install epel-release -y
sudo dnf groupinstall "Development Tools" -y

防火墙:

sudo systemctl enable firewalld
sudo systemctl start firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

二、安装 Nginx

sudo dnf install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
curl http://localhost

三、安装 MariaDB(或 MySQL)

sudo dnf install mariadb-server mariadb -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_secure_installation

四、安装 PHP 8.3

  1. 添加 Remi 仓库
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.3 -y
  1. 安装 PHP 及常用扩展
sudo dnf install php php-fpm php-cli php-mysqlnd php-json php-gd php-mbstring php-curl php-xml php-intl php-zip -y
  1. 配置 PHP-FPM
sudo vi /etc/php-fpm.d/www.conf

修改:

user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen = /run/php-fpm/www.sock
  1. 启动 PHP-FPM
sudo systemctl enable php-fpm
sudo systemctl start php-fpm

五、配置 Nginx 支持 PHP

编辑 Nginx 配置 /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    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;
    }

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

测试配置并重载:

sudo nginx -t
sudo systemctl reload nginx

六、测试 PHP

echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php

浏览器访问:http://服务器IP/info.php,确认 PHP 8.3 已运行。


七、可选:安装 Redis

sudo dnf install redis -y
sudo systemctl enable redis
sudo systemctl start redis
redis-cli ping  #

八、可选:node.js

# 安装 Node.js (使用 nvm 或系统包管理器)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20

# 安装前端依赖并构建
npm install
npm run build

九、可选:安装 Composer 依赖

# 如果没有安装 Composer,先安装
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# 安装项目依赖(生产环境使用 --no-dev)
composer install --no-dev --optimize-autoloader

One comment

回复 jiuzhouCancel Reply

您的邮箱地址不会被公开。 必填项已用 * 标注