(LEMP Stack) in Ubuntu 20.04

  • Post author:
  • 帖子最後修改:2022 年 7 月 16 日

LEMP 堆棧是一個技術堆棧,代表 Linux、 Nginx、 MySQL和 PHP。它廣泛用於託管從小型到大型的網站/博客。

在這裡,我們將看到如何 在 Ubuntu 20.04上安裝LEMP Stack。

安裝 LEMP 堆棧

安裝 Linux

這是關於 逐步安裝 Ubuntu 20.04和 將 Ubuntu 18.04 或 Ubuntu 19.10 升級到 Ubuntu 20.04的教程。

繼續在 Ubuntu 20.04上安裝 EMP(Nginx 1.18.0 、PHP 8.0、Mysql)包。

安裝 Nginx

Nginx是一個免費的高性能 Web 服務器,用於提供靜態 Web 內容。它以穩定性、配置簡單、資源消耗低而著稱。

從 Ubuntu 存儲庫安裝 Nginx

使用以下命令安裝 Nginx 包。默認情況下,Ubuntu 20.04 附帶 Nginx 1.18.0

				
					sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nginx
				
			

打開 網絡瀏覽器並訪問您的服務器 IP 地址。

http://你的 IP 地址

Nginx 在 Ubuntu 20.04 上的默認文檔根目錄是/var/www/html,其配置文件位於/etc/nginx/目錄下。

安裝 MySQL 8.0

從 Ubuntu 存儲庫安裝 MySQL 服務器 8.0 是一種直接的方式。

更新存儲庫索引。

DEB庫請參考 此篇ubuntu mysql
				
					sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mysql-server-8.0
				
			

使用mysql_secure_installation命令設置 MySQL root 密碼和其他安全設置以保護 MySQL 實例。

				
					sudo mysql_secure_installation
				
			
				
					Output:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y  << Enable Validate password component to improve security

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2  << Level of Password Policy
Please set the password for root here.

New password: xxx  << Enter MySQL root password

Re-enter new password: xxx  << Re-Enter MySQL root password

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y  << Continue with set password
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : Y  << Remove Anonymous users
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? (Press y|Y for Yes, any other key for No) : Y  << Disallow root login remotely
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : Y  << Remote test database
 - 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? (Press y|Y for Yes, any other key for No) : Y  << Reload tables
Success.

All done!
				
			

MySQL 8.0 使用基於改進的 SHA 256 密碼方法的新身份驗證。由於新的身份驗證機制,使用libmysqlclient構建的舊客戶端可能無法連接到數據庫服務器。

您可以隨時通過在配置/etc/mysql/my.cnf文件中設置以下內容來更改默認身份驗證插件。

				
					
sudo nano /etc/mysql/my.cnf
				
			

加入以下設定

				
					[mysqld]
default-authentication-plugin=mysql_native_password
				
			
檢查 MySQL 版本
				
					mysql -u root -p
				
			
				
					mysql> SELECT VERSION();
+-------------------------+
| VERSION()               |
+-------------------------+
| 8.0.29-0ubuntu0.22.04.2 |
+-------------------------+
1 row in set (0.00 sec)
				
			

創建 MySQL 用戶

				
					mysql>CREATE USER '帳號'@'%' IDENTIFIED BY '密碼';
mysql>GRANT ALL PRIVILEGES ON * . * TO '帳號'@'%';
mysql>FLUSH PRIVILEGES;
mysql>exit
				
			

然後要登入mysql 8.0 修改帳號使相容php

				
					mysql>ALTER USER '帳號'@'%' IDENTIFIED WITH mysql_native_password BY '密碼';
mysql>exit
				
			

自動輸入密碼

				
					[client]
password=YOUR_MYSQL_PASSWORD
				
			

備份還原檔案 使匯入匯出編碼一致,避免windows還原報錯ERROR 1064(42000)語法錯誤

				
					還原
mysql -u account -p --default-character-set=utf8 database < outputifle.sql
備份
mysqldump -u account -p --default-character-set=utf8 database > outputifle.sql
				
			
				
					Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1130
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> SELECT VERSION();
+-------------------------+
| VERSION()               |
+-------------------------+
| 8.0.29-0ubuntu0.22.04.2 |
+-------------------------+
1 row in set (0.00 sec)

mysql>
				
			

結論

就這樣。我希望你已經學會瞭如何 在 Ubuntu 20.04上安裝MySQL 8.0