在Debian上使用LNMP(Linux, Nginx, MySQL/MariaDB, PHP)搭建网站是一个相对简单的过程。以下是详细的步骤:
首先,确保你的系统是最新的:
sudo apt update
sudo apt upgrade -y
安装Nginx作为Web服务器,并启动服务:
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
你可以选择安装MySQL或MariaDB。这里以MariaDB为例:
sudo apt install mariadb-server mariadb-client -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
安装PHP及其常用扩展:
sudo apt install php-fpm php-mysql -y
编辑PHP-FPM的配置文件:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
找到以下行并取消注释:
listen = /run/php/php7.4-fpm.sock
重启PHP-FPM服务:
sudo systemctl restart php7.4-fpm
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default
修改配置文件以支持PHP:
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;
}
location ~ /\.ht {
deny all;
}
}
保存并退出编辑器,然后测试Nginx配置并重启服务:
sudo nginx -t
sudo systemctl reload nginx
创建一个简单的PHP文件来测试配置:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
在浏览器中访问服务器的IP地址或域名后跟 /info.php
,例如:http://your_server_ip/info.php
,如果看到PHP信息页面,则说明LNMP环境安装成功。
以上步骤涵盖了在Debian系统上安装和配置LNMP环境的基本流程。如果在配置过程中遇到问题,可以查看相应服务的日志文件进行排查。