王尘宇王尘宇

研究百度干SEO做推广变成一个被互联网搞的人

在Ubuntu中安装Apache、MySQL、PHP套件


LAMP 套件是一种流行的开源 Web 开发平台,可用于运行和部署动态网站和基于 Web 的应用程序。

• 来源:linux.cn • 作者:Sk • 译者:Hacker •

(本文字数:9231,阅读时长大约:11 分钟)

LAMP套件是一种流行的开源 Web 开发平台,可用于运行和部署动态网站和基于 Web 的应用程序。通常,LAMP 套件由 Apache Web 服务器、MariaDB/MySQL 数据库、PHP/Python/Perl 程序设计(脚本)语言组成。 LAMP 是Linux,MariaDB/MYSQL,PHP/Python/Perl 的缩写。 本教程描述了如何在 Ubuntu 18.04 LTS 服务器中安装 Apache、MySQL、PHP(LAMP 套件)。

就本教程而言,我们将使用以下 Ubuntu 测试。

操作系统:Ubuntu 18.04.1 LTS Server Edition

IP 地址:192.168.225.22/24

1. 安装 Apache Web 服务器

首先,利用下面命令更新 Ubuntu 服务器:

$ sudo apt update$ sudo apt upgrade

然后,安装 Apache Web 服务器(命令如下):

$ sudo apt install apache2

检查 Apache Web 服务器是否已经运行:

$ sudo systemctlstatusapache2

输出结果大概是这样的:

apache2.service-TheApacheHTTPServerLoaded:loaded(/lib/systemd/system/apache2.service;enabled;vendor preset:enDrop-In:/lib/systemd/system/apache2.service.d└─apache2-systemd.confActive:active(running)sinceTue2019-02-0510:48:03UTC;1min5sagoMain PID:2025(apache2)Tasks:55(limit:2320)CGroup:/system.slice/apache2.service├─2025/usr/sbin/apache2-kstart├─2027/usr/sbin/apache2-kstart└─2028/usr/sbin/apache2-kstartFeb0510:48:02ubuntuserversystemd[1]:StartingTheApacheHTTPServer...Feb0510:48:03ubuntuserverapachectl[2003]:AH00558: apache2:CouldnotreliablyFeb0510:48:03ubuntuserversystemd[1]:StartedTheApacheHTTPServer.

祝贺你! Apache 服务已经启动并运行了!!

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

默认情况下,如果你已在 Ubuntu 中启用 UFW 防火墙,则无法从远程系统访问 Apache Web 服务器。 必须按照以下步骤开启 http 和 https 端口。

首先,使用以下命令列出 Ubuntu 系统上可用的应用程序配置文件:

$ sudo ufw applist

输出结果:

Availableapplications:ApacheApacheFullApacheSecureOpenSSH

如你所见,Apache 和 OpenSSH 应用程序已安装 UFW 配置文件。你可以使用 ufw app info "Profile Name" 命令列出有关每个配置文件及其包含的规则的信息。

让我们研究一下 Apache Full 配置文件。 为此,请运行:

$ sudo ufw app info"Apache Full"

输出结果:

Profile:ApacheFullTitle:WebServer(HTTP,HTTPS)Description:Apachev2isthenextgenerationoftheomnipresentApachewebserver.Ports:80,443/tcp

如你所见,Apache Full 配置文件包含了启用经由端口80443的传输规则:

现在,运行以下命令配置允许 HTTP 和 HTTPS 传入通信:

$sudo ufw allow in "Apache Full"RulesupdatedRulesupdated (v6)

如果你不想允许 HTTP 通信,而只允许 HTTP(80) 通信,请运行:

$ sudo ufw app info"Apache"

1.2 测试 Apache Web 服务器

现在,打开 Web 浏览器并导航到 http://localhost/ 或 http://IP-Address/ 来访问 Apache 测试页。

如果看到上面类似的显示内容,那就成功了。 Apache 服务器正在工作!

2. 安装 MySQL

在 Ubuntu 安装 MySQL 请运行:

$ sudo aptinstallmysql-server

使用以下命令验证 MySQL 服务是否正在运行:

$ sudo systemctlstatusmysql

输出结果:

mysql.service-MySQLCommunityServerLoaded:loaded(/lib/systemd/system/mysql.service;enabled;vendor preset:enabActive:active(running)sinceTue2019-02-0511:07:50UTC;17sagoMain PID:3423(mysqld)Tasks:27(limit:2320)CGroup:/system.slice/mysql.service└─3423/usr/sbin/mysqld--daemonize--pid-file=/run/mysqld/mysqld.pidFeb0511:07:49ubuntuserversystemd[1]:StartingMySQLCommunityServer...Feb0511:07:50ubuntuserversystemd[1]:StartedMySQLCommunityServer.

MySQL 正在运行!

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

默认情况下,MySQL root 用户密码为空。你需要通过运行以下脚本使你的 MySQL 服务器安全:

$ sudo mysql_secure_installation

系统将询问你是否要安装 VALIDATE PASSWORD plugin(密码验证插件)。该插件允许用户为数据库配置强密码凭据。如果启用,它将自动检查密码的强度并强制用户设置足够安全的密码。禁用此插件是安全的。但是,必须为数据库使用唯一的强密码凭据。如果不想启用此插件,只需按任意键即可跳过密码验证部分,然后继续其余步骤。

如果回答是 y,则会要求你选择密码验证级别。

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords

and improve security. It checks the strength of password

and allows the users tosetonlythose passwords whicharesecure enough. Would youliketosetupVALIDATEPASSWORDplugin?Press y|YforYes,anyotherkeyforNoy

可用的密码验证有 low(低)、 medium(中) 和 strong(强)。只需输入适当的数字(0 表示低,1 表示中,2 表示强密码)并按回车键。

There are three levels of password validation policy:LOWLength>=8MEDIUMLength>=8,numeric,mixedcase,andspecialcharactersSTRONGLength>=8,numeric,mixedcase,specialcharactersanddictionaryfilePleaseenter0=LOW,1=MEDIUMand2=STRONG:

现在,输入 MySQL root 用户的密码。请注意,必须根据上一步中选择的密码策略,为 MySQL root 用户使用密码。如果你未启用该插件,则只需使用你选择的任意强度且唯一的密码即可。

Pleasesetthepasswordforroot here.Newpassword:Re-enternewpassword:Estimated strengthofthepassword:50Doyou wishtocontinuewiththepasswordprovided?(Press y|YforYes,anyotherkeyforNo) : y

两次输入密码后,你将看到密码强度(在此示例情况下为 50)。如果你确定可以,请按 y 继续提供的密码。如果对密码长度不满意,请按其他任意键并设置一个强密码。我现在的密码可以,所以我选择了y。

对于其余的问题,只需键入 y 并按回车键。这将删除匿名用户、禁止 root 用户远程登录并删除 test(测试)数据库。

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 comeswithadatabasenamedtestthatanyone can access. Thisisalso intendedonlyfortesting,andshould be removedbeforemovingintoa production

environment.

Removetestdatabaseandaccesstoit? (Press y|YforYes,anyotherkeyforNo) : y- Droppingtestdatabase...

Success.

- Removingprivilegesontestdatabase...

Success.

Reloading the privilegetableswill ensure thatallchangesmade sofarwill take effect immediately.Reload privilegetablesnow? (Press y|YforYes,anyotherkeyforNo) : ySuccess.Alldone!

以上就是为 MySQL root 用户设置密码。

2.2 更改 MySQL 超级用户的身份验证方法

默认情况下,Ubuntu 系统的 MySQL root 用户为 MySQL 5.7 版本及更新的版本使用插件 auth_socket 设置身份验证。尽管它增强了安全性,但是当你使用任何外部程序(例如 phpMyAdmin)访问数据库服务器时,也会变得更困难。要解决此问题,你需要将身份验证方法从 auth_socket 更改为 mysql_native_password。为此,请使用以下命令登录到你的 MySQL 提示符下:

$ sudo mysql

在 MySQL 提示符下运行以下命令,找到所有 MySQL 当前用户帐户的身份验证方法:

SELECTuser,authentication_string,plugin,hostFROMmysql.user;

输出结果:

+------------------|-------------------------------------------|-----------------------|-----------+|user| authentication_string |plugin| host |+------------------|-------------------------------------------|-----------------------|-----------+|root| |auth_socket| localhost || mysql.session |*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE| mysql_native_password |localhost||mysql.sys| *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |mysql_native_password| localhost || debian-sys-maint |*F126737722832701DD3979741508F05FA71E5BA0| mysql_native_password |localhost|+------------------|-------------------------------------------|-----------------------|-----------+4rowsinset (0.00sec)

如你所见,Mysql root 用户使用 auth_socket 插件进行身份验证。

要将此身份验证更改为 mysql_native_password 方法,请在 MySQL 提示符下运行以下命令。 别忘了用你选择的强大唯一的密码替换 password。 如果已启用 VALIDATION 插件,请确保已根据当前策略要求使用了强密码。

ALTERUSERroot@localhostIDENTIFIEDWITHmysql_native_passwordBYpassword;

使用以下命令更新数据库:

FLUSHPRIVILEGES;

使用命令再次检查身份验证方法是否已更改:

SELECTuser,authentication_string,plugin,hostFROMmysql.user;

输出结果:

好!MySQL root 用户就可以使用密码进行身份验证来访问 mysql shell。

从 MySQL 提示符下退出:

exit

3. 安装 PHP

安装 PHP 请运行:

$ sudo aptinstallphp libapache2-mod-php php-mysql

安装 PHP 后,在 Apache 文档根目录中创建 info.php 文件。通常,在大多数基于 Debian 的 Linux 发行版中,Apache 文档根目录为 /var/www/html/ 或 /var/www/。Ubuntu 18.04 LTS 系统下,文档根目录是 /var/www/html/。

在 Apache 根目录中创建 info.php 文件:

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

在此文件中编辑如下内容:

phpinfo();?>

然后按下 ESC 键并且输入 :wq 保存并退出此文件。重新启动 Apache 服务使更改生效。

$ sudo systemctl restart apache2

3.1 测试 PHP

打开 Web 浏览器,然后导航到 URL http://IP地址/info.php 。

你就将看到 PHP 测试页面。

通常,当用户向 Web 服务器发出请求时,Apache 首先会在文档根目录中查找名为 index.html 的文件。如果你想将 Apache 更改为 php 文件提供服务而不是其他文件,请将 dir.conf 配置文件中的 index.php 移至第一个位置,如下所示:

$ sudo vi /etc/apache2/mods-enabled/dir.conf

上面的配置文件(dir.conf) 内容如下:

DirectoryIndexindex.html index.cgi index.pl index.php index.xhtml index.htmvim: syntax=apache ts=4 sw=4 sts=4 sr noet

将 index.php 移动到最前面。更改后,dir.conf 文件内容看起来如下所示。

DirectoryIndexindex.php index.html index.cgi index.pl index.xhtml index.htmvim: syntax=apache ts=4 sw=4 sts=4 sr noet

然后按下 ESC 键并且输入 :wq 保存并关闭此文件。重新启动 Apache 服务使更改生效。

$ sudo systemctl restart apache2

3.2 安装 PHP 模块

为了增加 PHP 的功能,可以安装一些其他的 PHP 模块。

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

$ sudo apt-cachesearchphp- |less

输出结果:

使用方向键浏览结果。要退出,请输入 q 并按下回车键。

要查找任意 php 模块的详细信息,例如 php-gd,请运行:

$ sudo apt-cacheshowphp-gd

安装 PHP 模块请运行:

$ sudo apt install php-gd

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

$ sudo apt-getinstall php*

安装任何 php 模块后,请不要忘记重新启动 Apache 服务。要检查模块是否已加载,请在浏览器中打开 info.php 文件并检查是否存在。

接下来,你可能需要安装数据库管理工具,以通过 Web 浏览器轻松管理数据库。如果是这样,请按照以下链接中的说明安装 phpMyAdmin。

祝贺你!我们已经在 Ubuntu 服务器中成功配置了 LAMP 套件。


via: www.ostechnix.com

作者: SK 选题: lujun9972 译者: stevenzdg988 校对: wxy

本文由 LCTT 原创编译, Linux中国 荣誉推出

点击了解更多可访问文内链接

相关文章

评论列表

发表评论:
验证码

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。