LNMP 環境是指在 Linux 系統下,由 Nginx + MySQL/MariaDB + PHP 組成的網站服務器架構。本文介紹如何在搬瓦工VPS上手動搭建 LNMP 環境。進行手動搭建 LNMP 環境,需要熟悉 Linux 命令,例如 CentOS 環境下通過 YUM 安裝軟件 等常用命令,并對所安裝軟件的使用及版本兼容性比較了解。下面來查看詳細內容。
示例軟件版本
本文搭建的 LNMP 環境軟件組成版本及說明如下:
- Linux:Linux 操作系統,本文以 CentOS 7.6 為例。
- Nginx:Web 服務器,本文以 Nginx 1.17.7 為例。
- MariaDB:數據庫,本文以 MariaDB 10.4.8 為例。
- PHP:腳本語言,本文以 PHP 7.2.22 為例。
前提條件
已購買 搬瓦工Linux VPS服務器。如果您還未購買,請參考下表。
搬瓦工推薦方案
溫馨提醒 如果您有選擇困難癥,直接選中間的 CN2 GIA-E方案,季付 $49.99,多達 12 個機房任意切換
| 方案 | 內存 | CPU | 硬盤 | 流量/月 | 帶寬 | 機房 | 價格 | 購買 |
|---|---|---|---|---|---|---|---|---|
| CN2 (最便宜) |
1GB | 1核 | 20GB | 1TB | 1Gbps | DC3 CN2 DC8 ZNET |
$49.99/年 | 購買 |
| CN2 | 2GB | 1核 | 40GB | 2TB | 1Gbps | $52.99/半年 $99.99/年 |
購買 | |
| CN2 GIA-E (最推薦) |
1GB | 2核 | 20GB | 1TB | 2.5Gbps | DC6 CN2 GIA-E DC9 CN2 GIA 日本軟銀 JPOS_1 荷蘭 EUNL_9 |
$49.99/季度 $169.99/年 |
購買 |
| CN2 GIA-E | 2GB | 3核 | 40GB | 2TB | 2.5Gbps | $89.99/季度 $299.99/年 |
購買 | |
| HK | 2GB | 2核 | 40GB | 0.5TB | 1Gbps | 中國香港 CN2 GIA | $89.99/月 $899.99/年 |
購買 |
| HK | 4GB | 4核 | 80GB | 1TB | 1Gbps | $155.99/月 $1559.99/年 |
購買 | |
| TOKYO | 2GB | 2核 | 40GB | 0.5TB | 1.2Gbps | 日本東京 CN2 GIA | $89.99/月 $899.99/年 |
購買 |
| TOKYO | 4GB | 4核 | 80GB | 1TB | 1.2Gbps | $155.99/月 $1559.99/年 |
購買 | |
| 搬瓦工優惠碼:BWH3HYATVBJW | 搬瓦工全方案實時庫存檢測 | |||||||
操作步驟
步驟1:登錄 Linux VPS主機
使用標準方式登錄 搬瓦工Linux 主機(推薦)。您也可以根據實際操作習慣,選擇其他不同的登錄方式:
步驟2:安裝 Nginx
- 執行以下命令,在?/etc/yum.repos.d/?下創建?nginx.repo?文件。
vi /etc/yum.repos.d/nginx.repo - 按?i?切換至編輯模式,寫入以下內容。
[nginx] name = nginx repo baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck = 0 enabled = 1 - 按?Esc,輸入?:wq,保存文件并返回。
- 執行以下命令,安裝 nginx。
yum install -y nginx - 執行以下命令,打開?default.conf?文件。
vim /etc/nginx/conf.d/default.conf - 按?i?切換至編輯模式,編輯?default.conf?文件。
- 找到?server{…},并將?server?大括號中相應的配置信息替換為如下內容。用于取消對 IPv6 地址的監聽,同時配置 Nginx,實現與 PHP 的聯動。
server { listen 80; root /usr/share/nginx/html; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; # location / { index index.php index.html index.htm; } #error_page 404 /404.html; #redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } - 按?Esc,輸入?:wq,保存文件并返回。
- 執行以下命令啟動 Nginx。
systemctl start nginx - 執行以下命令,設置 Nginx 為開機自啟動。
systemctl enable nginx - 在本地瀏覽器中訪問以下地址,查看 Nginx 服務是否正常運行。
http://云服務器實例的公網 IP顯示如下,則說明 Nginx 安裝配置成功。

步驟3:安裝數據庫
- 執行以下命令,查看系統中是否已安裝 MariaDB。
rpm -qa | grep -i mariadb- 返回結果類似如下內容,則表示已存在 MariaDB。

為避免安裝版本不同造成沖突,請執行以下命令移除已安裝的 MariaDB。yum -y remove 包名 - 若返回結果為空,則說明未預先安裝,則執行下一步。
- 返回結果類似如下內容,則表示已存在 MariaDB。
- 執行以下命令,在?/etc/yum.repos.d/?下創建?MariaDB.repo?文件。
vi /etc/yum.repos.d/MariaDB.repo - 按?i?切換至編輯模式,寫入以下內容,添加 MariaDB 軟件庫。
# MariaDB 10.4 CentOS repository list - created 2019-11-05 11:56 UTC # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = https://mirrors.cloud.tencent.com/mariadb/yum/10.4/centos7-amd64 gpgkey=https://mirrors.cloud.tencent.com/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 - 按?Esc,輸入?:wq,保存文件并返回。
- 執行以下命令,安裝 MariaDB。此步驟耗時較長,請關注安裝進度,等待安裝完畢。
yum -y install MariaDB-client MariaDB-server - 執行以下命令,啟動 MariaDB 服務。
systemctl start mariadb - 執行以下命令,設置 MariaDB 為開機自啟動。
systemctl enable mariadb - 執行以下命令,驗證 MariaDB 是否安裝成功。
mysql顯示結果如下,則成功安裝。

- 執行以下命令,退出 MariaDB。
\q
步驟4:安裝配置 PHP
- 依次執行以下命令,更新 yum 中 PHP 的軟件源。
rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpmrpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm - 執行以下命令,安裝 PHP 7.2 所需要的包。
yum -y install mod_php72w.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-mysqlnd php72w-fpm.x86_64 - 執行以下命令,啟動 PHP-FPM 服務。
systemctl start php-fpm - 執行以下命令,設置 PHP-FPM 服務為開機自啟動。
systemctl enable php-fpm
驗證環境配置
當您完成環境配置后,可以通過以下驗證 LNMP 環境是否搭建成功。
- 執行以下命令,創建測試文件。
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php - 執行以下命令,重啟 Nginx 服務。
systemctl restart nginx - 在本地瀏覽器中訪問如下地址,查看環境配置是否成功。
http://云服務器實例的公網 IP顯示結果如下, 則說明環境配置成功。

相關操作
在完成了 LNMP 環境搭建之后,您可在此基礎上進行?手動搭建 WordPress 個人站點?實踐,了解并掌握更多關于云服務器的相關功能。





















