簡介
LAMP是由Linux、Apache、MySQL和PHP建立的web應用平臺。
本文主要介紹了在天翼云上如何使用彈性云主機的Linux實例手工搭建LAMP平臺的web環境。該指導具體操作以CentOS 7.8 64位操作系統為例。
前提條件
1、彈性云主機已綁定彈性公網IP。
2、彈性云主機所在安全組添加了如下表所示的安全組規則
| 方向 | 協議/應用 | 端口/范圍 | 源地址 |
|---|---|---|---|
| 入方向 | HTTP(80) | 80 | 0.0.0.0/0 |
資源規劃
本次實踐所用的資源配置及軟件版本如下表中所示。當您使用不同的硬件規格或軟件版本時,本指導中的命令及參數可能會發生改變,需要您根據實際情況進行調整。
| 資源 | 資源說明 | 成本說明 |
|---|---|---|
| 彈性云主機 | 計費模式:按需計費 規格:c7n.large.2 鏡像:CentOS 7.8 64bit 系統盤:40G 彈性公網IP:自動分配 公網帶寬:按流量計費 帶寬大小:5 Mbit/s |
ECS涉及以下幾項費用: · 云主機 · 云硬盤 · 彈性公網IP |
| Apache | 是一個開放源碼的Web服務器。 | 免費 |
| MySQL | 是一款開源的關系數據庫軟件。獲取方式: | 免費 |
| PHP | 是一款開源軟件,用于Web開發。獲取方式: | 免費 |
操作步驟
1、安裝Apache。
a.登錄彈性云主機。
b.使用root用戶執行以下命令更新軟件包,并安裝Apache。
yum -y updateyum -y install httpd
c.執行以下命令,驗證Apache的安裝版本。
httpd -v
回顯如下類似信息:
Server version:Apache/2.4.6 (CentOS)Server built: ?May 30 2023 14:01:11
d.依次執行以下命令,啟動Apache服務并設置開機自啟動。
systemctl start httpd
systemctl enable httpd
e.使用瀏覽器訪問“//服務器IP地址”,顯示如下頁面,說明Apache安裝成功。

2、安裝MySQL。
a.依次執行以下命令,安裝MySQL。
wget -i -c //dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server --nogpgcheck
b.執行以下命令,驗證MySQL的安裝版本。
mysql -V
回顯如下類似信息:
mysql Ver 14.14 Distrib 5.7.44, for Linux (x86_64) using EditLine wrapper
c.依次執行以下命令,啟動MySQL服務并設置開機自啟動。
systemctl start mysqld
systemctl enable mysqld
d.查看MySQL運行狀態。
systemctl status mysqld.service
[root@ecs-adc3 ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2023-10-31 19:33:40 CST; 36s ago
Docs: man:mysqld(8)
//dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 7916 (mysqld)
CGroup: /system.slice/mysqld.service
└─7916 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Aug 16 19:33:35 ecs-adc3 systemd[1]: Starting MySQL Server...
Aug 16 19:33:40 ecs-adc3 systemd[1]: Started MySQL Server.
e.執行以下命令,獲取安裝MySQL時自動設置的root用戶密碼。
grep 'temporary password' /var/log/mysqld.log
回顯如下類似信息。
2023-10-31T11:53:08.691748Z 1 [Note] A temporary password is generated for root@localhost: 2YY?3uHUA?Ys
f.執行以下命令,并按照回顯提示信息進行操作,加固MySQL。
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #輸入上一步驟中獲取的安裝MySQL時自動設置的root用戶密碼
The existing password for the user account root has expired. Please set a new password.
New password: #設置新的root用戶密碼
Re-enter new password: #再次輸入密碼
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
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) : N #是否更改root用戶密碼,輸入N
... skipping.
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 7和一些所需的PHP擴展。
rpm -Uvh //mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh //mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
b.執行以下命令,驗證PHP的安裝版本。
php -v
回顯如下類似信息:
PHP 7.0.33 (cli) (built: Dec 6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
c.執行以下命令,啟動PHP服務并設置開機自啟動。
systemctl start php-fpm
systemctl enable php-fpm
4、瀏覽器訪問測試。
a.在/var/www/html/目錄下創建“info.php”的測試頁面。
1.執行以下命令創建并打開“info.php”的測試文件。
vim /var/www/html/info.php
2.按i鍵進入編輯模式。
3.修改打開的“info.php”文件,將如下內容寫入文件。
<?php
phpinfo();
?>
4.按Esc鍵退出編輯模式,并輸入:wq保存后退出。
b.執行以下命令,重啟Apache服務。
systemctl restart httpd
c.使用瀏覽器訪問“//服務器IP地址/info.php”,顯示如下頁面,說明環境搭建成功。
