如何在Ubuntu 20.04(Focal Fossa)上安装 LAMP 堆栈
本教程将讨论如何在Ubuntu 20.04(焦索)Linux系统上安装和设置Apache,MariaDB和PHP(Lamp Stack)。
LAMP是 - Linux,Apache,MySQL/MariaDB和PHP的首字母缩写。
LAMP 堆栈不是单个包,而是一组用于电源Web应用程序和的开源工具。
每个组件可以独立使用以供应应用。
在Ubuntu 20.04上安装 LAMP 堆栈
LAMP 堆栈包括以下开源软件应用程序。
Linux - 这是托管应用程序的操作系统。
Apache - Apache HTTP是一个免费且开源的跨平台Web服务器。
MySQL/MariaDB - 开源关系数据库管理系统。
PHP - 用于开发Web应用程序的编程/脚本语言。
我们可以在前提下使用虚拟机,在云或者专用服务器中安装和配置Ubuntu 20.04(Focal FOSEA)操作系统上的 LAMP 堆栈。
此设置中使用的使用帐户需要sudo权限来安装软件,编辑配置文件和管理服务。
第1步:更新Ubuntu 20.04
在我们可以在Ubuntu 20.04上开始安装Lamp Stack Packages之前,建议将存储库和包装保持最新。
运行以下命令以更新系统。
sudo apt update && sudo apt -y upgrade
第2步:安装MariaDB数据库服务器
MariaDB是一个从MySQL分叉的关系数据库管理系统。
它是自由和开源的。
使用下面的教程安装它。
如何在Ubuntu 20.04上安装Mariadb Server(Focal Fossa)
安装后,请确保我们保护数据库服务器。
这包括:
设置强根密码
删除匿名用户
禁用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, 2016, 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服务器
Ubuntu 20.04官方存储库上有Apache Web服务器包。
所需的只是使用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 apache2 sudo systemctl enable apache2
使服务能够在启动时启动,使用
sudo systemctl is-enabled apache2
查看Apache服务器全部状态,使用 apache2ctl
命令。
$sudo apache2ctl fullstatus
输出应类似于下面。
打开浏览器上的服务器IP地址以查看默认的Apache页面。
第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-2016 The PHP Group Zend Engine v3.3.11, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.3.11-0ubuntu1, Copyright (c) 1999-2016, by Zend Technologies
创建一个php脚本以测试 LAMP 堆栈安装。
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php
打开服务器IP和URL: http://[ServerIP/hostname]/phpinfo.php