如何在 Debian 12 上安裝和設定 LEMP

在本教程中,您將了解在 Debian 12 上安裝和設定 LEMP 所需遵循的步驟。Nginx、MariaDB 和 PHP 是高效能 Web 託管的必要軟體包,無論是演示網站、部落格還是線上商店。

LEMP 是縮寫 Linux、Nginx、MySQL/MariaDB、PHP,是世界各地許多 Web 專案和 Web 應用程式的基礎。 不然後面你就看不到這篇文章了 stealthsettings.com 不會擁有所有這些軟體包來確保我們網站的線上運作。

Debian 是一個基於 Debian 的開源作業系統 Linux,以其穩定性、安全性和靈活性而聞名。 Debian 擁有悠久的歷史和活躍的開發者社區,提供了廣泛的軟體包和庫來滿足用戶的多樣化需求。 該作業系統經常用於為公司創建可靠且高效的伺服器,但它也可以適合個人使用。

如何在 Debian 12 上安裝和設定 LEMP

在 Debian 12 上安裝和設定 LEMP 之前的第一步是透過 SSH 連線到伺服器(如果您有本機存取權限,請直接從控制台連線)。 確保伺服器上尚未安裝來自 LEMP 或管理系統的軟體包,例如:cPanel、 VestaCP 或其他。

本教程中的命令集以“sudo”,但是如果您使用用戶“連接到伺服器root“, 沒有必要 ”sudo” 在命令列前面。 命令 ”sudo” 用於在命令列前面臨時向普通用戶授予超級用戶權限,允許他們執行需要特殊權限或訪問受保護資源的命令,例如安裝或配置軟體、管理系統或執行其他敏感操作。

Update 軟件。

在開始 LAMP 安裝之前,建議更新作業系統和已安裝的軟體套件。

sudo apt update
sudo apt upgrade

如果有可用的軟體包 update, 按 ”Y” 當被問到時:

Do you want to continue? [Y/n] Y

在 Debian 12 上安裝並設定 Nginx Web 伺服器

為了使網頁伺服器向訪客提供網頁, Nginx Web Server 必須正確安裝和配置它。

sudo apt install nginx

類型 ”Y」以確認 Nginx Web 伺服器的安裝。

Do you want to continue? [Y/n] Y
Install Debian 12 上的 Nginx Web 伺服器
Install Debian 12 上的 Nginx Web 伺服器

安裝過程結束時的行應如下所示:

Setting up nginx-common (1.22.1-9) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
Setting up nginx (1.22.1-9) ...
Upgrading binary: nginx.
Processing triggers for man-db (2.11.2-2) ...

最後,為了確保一切正常運作,請訪問地址 IP 在網頁瀏覽器中。 http://your_server_ip.

歡迎來到 NGINX 頁面
歡迎來到 NGINX 頁面

Nginx 服務已在您的伺服器上啟動並執行,並準備好提供網頁服務。

相關新聞:

在 Debian 12 上安裝並設定 MariaDB 伺服器

與 MySQL 相比,MariaDB 提供了改進的效能、更快的複製速度、增強的安全措施以及額外的儲存引擎。

運行下面的命令列並輸入“Y” 確認安裝 MariaDB Server.

sudo apt install mariadb-server

安裝後,您需要透過執行腳本來保護 MariaDB Server: mysql_secure_installation。 它將限制對伺服器的存取並刪除未使用的帳戶。

運行命令列:

sudo mysql_secure_installation

按 ”Enter“目前使用者密碼”root“。

如何在 Debian 12 上安裝和設定 LEMP
安全性 SQL #1
Switch to unix_socket authentication [Y/n] Y

設定密碼 MariaDB:

Change the root password? [Y/n] Y     
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

接下來您將刪除放置的權限和資料庫 default 安裝期間 MariaDB Server.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

上面為帳戶設定的密碼“root” 登入遠端伺服器時需要。 偏僻的。 這是密碼 MariaDB.

測試您的 SQL Server 安裝。

root@mars:~# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.11.3-MariaDB-1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select version();
+-------------------+
| version()         |
+-------------------+
| 10.11.3-MariaDB-1 |
+-------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> quit
Bye
root@mars:~# 

在 Debian 12 上安裝和設定 PHP / PHP-FPM

如果到此步驟一切正常,為了在 Debian 12 上安裝和設定 LEMP,您還需要 PHP 軟體。 要在 Nginx 中支援 PHP,您還需要安裝 PHP-FPM。

sudo apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcache php-xml

等待安裝過程完成 PHP-FPM,以及最重要的 PHP 模組。

最後,在我的教程中我能夠安裝 PHP 8.2 在 Debian 12 作業系統上。

root@mars:~# php -v
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies
root@mars:~# 

至此,LEMP的所有元素都已安裝,但仍有一些配置需要完成 Nginx 執行 PHP 檔案。

配置 NGINX 執行 PHP 文件

為頁面配置Nginx“default“, 編輯 ”/etc/nginx/sites-enabled/default”。 我更喜歡 ”nano”進行編輯。

替換為“location /”行:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
     }

與行:

location / {
    try_files $uri $uri/ /index.php?$args;
    }

接下來,在區塊「中新增以下行server” 以允許 Nginx 處理 PHP。

location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/var/run/php/php-fpm.sock;
     }
}
如何在 Debian 12 上安裝和設定 LEMP
如何在 Debian 12 上安裝和設定 LEMP

儲存檔案並檢查配置:

root@mars:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@mars:~# 

如果配置測試成功,重新啟動服務 nginx.

sudo systemctl restart nginx

測試您是否已在 Debian 12 上成功安裝和設定 LEMP

在 Debian 12 (LEMP) 上安裝並設定 Nginx、MariaDB 和 PHP 後,是時候測試一切是否正常。 要進行測試,最簡單的方法是建立一個 info.php 檔案。

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

在瀏覽器中造訪“https://your_server_ip/info.php“。

PHP 版本資訊
PHP 版本資訊

PHP 頁面表示您已成功安裝 Debian 12 的 LEMP。

恭喜!

對技術充滿熱情,我很高興寫 StealthSettings.com 自 2006 年起。我在作業系統方面擁有豐富的經驗: macOS, Windows 對 Linux,而且還包括程式語言和部落格平台(WordPress)和線上商店(WooCommerce、Magento、PrestaShop)。

如何 » Web託管 » 如何在 Debian 12 上安裝和設定 LEMP
發表評論