手工搭建LNMP环境(Ubuntu 20.04)
简介
本文主要介绍了在华为云上如何使用弹性云服务器的Linux实例手工搭建LNMP平台的Web环境。本文档以Ubuntu 20.04 64位操作系统为例。
前提条件
- 弹性云服务器已绑定弹性公网IP。
- 弹性云服务器所在安全组添加了如下表所示的安全组规则,具体步骤参见为安全组添加安全组规则。
表1 安全组规则 方向
协议/应用
端口/范围
源地址
入方向
HTTP(80)
80
0.0.0.0/0
- 为了更好的获取和更新系统和软件,建议您更新镜像源为华为云镜像源,详细操作,请参见如何使用自动化工具配置华为云镜像源(x86_64和ARM)?。
资源规划
本次实践所用的资源配置及软件版本如表2中所示。当您使用不同的硬件规格或软件版本时,本指导中的命令及参数可能会发生改变,需要您根据实际情况进行调整。
资源 |
资源说明 |
成本说明 |
---|---|---|
弹性云服务器 |
|
ECS涉及以下几项费用:
具体的计费方式及标准请参考计费模式概述。 |
Nginx |
是一个高性能的HTTP和反向代理web服务器。 |
免费 |
MySQL |
是一款开源的关系数据库软件。 |
免费 |
PHP |
是一款开源软件,用于Web开发。 |
免费 |
操作步骤
- 安装Nginx。
- 登录弹性云服务器。
- 执行以下命令安装Nginx。
sudo apt-get install nginx
- 调整防火墙(可选)。
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'
- 验证Nginx是否正常工作。
在浏览器中通过域名或者IP地址进行访问Nginx,如果Nginx正常启动则会打开Welcome to nginx的欢迎页面。
使用浏览器访问 “http://云服务器IP地址”,显示如下页面,说明Nginx安装成功。
图1 测试访问nginx
- 安装MySQL。
- 执行以下命令安装MySQL。
- 查看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.
- 执行以下命令,进入MySQL。
- 执行以下命令,设置root用户密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'xxxxx';
其中'xxxxx'为待设置的密码。
- 执行以下命令,退出MySQL数据库。
- 执行以下命令,并按照回显提示信息进行操作,加固MySQL。
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!
- 安装PHP。
- 执行以下命令,安装PHP。
sudo apt install php-fpm
- 执行以下命令,验证PHP的安装版本。
回显如下类似信息:
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
- 执行以下命令,查看PHP运行状态。
回显如下信息:
● 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键退出。
- 修改Nginx配置文件以支持PHP。
- 执行以下命令,重新载入nginx的配置文件。
- 执行以下命令,安装PHP。
- 浏览器访问测试。