如何在 Ubuntu 22.04 上安装 LAMP( Apache, MariaDB, PHP) 网站环境

2022-11-06 2721 次阅读 0

LAMP ( Linux, Apache, MariaDB / MySQL, and PHP) 是使用最广泛的网站或者博客搭建环境之一。通常个人网站或博客大部分都是部署在这样的环境上。

下面我们讲解下如何在 Ubuntu 22.04 上部署 LAMP。

安装前言

在 Ubuntu 22.04 的软件仓库中已经包含了我们需要安装软件包,我们可以手动逐个安装Apache,MariaDB和PHP也可以一条命令安装lamp服务环境。本文中我们将安装AMP (Apache v2.4, PHP v8.1, and MariaDB v10.6)作为示例。

安装Apache

首先,先更新包存储索引,命令如下:

sudo apt update

更新完成后,用下面命令安装apache:

sudo apt install -y apache2 apache2-utils

命令执行后,等待apache安装完成即可。

安装MariaDB

你需要知道的是,MariaDB 已经取代 MySQL 成为 Ubuntu 中的默认数据库服务器软件,并且可以从Ubuntu存储库中获得(v10.6)。 当然,如果你有特殊要求,您也可以从MariaDB存储库安装最新版本的MariaDB。

从Ubuntu 22.04 的软件仓库中默认安装MariaDB的命令如下:

sudo apt install -y mariadb-server mariadb-client

安装完成后,执行如下命令可以对安装的MariaDB进行安全加固。

sudo mysql_secure_installation

命令执行完成后,你会看到类似如下输出,可以仿照下面内容中的输入进行每一步操作:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none): << Just Press Enter
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] N  << Disable Unix Socket Authentication
 ... skipping.
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] Y << Set MariaDB root password
New password: xxx << Enter new MariaDB root password
Re-enter new password: xxx << Re-enter new MariaDB root password
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] Y << Remove Anonymous Users
 ... 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? [Y/n] Y << Disallow root login remotely
 ... Success!
By default, MariaDB 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? [Y/n] Y << Remove test database
 - 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? [Y/n] Y << Reload privilege tables
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

安装PHP

默认情况下,Apache服务软件默认只支持HTML这种静态文件,并不支持PHP文件。所以我们需要启用PHP以及安装PHP的相关扩展包来实现PHP环境语言的支持。命令如下:

sudo apt install -y php php-mysql libapache2-mod-php

安装完成后,通过以下命令重启apache服务。

sudo systemctl restart apache2

重启完成后,我们就可以测试我们的LAMP网站环境了。

一条命令安装

这种方式将通过一条命令安装amp环境,同样在安装前我们先更新系统包存储索引。

sudo apt update

更新完成后,输入以下一条命令即可完成Apache,MariaDB,PHP及相关扩展的安装。

sudo apt install -y lamp-server^

需要等待几分钟才能完成LAMP的安装。安装完成后,运行mysql_secure_installation命令以保护加固MariaDB。

sudo mysql_secure_installation

配置内容类似如下:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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 << Enable validate password component to validate the strength of user passwords

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 << Level of password validation policy
Please set the password for root here.

New password: xxx << Set MySQL root password

Re-enter new password: xxx << Re-Enter MySQL root 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 << Continue
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 << Remove anonymous users
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 << Disable root login remotely
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 << Remove test database
 - 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 << Reload tables
Success.

All done!

当从Ubuntu存储库安装时,MySQL 对 MySQL 根用户使用 Unix Socket 身份验证机制。这种身份验证机制允许 MySQL 根用户仅从 Unix套接字(#prompt或sudo)登录。要禁用UnixSocket身份验证并启用本机密码机制,请执行以下步骤。

以MariaDB root用户身份登录。

sudo mysql -u root -p

无需密码

use mysql;

update user set plugin='mysql_native_password' where user='root';

flush privileges;

quit;

测试LAMP环境

Apache的默认Document根目录是/var/www/html,主配置文件是/etc/apache2/apache2.conf。可以在/etc/apache2/*-enabled目录中找到其他配置,如虚拟主机和模块。 为了测试PHP,我们将在Apache的默认文档根目录下放置一个PHP文件。

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

现在打开web浏览器,在web地址中键入以下内容以访问测试页面:

http://your_server_ip/info.php

正常情况下你将会看到如下页面:

PHP Information

向下滚动页面以查看MySQL/MariaDB的PHP扩展的详细信息。

写在最后

至此,本文讲解了在 Ubuntu 20.04 上有安装LAMP软件环境的方法,希望该问对你有所帮助。

评论(0

暂无评论,来说两句吧

发表评论

回复 取消