debian

Debian LNMP环境配置步骤是什么

小樊
34
2025-04-07 21:40:31
栏目: 智能运维

在Debian系统上配置LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境是一个相对直接的过程,以下是详细的步骤:

安装Nginx

  1. 更新包列表:
sudo apt update
  1. 安装Nginx:
sudo apt install nginx
  1. 启动Nginx服务并设置开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
  1. 验证Nginx是否正常运行:
sudo systemctl status nginx

安装MariaDB

  1. 安装MariaDB:
sudo apt install mariadb-server mariadb-client
  1. 初始化MariaDB:
sudo mysql_secure_installation
  1. 启动MariaDB服务并设置开机自启动:
sudo systemctl start mariadb
sudo systemctl enable mariadb
  1. 验证MariaDB是否正常运行:
sudo systemctl status mariadb

安装PHP及其扩展

  1. 安装PHP及其常用扩展:
sudo apt install php php-fpm php-mysql php-opcache php-cli php-gd php-curl
  1. 配置PHP-FPM:
    • 编辑PHP-FPM配置文件:
    sudo vim /etc/php/7.4/fpm/pool.d/www.conf
    
    • 修改以下行以允许访问所有文件:
    security.limit_extensions = .php .php3 .php4 .php5 .php7 .html .js .css .jpg .jpeg .gif .png .htm
    
    • 重启PHP-FPM服务:
    sudo systemctl restart php7.4-fpm
    

配置Nginx以处理PHP请求

  1. 编辑Nginx默认站点配置文件:
sudo vim /etc/nginx/sites-available/default
  1. 找到以下部分并修改:
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
  1. 测试Nginx配置文件是否有语法错误:
sudo nginx -t
  1. 重启Nginx服务以应用更改:
sudo systemctl restart nginx

验证LNMP环境

  1. 创建一个PHP信息文件以测试环境:
sudo vim /var/www/html/info.php

添加以下内容:

<?php
phpinfo();
?>
  1. 保存并退出编辑器。
  2. 在浏览器中访问服务器的IP地址或域名,加上/info.php,例如:http://your_server_ip/info.php
  3. 如果看到PHP信息页面,说明LNMP环境配置成功。

0
看了该问题的人还看了