职场心理 · 2023年12月1日 0

Ubuntu22.04搭建LAMP环境

LAMP 是一个常用于构建动态网站和Web应用程序的开发堆栈,它由以下四个主要组件组成:

Linux(操作系统):LAMP 中的 “L” 代表 Linux,它是操作系统的一种选择。Linux 是一个开源的、免费的操作系统,广泛用于Web服务器环境,因为它稳定、安全且可扩展。在 LAMP 堆栈中,Linux 提供了服务器的基本操作系统框架。

Apache(Web服务器):LAMP 中的 “A” 代表 Apache,它是一个流行的开源Web服务器软件。Apache 提供了处理HTTP请求和响应的能力,它是 Web 应用程序的后端服务器,能够将请求发送给应用程序和处理应用程序生成的响应。

MySQL(数据库管理系统):LAMP 中的 “M” 代表 MySQL,它是一个开源的关系型数据库管理系统(RDBMS)。MySQL 用于存储和管理Web应用程序中的数据,例如用户信息、文章内容、产品信息等。它提供了高性能、可靠性和扩展性,适用于各种规模的Web应用程序。

PHP(服务器端脚本语言):LAMP 中的 “P” 代表 PHP,它是一种流行的服务器端脚本语言。PHP 用于编写服务器端的Web应用程序代码,它可以嵌入到HTML中,动态生成Web页面内容。PHP 具有丰富的功能和库,使开发人员能够创建动态和交互性的Web应用程序。

LAMP 堆栈提供了一个完整的Web开发环境,包括操作系统、Web服务器、数据库和服务器端脚本语言。这个堆栈被广泛用于开发各种类型的Web应用程序,从简单的个人博客到复杂的电子商务平台。

搭建LAMP环境的前期准备
在开始搭建LAMP环境之前,你需要先做好以下前期工作:

准备好一台VPS,可以参考我的搭建教程。
已通过SSH连接VPS,后续所有的搭建操作都是在VPS上完成的。
第一步:安装Apache
Apache是一款强大的开源Web服务器软件,许多网站和Web应用程序都依赖于Apache作为其服务器基础,因此我们需要首先安装Apache。

sudo apt update && sudo apt install apache2 –yes && sudo ufw allow in “Apache”
上面的命令完成了三件事情:

更新Linux系统软件包索引
安装Apache
设置系统防火墙,允许进入的流量通过Apache
接下来,你可以在浏览器中测试Apache是否正常运行,输入以下地址:

http://VPS_IP需要注意的是,在这里应使用HTTP协议,而不是HTTPS,因为我们在上面的命令中只开放了80端口。例如,我这里访问的是:

http://20.243.21.117
如果你能打开,表明你已经配置成功了。

网页端访问VPS

第二步:安装MySQL
MySQL是一种广受欢迎的关系型数据库,我们WordPress网站的各种数据都存储在MySQL中。

sudo apt install mysql-server -y && sudo mysql
执行完上面的命令,你就进入MySQL数据库了。

进入MySQL数据库

接下来我们需要设置MySQL数据库的密码,在MySQL中输入以下代码,其中PASSWORD必须替换成你自己的密码。请牢记这个密码,后面会用到。

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password by ‘PASSWORD’;
然后输入exit退出MySQL数据库。

执行成功后,你将返回到原来的VPS终端界面。

退出MySQL数据库

接下来初始化MySQL,输入以下命令:

sudo mysql_secure_installation
在运行该命令后,会要求你输入密码,直接输入上面设置的密码即可。然后,按照以下提示进行选择:

是否启用密码强度检查:选择Y。
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
密码强度要求:选择 0(代表最低强度,适合初学者;如果你有经验,可以选择 1 或 2)。
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: 0
是否要重新设置root用户密码:选择N。

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
是否删除匿名用户:选择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
是否禁止root用户远程登录:选择Y。
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
是否删除测试数据库:选择Y。
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
是否刷新权限表:选择Y。
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
这些步骤完成后,MySQL就安装并进行了基本配置。你将看到屏幕上显示 “All done” 字样。

第三步:安装PHP
首先,运行以下命令来安装PHP及相关模块:

sudo apt install php libapache2-mod-php php-mysql -y
安装完成后,你可以使用以下命令检查PHP版本:

php -v
你应该会看到类似于下面的输出,显示你的PHP版本(例如8.1.2)。

PHP版本

最后
到这里,你已经成功安装了LAMP环境,可以准备运行或安装其他程序了。你可以继续按照我的教程《最新VPS搭建WordPress教程》安装WordPress,利用这个搭建出来的WordPress来创造更多的价值。

1.问题

将此处不小心改为https,然后保存
从此wordpress就不可访问

2.办法
将数据库中wp_options表里的数据修改即可

去配置网站的云服务器,打开mysql

mysql -u root -p
#此处还可以是你配置的只有wordpress数据库权限的新用户,将root改为改用户名字即可,输入密码
1
2
显示当前的数据库

show databases;
1
结果如下:

+——————–+
| Database |
+——————–+
| wordpress |
+——————–+
1
2
3
4
5
使用数据库wordpress

use wordpress;
1
查找信息

select * from wp_options where option_name = ‘home’;
select * from wp_options where option_name = ‘siteurl’
1
2
结果如下即可,说明搜到了当时更改的地方

可以看到当时修改的https就在这里,接下来将该数据更改即可

xxxxx是你的地址

update wp_options set option_value = ‘http://xxxxx/wordpress’ where option_name = ‘home’;
update wp_options set option_value = ‘http://xxxxx/wordpress’ where option_name = ‘siteurl’;
1
2
3
修改完成后退出mysql
重启系统,即可解决
————————————————