如何在Ubuntu 20.04(Focal Fossa)上安装LAMP堆栈

作为用户,本指南描述了如何在Ubuntu 20.04(Focal Fossa)Linux系统上安装和设置Apache,MariaDB和PHP(LAMP堆栈)。 LAMP是–的首字母缩写。 的Linux阿帕奇MySQL的/ MariaDB的的PHP。 LAMP堆栈不是单个软件包,而是用于增强Web应用程序和网站的一组开源工具。每个组件可以单独使用以提供应用程序。

在Ubuntu 20.04(Focal Fossa)上安装LAMP堆栈

LAMP Stack由以下开源软件应用程序组成:

  • 的Linux –这是承载应用程序的操作系统。
  • 阿帕奇 – Apache HTTP是免费的,开源的,跨平台的Web服务器。
  • MySQL的/ MariaDB的 –开源关系数据库管理系统。
  • 的PHP –用于开发Web应用程序的编程/脚本语言。

您可以使用本地,云或专用服务器虚拟机在Ubuntu 20.04(Focal Fossa)操作系统上安装和配置LAMP堆栈。此设置中使用的使用帐户必须具有sudo特权才能安装软件,编辑配置文件和管理服务。

步骤1:更新Ubuntu 20.04(Focal Fossa)

我们建议您在开始在Ubuntu 20.04上安装LAMP Stack软件包之前,使存储库和软件包保持最新。

sudo apt update && sudo apt -y upgrade

步骤2:安装MariaDB数据库服务器

MariaDB是从MySQL分支出来的关系数据库管理系统。免费和开源。使用以下指南进行安装。

如何在Ubuntu 20.04(Focal Fossa)上安装MariaDB服务器

安装后,保护数据库服务器。这也是:

  • 设置强密码
  • 删除匿名用户
  • 禁用root用户的远程登录。
  • 测试数据库删除和访问

运行以下命令以保护数据库服务器的安全:

$ 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:  Re-enter new 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  … 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数据库安装。

$ mysql -u root -p
 Enter password: 
 Welcome to the MariaDB monitor.  Commands end with ; or g.
 Your MariaDB connection id is 67
Server version: 10.3.19-MariaDB-1 Ubuntu 20.04
 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 MariaDB [(none)]> SELECT VERSION();
 +-------------------+
 | VERSION()         |
 +-------------------+
 | 10.3.19-MariaDB-1 |
 +-------------------+
 1 row in set (0.001 sec)
 MariaDB [(none)]> 

步骤3:安装Apache Web服务器

Apache Web服务器软件包可在Ubuntu 20.04官方存储库中获得。您需要做的就是使用sudo运行安装命令。

sudo apt install -y apache2 apache2-utils

检查Apache的版本和版本。

$ sudo apache2 -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2019-08-14T14:36:32

该服务在安装后自动启动。

$ systemctl status apache2
 ● apache2.service - The Apache HTTP Server
      Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
      Active: active (running) since Tue 2019-12-24 11:17:28 UTC; 20s ago
        Docs: https://httpd.apache.org/docs/2.4/
    Main PID: 721 (apache2)
       Tasks: 55 (limit: 614)
      Memory: 4.9M
      CGroup: /system.slice/apache2.service
              ├─721 /usr/sbin/apache2 -k start
              ├─723 /usr/sbin/apache2 -k start
              └─724 /usr/sbin/apache2 -k start
 Dec 24 11:17:28 ubuntu20 systemd[1]: Starting The Apache HTTP Server…
 Dec 24 11:17:28 ubuntu20 apachectl[720]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.10.1.122. Se>
 Dec 24 11:17:28 ubuntu20 systemd[1]: Started The Apache HTTP Server.

进行更改后,可以使用systemctl命令重新启动或重新加载服务。

sudo systemctl reload apache2sudo systemctl enable apache2

为了能够在启动时启动服务,请使用:

sudo systemctl is-enabled apache2

要查看Apache服务器的完整状态,请使用 apache2ctl 命令

$ sudo apache2ctl fullstatus 

输出看起来像这样:

在浏览器中打开服务器的IP地址,并查看默认的Apache页面。

如何在Ubuntu 20.04(Focal Fossa)上安装LAMP堆栈

步骤4:在Ubuntu 20.04上安装PHP

由于同时安装了Apache和MariaDB,因此缺少的部分是PHP。安装常用的PHP和标准扩展。在Ubuntu 20.04上安装的PHP版本是 PHP 7.3

sudo apt install php libapache2-mod-php php-cli php-fpm php-json php-pdo php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath

如果尚未启用Apache模块,请启用它,然后重新启动Web服务器。

sudo a2enmod php7.3 

检查PHP版本。

$ php -v
PHP 7.3.11-0ubuntu1 (cli) (built: Nov 20 2019 14:21:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
     with Zend OPcache v7.3.11-0ubuntu1, Copyright (c) 1999-2018, by Zend Technologies

创建一个php脚本来测试LAMP堆栈安装。

echo "" | sudo tee /var/www/html/phpinfo.php

打开服务器IP和URL。 http://[ServerIP/hostname]/phpinfo.php

如何在Ubuntu 20.04(Focal Fossa)上安装LAMP堆栈

这提供了有关PHP和Apache Web服务器的详细信息。总结了有关如何在Ubuntu 20.04 Linux系统上安装LAMP堆栈的指南。

其他有趣的指南:

如何在Debian 10(Buster)上用Apache安装phpMyAdmin

如何在5分钟内使用Prometheus和Grafana监视Apache Web服务器

在5分钟内使用Prometheus监视MySQL / MariaDB

Sidebar