如何在Ubuntu 20.04上安装Linux,Apache,MariaDB,PHP(LAMP Stack)

LAMP堆栈(Linux,Apache,MariaDB / MySQL和PHP)是用于托管网站和博客的广泛使用的技术堆栈。 该堆栈主要用于托管内容管理软件,例如WordPress,Drupal,Joomla等。

在这里,我们将看到如何在Ubuntu 20.04上安装LAMP堆栈。

安装LAMP堆栈

安装Linux

这是有关逐步安装Ubuntu 20.04以及将Ubuntu 18.04 LTS升级到Ubuntu 20.04 LTS的指南。

安装AMP堆栈

在其他Linux环境中,您需要手动安装所需的软件包。 但是,在Ubuntu中,您可以使用单个命令安装整个堆栈。

  1. 在Ubuntu 18.04上安装LAMP堆栈–单个命令

  2. 在Ubuntu 18.04上安装LAMP堆栈–手动(一对一)

使用单个命令安装LAMP堆栈时,您将获得MySQL 8.0作为数据库服务器。 但是,同时,如果您选择手动安装LAMP堆栈,您将获得MariaDB 10.3作为数据库。

方法1:手动安装LAMP堆栈(一个接一个)

现在,我们将继续在Ubuntu 20.04上安装AMP(Apache v2.4,PHP v7.4和MariaDB v10.3)。

安装Apache

更新软件包存储库索引。

sudo apt update

使用apt命令安装apache2软件包。

sudo apt -y install apache2 apache2-utils

Apache的默认文档根目录是/ var / www / html,而主要配置文件是/etc/apache2/apache2.conf。 可以在/ etc / apache2 / *-enabled目录中找到其他配置,例如虚拟主机和模块。 安装MariaDB

MariaDB已将MySQL替换为Ubuntu中的默认数据库服务器。 要安装MariaDB v10.3,请运行以下命令。

读: 如何在Ubuntu 20.04上安装MariaDB

sudo apt install -y mariadb-server mariadb-client

运行mysql_secure_installation命令以保护MySQL安装。

 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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y  
New password: xxx 
Re-enter new password: xxx 
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  
 ... 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  
 ... 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  
 - 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  
 ... 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!

MariaDB对MariaDB根用户使用Unix套接字身份验证机制,该机制允许MariaDB根用户仅从Unix套接字(#提示符或sudo)登录。 要禁用Unix套接字认证并启用本机密码,请遵循以下步骤。

以MariaDB根用户身份登录。

sudo mysql -u root -p

无需密码

use mysql;

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

flush privileges;

quit;

安装PHP

默认情况下,Apache服务器仅支持提供静态HTML文件,不支持PHP。 要获得PHP支持,您需要安装PHP和用于MariaDB / MySQL的PHP​​扩展软件包。

在这里,我们将在Ubuntu 20.04上安装PHP 7.4。

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

重新启动Apache Web服务器。

sudo systemctl restart apache2

现在,跳到“测试LAMP堆栈”。

方法2:在单个命令中安装LAMP堆栈

现在,我们将继续在Ubuntu 20.04上安装AMP(Apache v2.4,PHP v7.4和MySQL v8.0)。

使用apt命令更新存储库缓存。

sudo apt update

粘贴到终端中的行下方,然后按Enter。

sudo apt install -y lamp-server^

上面的命令将搜索并安装LAMP服务器所需的软件包。 等待几分钟,以完成LAMP堆栈的安装。

Apache的默认文档根目录是/ var / www / html,而主要配置文件是/etc/apache2/apache2.conf。 可以在/ etc / apache2 / *-enabled目录中找到其他配置,例如虚拟主机和模块。

运行mysql_secure_installation命令以保护MySQL安装。

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  

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

New password: xxx 

Re-enter new password: xxx 

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  
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  
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  
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  
 - 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  
Success.

All done!

从Ubuntu存储库安装后,MySQL对MySQL根用户使用Unix套接字身份验证机制。 这样,MySQL root用户只能从Unix套接字(#提示符或sudo)登录。 要禁用Unix套接字认证并启用本机密码,请遵循以下步骤。

以MariaDB根用户身份登录。

sudo mysql -u root -p

无需密码

use mysql;

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

flush privileges;

quit;

测试LAMP堆栈

为了测试PHP,我们将一个PHP文件放在Apache的默认文档根目录下。

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

现在打开网络浏览器,然后在网址中键入以下内容以访问测试页面:

http://your-ip-addr.ess/info.php

该页面如下所示。


PHP信息

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

数据库支持
数据库支持

结论

就这样。 我希望到目前为止,您在Ubuntu 20.04上具有LAMP堆栈。 请在评论部分分享您的反馈。

Sidebar