安装 Apache、MariaDB、PHP(LAMP 堆栈)在 Ubuntu 18.04 LTS 服务器中

你可能已经知道, stack 是一个流行的开源 Web 开发平台,可用于运行和部署动态网站和基于 Web 的应用程序。 通常,LAMP 堆栈包括 Apache 网络服务器、MariaDB/MySQL 数据库、PHP/Python/Perl 编程语言。 LAMP 是 大号内克斯, 咏叹调/YSQL, 生命值/ython/呃。 在本教程中,我们将看到如何安装 Apache、MariaDB、PHP(LAMP 堆栈)在 Ubuntu 18.04 LTS 服务器中。

安装 ApacheMariaDB, PHP (LAMP stack) 在 Ubuntu 18.04 LTS 服务器

出于本教程的目的,我将使用以下测试框。

  • 操作系统 : Ubuntu 18.04 64 位 LTS 服务器
  • IP地址 : 192.168.225.22/24

1.安装 Apache 网络服务器

安装 Apache Web 服务器,从终端运行以下命令:

$ sudo apt install apache2

检查是否 Apache Web 服务器是否正在运行:

$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Fri 2018-07-06 11:42:35 UTC; 27s ago
Main PID: 2079 (apache2)
Tasks: 55 (limit: 2322)
CGroup: /system.slice/apache2.service
├─2079 /usr/sbin/apache2 -k start
├─2081 /usr/sbin/apache2 -k start
└─2082 /usr/sbin/apache2 -k start

Jul 06 11:42:35 ubuntuserver systemd[1]: Starting The Apache HTTP Server...
Jul 06 11:42:35 ubuntuserver systemd[1]: Started The Apache HTTP Server.

Apache 服务已加载并运行!

1.1 调整防火墙允许 Apache 网络服务器

默认情况下,如果您在 Ubuntu 18.04 LTS 中启用了 UFW 防火墙,则无法从远程系统访问 apache Web 浏览器。 您必须按照以下步骤通过 UFW 允许 http 和 https 流量。

首先,让我们使用命令查看哪些应用程序安装了配置文件:

$ sudo ufw app list
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

如你看到的, Apache 和 OpenSSH 应用程序已安装 UFW 配置文件。

如果您查看 “Apache 满的” 配置文件,您将看到它启用了到端口的流量 80443

$ sudo ufw app info "Apache Full"
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Ports:
80,443/tcp

现在,运行以下命令以允许此配置文件的传入 HTTP 和 HTTPS 流量:

$ sudo ufw allow in "Apache Full"
Rules updated
Rules updated (v6)

如果您不想允许 https 流量,而只允许 http (80) 流量,请改为运行以下命令:

$ sudo ufw app info "Apache"

现在,打开您的网络浏览器并导航到 https://localhost/ 或者 https://IP 地址/.

如果您看到类似上面的屏幕,那么您就可以开始了。 Apache 服务器正在工作!

2. 安装 MariaDB

MariaDB 是 MySQL 数据库服务器的直接替代品。

要安装它,请运行:

$ sudo apt install mariadb-server mariadb-client

Ubuntu 官方存储库中的 MariaDB 版本可能已过时。 如果要安装最新的 MariaDB,请添加 MariaDB 官方仓库 对于 Ubuntu 并安装它,如下所示。

首先,添加 MariaDB 存储库并导入密钥,如下所示。

$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 
$ sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://sgp1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu bionic main'

添加存储库后,运行以下命令安装 MariaDB。

$ sudo apt update
$ sudo apt install mariadb-server

使用命令验证 MariaDB 服务是否正在运行:

$ sudo systemctl status mysql

样本输出:

● mariadb.service - MariaDB database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2018-07-06 11:52:12 UTC; 2min 39s ago
Main PID: 3869 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 27 (limit: 2322)
CGroup: /system.slice/mariadb.service
└─3869 /usr/sbin/mysqld

Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: mysql
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: performance_schema
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: Phase 6/7: Checking and upgrading tables
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: Processing databases
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: information_schema
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: performance_schema
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: Phase 7/7: Running 'FLUSH PRIVILEGES'
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: OK
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3975]: Checking for insecure root accounts.
Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3979]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables

mysql正在运行!

2.1 设置数据库管理用户(root)密码

在 MariaDB 安装期间,它将为管理用户帐户 (root) 设置密码。

如果您尝试使用命令手动设置密码:

$ 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): 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Enter current password for root (enter for none):

要解决此问题,请使用以下命令以 MySQL 数据库管理员身份登录:

$ sudo mysql -u root

登录到 MySQL 提示符后,一一运行以下命令。

use mysql;
update user set plugin='' where User="root";
flush privileges;
q

现在,您可以使用命令设置数据库管理密码:

$ mysql_secure_installation

Enter 密码密码,然后按 ENTER 键接受默认值。

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): # Press ENTER
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] # Press ENTER
New password: # Enter password
Re-enter new password: # Re-enter 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] # Press ENTER
 ... 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] # Press ENTER
 ... 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] # Press ENTER
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed! Not critical, keep moving...
 - 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] # Press ENTER
 ... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

就是这样。 已设置数据库管理用户帐户的密码。

3、安装PHP

要安装 PHP,请运行:

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

安装PHP后,创建 信息.php 文件中 Apache 根文档文件夹。 通常,apache根文件夹是 /var/www/html/ 或者 /var/www/ 在大多数基于 Debian 的 Linux 发行版中。 在 Ubuntu 18.04 LTS 中,它是 /var/www/html/.

在 apache 根文件夹中创建 info.php,如下所示。

$ sudo vi /var/www/html/info.php

添加以下行:

<?php
phpinfo();
?>

Save 并退出文件。 重新启动 apache 服务以使更改生效。

$ sudo systemctl restart apache2

现在,打开您的网络浏览器并输入: https://IP地址/info.php 在地址栏中。

您会对如下所示的屏幕感到满意。

安装 PHP 模块

为了改进 PHP 的功能,您可以安装一些额外的 PHP 模块。

要列出可用的 PHP 模块,请运行:

$ sudo apt-cache search php- | less

样本输出:

使用 和 箭头在结果之间上下移动。 要退出结果,请键入 q.

要查找任何特定 php 模块的详细信息,对于 example php-gd, 跑:

$ sudo apt-cache show php-gd

要安装 php 模块,请运行:

$ sudo apt install php-gd

要安装所有模块(虽然不是必需的),请运行:

$ sudo apt-get install php*

不要忘记重新启动 Apache 安装任何 php 模块后的服务。

恭喜! 我们已经在 Ubuntu 18.04 LTS 服务器中成功设置了 LAMP 堆栈。

  • 安装 ApacheMySQL, PHP (LAMP) 堆栈在 Ubuntu 18.04 LTS
  • 在 Ubuntu 18.04 LTS 中安装 Nginx、MariaDB、PHP(LEMP 堆栈)
  • 安装 ApacheMariaDB, Arch Linux 上的 PHP (LAMP) 堆栈
  • 在 Arch Linux 上安装 Nginx、MariaDB、PHP (LEMP) 堆栈

而且,这就是现在的全部。 如您所见,在 Ubuntu 中设置 LAMP 堆栈绝对简单直接。 任何经验有限的人都​​可以轻松设置 LAMP 堆栈。 更多好东西即将到来。 敬请关注!

干杯!

感谢您的光临!

帮助我们帮助您:

祝你有美好的一天!!