在Debian上部署LNMP(Linux, Nginx, MySQL/MariaDB, PHP)網站的步驟如下:
首先,確保你的Debian系統是最新的:
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
運行安全腳本以設置root密碼和其他安全選項:
sudo mysql_secure_installation
安裝PHP及其常用擴展:
sudo apt install php-fpm php-mysql -y
啟動PHP-FPM并設置開機自啟:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
編輯Nginx配置文件以使用PHP-FPM處理PHP文件。打開Nginx默認站點配置文件:
sudo nano /etc/nginx/sites-available/default
找到以下部分并進行修改:
server {
...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
...
}
保存并退出編輯器,然后測試Nginx配置:
sudo nginx -t
重新加載Nginx以應用更改:
sudo systemctl reload nginx
如果你使用的是UFW(Uncomplicated Firewall),允許HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
登錄到MySQL/MariaDB并創建數據庫和用戶:
sudo mysql -u root -p
在MySQL shell中執行以下命令:
CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
將你的網站文件上傳到服務器上的適當目錄(例如 /var/www/html)。你可以使用FTP、SCP或其他文件傳輸方法。
如果你有多個網站,可以為每個網站創建一個單獨的Nginx配置文件。例如,創建一個新的配置文件:
sudo nano /etc/nginx/sites-available/your_site
添加以下內容:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/html/your_site;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
啟用該站點:
sudo ln -s /etc/nginx/sites-available/your_site /etc/nginx/sites-enabled/
測試Nginx配置并重新加載:
sudo nginx -t
sudo systemctl reload nginx
打開瀏覽器并訪問你的域名,確保一切正常運行。
通過以上步驟,你應該能夠在Debian上成功部署一個LNMP網站。