簡介
本文主要介紹了在天翼云上如何使用彈性云主機的Linux實例手工搭建LNMP平臺的Web環境。本文檔以Ubuntu 20.04 64位操作系統為例。
前提條件
1、彈性云主機已綁定彈性公網IP。
2、彈性云主機所在安全組添加了如下表所示的安全組規則。
| 方向 | 協議/應用 | 端口/范圍 | 源地址 |
|---|---|---|---|
| 入方向 | HTTP(80) | 80 | 0.0.0.0/0 |
資源規劃
本次實踐所用的資源配置及軟件版本如下表中所示。當您使用不同的硬件規格或軟件版本時,本指導中的命令及參數可能會發生改變,需要您根據實際情況進行調整。
| 資源 | 資源說明 | 成本說明 |
|---|---|---|
| 彈性云主機 | 計費模式:按需計費 規格:s6.large.2 鏡像:Ubuntu 20.04 64bit 系統盤:40G 彈性公網IP:自動分配 公網帶寬:按流量計費 帶寬大小:5 Mbit/s |
ECS涉及以下幾項費用: 云主機 云硬盤 彈性公網IP |
| Nginx | 是一個高性能的HTTP和反向代理web服務器。 | 免費 |
| MySQL | 是一款開源的關系數據庫軟件。 | 免費 |
| PHP | 是一款開源軟件,用于Web開發。 | 免費 |
操作步驟
1、安裝Nginx。
a.登錄彈性云主機。
b.執行以下命令安裝Nginx。
sudo apt-get update
sudo apt-get install nginx
c.調整防火墻(可選)。
UFW(Uncomplicated Firewall)是一個iptables的接口,可以簡化配置防火墻的過程。Ubuntu默認安裝了UFW,執行以下命令查看防火墻的狀態。
sudo ufw status
如果你沒有也不想開啟防火墻,則可以直接跳過此步驟,如果你想要開啟防火墻可以通過以下命令實現。
sudo ufw enable
之后再次檢查防火墻狀態驗證是否成功開啟防火墻。
在測試Nginx之前,需要重新配置我們的防火墻軟件以允許訪問Nginx。執行以下命令,將Nginx自動注冊在UFW。
sudo ufw app list
回顯信息:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
...
- Nginx Full:此配置文件打開端口 80(正常,未加密的Web流量)和端口443(TLS / SSL加密流量)
- Nginx HTTP:此配置文件僅打開端口 80(正常,未加密的Web流量)
- Nginx HTTPS:此配置文件僅打開端口 443(TLS / SSL加密流量)
執行以下命令確保防火墻允許HTTP和HTTPS連接。
sudo ufw allow 'Nginx Full'
d.驗證Nginx是否正常工作。
在瀏覽器中通過域名或者IP地址進行訪問Nginx,如果Nginx正常啟動則會打開Welcome to nginx的歡迎頁面。
使用瀏覽器訪問 “//云主機IP地址”,顯示如下頁面,說明Nginx安裝成功

2、安裝MySQL。
a.執行以下命令安裝MySQL。
sudo apt -y install mysql-server
b.查看MySQL運行狀態。
sudo systemctl status mysql
mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-07-26 15:57:29 CST; 22min ago
Main PID: 10770 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 4217)
Memory: 364.9M
CGroup: /system.slice/mysqld.service
└─10770 /usr/sbin/mysqld
Jul 26 15:57:29 ecs-ubuntu systemd[1]: Starting MySQL Community Server...
Jul 26 15:57:29 ecs-ubuntu systemd[1]: Started MySQL Community Server.
c.執行以下命令,進入MySQL。
sudo mysql
d.執行以下命令,設置root用戶密碼。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'newpassword';
其中'newpassword'為待設置的密碼。
e.執行以下命令,退出MySQL數據庫。
exit;
f.執行以下命令,并按照回顯提示信息進行操作,加固MySQL。
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #輸入步驟4中設置的root用戶密碼
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration of the component.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用戶密碼,輸入Y
New password: #設置新的root用戶密碼
Re-enter new 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 #確認使用已設置的密碼,輸入Y
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 #是否刪除匿名用戶,輸入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? (Press y|Y for Yes, any other key for No) : Y #禁止root遠程登錄,輸入Y
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 #是否刪除test庫和對它的訪問權限,輸入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? (Press y|Y for Yes, any other key for No) : Y #是否重新加載授權表,輸入Y
Success.
All done!
3、安裝PHP。
a.執行以下命令,安裝PHP。
sudo apt update
sudo apt install php-fpm
b.執行以下命令,驗證PHP的安裝版本。
php -v
回顯如下類似信息:
PHP 7.4.3-4ubuntu2.19 (cli) (built: Jun 27 2023 15:49:59) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3-4ubuntu2.19, Copyright (c), by Zend Technologies
c.執行以下命令,查看PHP運行狀態。
systemctl status php7.4-fpm
回顯如下信息:
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-31 17:33:35 CST; 3min 50s ago
Docs: man:php-fpm7.4(8)
說明回顯信息中若出現“lines 1-16/16 (end)”,可按q鍵退出。
d.修改Nginx配置文件以支持PHP。
1.執行以下命令,打開Nginx默認的配置文件。
sudo vim /etc/nginx/sites-enabled/default
2.按i鍵進入編輯模式。
3.修改打開的Nginx配置文件。
在server{}內,找到index開頭的配置行,在該行中添加index.php。

在server{}內找到location ~ .php$ {},去除以下配置行的注釋符號。

4.按Esc鍵退出編輯模式,并輸入:wq保存后退出。
e.執行以下命令,重新載入nginx的配置文件。
sudo systemctl restart nginx
4、瀏覽器訪問測試。
a.在Nginx網站根目錄中,新建phpinfo.php文件。
sudo vim /var/www/html/phpinfo.php
b.按i鍵進入編輯模式。
c.修改打開的“phpinfo.php”文件,將如下內容寫入文件。
<?php echo phpinfo(); ?>
d.按Esc鍵退出編輯模式,并輸入:wq保存后退出。
e.使用瀏覽器訪問“//服務器IP地址/phpinfo.php”,顯示如下頁面,說明環境搭建成功。
