在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環境的基本流程。如果在配置過程中遇到問題,可以查看相應服務的日志文件進行排查。