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

时间:2020-03-21 11:45:16  来源:igfitidea点击:

正如我们所知道的, LAMP 堆栈是一个流行的开源Web开发平台,可用于运行和部署动态和基于Web的应用程序。
通常, LAMP 堆栈由Apache WebServer,MariadB/MySQL数据库,PHP/Python/Perl编程语言组成。
灯是Linux,Mariadb/MySQL,PHP/Python/Perl的首字母缩略词。
在本教程中,我们将看到如何在Ubuntu 18.04 LTS服务器中安装Apache,MariaDB,PHP(Lamp Stack)。

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

出于本教程的目的,我将使用以下TestBox。

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

1.安装Apache WebServer

要安装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 2016-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
May 06 11:42:35 ubuntuserver systemd[1]: Starting The Apache HTTP Server...
May 06 11:42:35 ubuntuserver systemd[1]: Started The Apache HTTP Server.

Apache服务已加载和运行!

1.1调整防火墙以允许Apache Web服务器

缺省情况下,如果我们在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完整"配置文件,我们将看到它使流量能够向端口80和443:

$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"

现在,打开Web浏览器并导航到http://localhost /或者http://IP地址/。

如果我们看到上面的屏幕,那么我们很高兴。
Apache服务器正在工作!

2.安装MariaDB.

MariaDB是替换MySQL数据库服务器的替换。

要安装它,请运行:

$sudo apt install mariadb-server mariadb-client

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

首先,添加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] http://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 2016-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
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: mysql
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: performance_schema
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: Phase 6/7: Checking and upgrading tables
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: Processing databases
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: information_schema
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: performance_schema
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: Phase 7/7: Running 'FLUSH PRIVILEGES'
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3906]: OK
May 06 11:52:13 ubuntuserver /etc/mysql/debian-start[3975]: Checking for insecure root accounts.
May 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键接受默认值。

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后,在Apache根文档文件夹中创建Info.php文件。
通常,apache根文档文件夹将是/var/www/html /或者/var/var/www /在大多数基于Debian的Linux发行版中。
在Ubuntu 18.04 LTS中,它是/var/www/html /。

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

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

添加以下行:

<?php
phpinfo();
?>

保存并退出文件。
重新启动Apache服务以生效更改。

$sudo systemctl restart apache2

现在,打开Web浏览器并在地址列中键入:http://ip-address/info.php。

我们将对如下屏幕感到满意。

安装PHP模块

为了提高PHP的功能,我们可以安装一些另外的PHP模块。

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

$sudo apt-cache search php- | less

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

要查找任何特定PHP模块的详细信息,例如PHP-GD,运行:

$sudo apt-cache show php-gd

要安装PHP模块运行:

$sudo apt install php-gd

要安装所有模块(但不必要),运行:

$sudo apt-get install php*

安装任何PHP模块后,请勿忘记重新启动Apache服务。

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