在Debian系统上配置LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境,可以按照以下步骤进行:
首先,确保你的系统包是最新的:
sudo apt update
sudo apt upgrade -y
安装Nginx作为Web服务器:
sudo apt install nginx -y
启动并启用Nginx服务:
sudo systemctl start nginx
sudo systemctl enable nginx
安装MySQL或MariaDB作为数据库服务器。这里以MariaDB为例:
sudo apt install mariadb-server -y
启动并启用MariaDB服务:
sudo systemctl start mariadb
sudo systemctl enable mariadb
运行安全脚本以提高安全性:
sudo mysql_secure_installation
安装PHP及其常用扩展:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
启动并启用PHP-FPM服务:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
将以下内容添加到server
块中:
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:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存并退出编辑器,然后测试Nginx配置:
sudo nginx -t
重新加载Nginx以应用更改:
sudo systemctl reload nginx
创建一个简单的PHP文件来测试PHP环境:
sudo nano /var/www/html/index.php
添加以下内容:
<?php
phpinfo();
?>
保存并退出编辑器,然后在浏览器中访问你的服务器IP地址或域名,你应该能看到PHP信息页面。
如果你使用的是UFW防火墙,可以允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
为了提高安全性,可以安装Let’s Encrypt提供的免费SSL证书:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
按照提示完成SSL证书的安装和配置。
通过以上步骤,你就可以在Debian系统上成功配置LNMP环境了。