在 Ubuntu 20.04 上安裝 LAMP Stack

是一組由Linux組成的開源軟件, Apache、MySQL 或 MariaDB 和 PHP。 就像包含 NGINX 而不是 Apache, LAMP 也用於開發和部署 Web 解決方案; 包括動態網站和網絡應用程序。

在本指南中,我們將安裝 燈堆棧Ubuntu 20.04.

先決條件

  • 運行 Ubuntu 20.04 的 Linux 服務器。 這會處理 LAMP 中的“L”
  • 一個用戶 sudo 特權

安裝 Apache 在 Ubuntu 上

這 Apache HTTP 服務器用於通過 Web 向客戶端提供資源。 首先,如果您最近沒有這樣做,請使用以下命令更新您的 Ubuntu 軟件包。

$ sudo 適當更新

接下來,運行以下命令進行安裝 Apache 版本 2 在 Ubuntu 20.04 上。

$ sudo apt安裝apache2

如果出現提示,請輸入 繼續安裝。

安裝後,您可以使用以下命令檢查 Apache2 HTTP 服務器的版本:

$ sudo apache2 -v

此外,使用下一個命令確認 Apache2 服務器處於活動狀態(正在運行)。

$ sudo 系統控制 apache2

檢查 Apache2 服務狀態

現在,打開 Web 瀏覽器並輸入服務器的 IP 地址。 或輸入 本地主機 如果您在本地連接到服務器。

你應該看到 Apache2 Ubuntu 默認頁面 如下圖所示。

Apache2 Ubuntu 默認頁面Apache2 Ubuntu 默認頁面

您可以在單個 Apache2 Web 服務器上託管多個站點,並相應地連接您註冊的域。

這會處理 LAMP 中的“A”。

在 Ubuntu 上安裝 MariaDB

對於 LAMP 首字母縮寫詞的“M”部分,我們需要安裝一個數據庫管理系統來存儲和管理站點/應用程序數據。 讓我們選擇 MariaDB。 MariaDB 是 MySQL 的衍生產品,但它提供了相對較快的性能。

運行下面的命令來 安裝 MariaDB 在 Ubuntu 20.04 上。

$ sudo apt 安裝 mariadb 服務器

Enter 如果提示繼續安裝。

MariaDB 安裝成功後,使用下一條命令檢查服務狀態。

$ sudo systemctl 狀態 mariadb

檢查 MariaDB 服務狀態檢查 MariaDB 服務狀態

您應該看到 MariaDB 服務是 活動(運行) 已經。 你可以按 q 返回到 shell 提示符。 否則,使用以下命令啟動服務,然後再次檢查狀態。

$ sudo systemctl 啟動 mariadb

接下來,運行下面的命令來運行 mysql_secure_installation 腳本,然後按照說明保護 MariaDB。

$ mysql_secure_installation

以下是運行腳本後預期結果的示例。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.

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

By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

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!

現在,運行下一個命令以使用您選擇的密碼登錄到 mysql。

$ sudo mysql -u 根 -p

如果你看到 瑪麗亞數據庫 提示,你在課程中。

在 Ubuntu 上安裝 PHP

PHP (超文本預處理器)是一種服務器端腳本語言,它可以在與數據庫交互的同時動態處理 Web 內容。 運行下面的命令來安裝 PHP 和相關組件。

$ sudo apt 安裝 php php-mysql

使用以下命令檢查已安裝的 PHP 版本:

$ php -v

檢查 PHP 版本檢查 PHP 版本

為了確認 PHP 是否正常工作,讓我們執行如下簡單的測試。

創建一個 索引.php 使用以下命令在默認網站根目錄中創建文件。

$ sudo 納米/var/www/html/index.php

將下面的示例 PHP 代碼粘貼到文本編輯器中。

<?php
phpinfo();
?>

Save 變化和 close 文件。

使用以下命令重新啟動 Apache2:

$ sudo systemctl 重啟 apache2

現在,打開網絡瀏覽器並訪問 服務器IP/index.php. 為了 example, 192.11.9.2/index.php.

如果一切正常,您應該會看到一個頁面,顯示有關您的 PHP 安裝的信息,如下圖所示。

測試 Apache2 PHP 處理測試 Apache2 PHP 處理

結論

在本指南中,我們成功設置了包含 Ubuntu 20.04 Linux 服務器、Apache2、MariaDB 和 PHP 的 LAMP 堆棧。 您已準備好開始開發、測試和部署動態網站和 Web 應用程序。