更新时间:2025-08-06 GMT+08:00
手工搭建LAMP环境(Ubuntu)
应用场景
LAMP常用于构建网站和Web应用程序,是操作系统Linux、Web服务器Apache、数据库服务器MySQL和编程语言PHP的首字母缩写。LAMP由来自世界各地的专家维护,并且有庞大的社区支持,提供了丰富的资源和支持,使您能够更高效地构建、部署和维护应用程序。
本文介绍如何在Ubuntu操作系统的ECS实例中部署LAMP环境。
方案优势
- ECS实例的弹性伸缩、安全性、高可用性和易于管理等优势得到充分利用。
- LAMP架构具有丰富的Web资源、可实现轻量级与快速开发、同时具备高性能与低成本特点。
资源和成本规划
本次实践所用的资源配置及软件版本如表1中所示。当您使用不同的硬件规格或软件版本时,本指导中的命令及参数可能会发生改变,需要您根据实际情况进行调整。
资源 |
资源说明 |
成本说明 |
---|---|---|
弹性云服务器 |
|
ECS涉及以下几项费用:
具体的计费方式及标准请参考计费模式概述。 |
Apache |
是一个开源的Web服务器软件。 |
免费 |
MySQL |
是一款开源的关系数据库软件。 |
免费 |
PHP |
是一款开源软件,用于Web开发。 |
免费 |
实施步骤
安装前准备
- 弹性云服务器已绑定弹性公网IP,具体操作请参考绑定弹性公网IP。
- 弹性云服务器所在安全组添加了如表2所示的安全组规则,具体步骤参见为安全组添加安全组规则。
操作步骤
- 登录弹性云服务器。
- 安装Apache。
- 执行以下命令,更新Ubuntu系统内的软件包。
sudo apt update
- 执行以下命令,安装Apache。
sudo apt-get -y install apache2
- 执行以下命令,查看Apache版本。
apache2 -v
回显如下类似信息:
- 依次执行以下命令,启动Apache服务并设置服务开机自启动。
sudo systemctl start apache2 sudo systemctl enable apache2
- 执行以下命令,查看Apache服务状态是否已启动。
sudo systemctl status apache2
出现如下图所示active状态,则表示Apache服务已启动。
- 执行以下命令,更新Ubuntu系统内的软件包。
- 安装MySQL并配置。
- 执行以下命令,安装MySQL。
sudo apt -y install mysql-server
- 执行以下命令,查看MySQL版本。
mysql -V
回显如下类似信息:
- 执行以下命令,启动MySQL服务。
sudo systemctl enable mysql sudo systemctl daemon-reload
- 配置MySQL
- 执行以下命令,进入MySQL。
sudo mysql
- 设置root用户密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'newpassword';
- 退出MySQL。
exit;
- 执行以下命令,对MySQL进行安全性配置。
sudo mysql_secure_installation
Securing the MySQL server deployment. Enter password for user root: #输入上一步骤中设置的root用户密码 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 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 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!
- 执行以下命令,进入MySQL。
- 执行以下命令,安装MySQL。
- 安装PHP。
- 执行以下命令,安装software-properties-common软件包。
sudo apt-get install -y software-properties-common
- 执行以下命令,安装PHP第三方软件包Ondrej PPA。
sudo add-apt-repository ppa:ondrej/php
出现如下回显信息时,请按下Enter键。
- 执行以下命令,安装PHP。
sudo apt-get install -y php8.3 php8.3-fpm libapache2-mod-php8.3
- 执行以下命令,查看PHP版本。
php -v
回显信息如下类似信息,表示PHP安装成功。
- 执行以下命令,安装software-properties-common软件包。
- 浏览器访问测试。
- 执行以下命令,进入Apache配置文件目录,将apache2.conf文件复制到apache2.conf.bak文件中,并将原先的示例配置文件保留作为备份。
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
- 执行以下命令,修改Apache配置文件,添加Apache对PHP的支持。
- 执行以下命令,打开Apache配置文件。
sudo vim /etc/apache2/apache2.conf
- 按i键进入编辑模式。
- 修改打开的“apache2.conf”文件,将如下内容写入文件。
DirectoryIndex index.html index.php <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
- 按Esc键退出编辑模式,并输入:wq保存后退出。
- 执行以下命令,打开Apache配置文件。
- 在Apache网站根目录中,创建测试网页。
- 执行以下命令,获取Apache网站根目录的路径信息。
sudo cat /etc/apache2/sites-available/000-default.conf
- 执行以下命令,在网站根目录创建测试网页,使用phpinfo()函数添加至网页内容中。phpinfo()函数会展示PHP的所有配置信息。
sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
- 执行以下命令,获取Apache网站根目录的路径信息。
- 执行以下命令,重启Apache服务
sudo systemctl restart apache2
- 使用浏览器访问“http://服务器IP地址/phpinfo.php”,显示如下页面,说明环境搭建成功。
- 在搭建LAMP环境成功后,建议执行以下命令,删除phpinfo.php测试文件,以消除数据泄露风险。
sudo rm -rf <网站根目录>/phpinfo.php
本示例中网站的根目录为/var/www/html,则需要运行以下命令删除测试文件。
sudo rm -rf /var/www/html/phpinfo.php
- 执行以下命令,进入Apache配置文件目录,将apache2.conf文件复制到apache2.conf.bak文件中,并将原先的示例配置文件保留作为备份。
父主题: 搭建LAMP环境