快速搭建 LAMP 环境 Ubuntu 16.04 Apache MySQL | MariaDB PHP

原创

LAMP 是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写,即 Linux + Apache + MariaDB + PHP。掌握 LAMP 也是后端程序员的基本技能,面试、工作都要用到,本文将带你快速上手搭建 LAMP 环境。

本教程是针对 Ubuntu 16.04 版本写的,现在官方已经停止更新,推荐看更新的版本:
快速搭建 LAMP 环境 Ubuntu 20.04 Apache MySQL | MariaDB PHP

本指南版本说明

按照本指南,您可以顺利完成 LAMP 环境的搭建,最终获得以下运行环境:
* Ubuntu 16.04
* Apache 2.4
* MariaDB 10.1
* PHP 7.0

本教程假定您已经拥有 Ubuntu 16.04 LTS,接下来我们将开始安装这三款软件:PHP、MariaDB、Apache。

虽说这三款软件是相互独立的,但是推荐您先安装 PHP、MariaDB,最后再安装 Apache。
因为如果先安装 Apache,在没安装 PHP 之前,Apache 可能会泄密,例如直接把 db-conf.php 发给用户。是非常危险的行为。

安装 PHP

PHP(全称:PHP:Hypertext Preprocessor,即“PHP:超文本预处理器”)是一种开源的通用计算机脚本语言,尤其适用于网络开发并可嵌入 HTML 中使用。PHP 的语法借鉴吸收 C 语言、Java 和 Perl 等流行计算机语言的特点,易于一般程序员学习。PHP 的主要目标是允许网络开发人员快速编写动态页面,但 PHP 也被用于其他很多领域。

在招聘网站上,PHP 的职位也是不计其数。

要安装 PHP,请使用以下命令安装:(先更新服务器上的包索引,然后安装 PHP)

sudo apt update sudo apt install php php-fpm php-mysql

出现提示时,键入 Y 并 ENTER 确认安装。

libapache2-mod-php 是针对 Apache 的,如果您是 ngitx 可以跳过此模块。

如果你打算将来运行 WordPress,请安装以下 PHP 模块,这些模块 WordPress 都会用到:

$ sudo apt install php-mbstring php-curl php-imagick php-zip php-gd php-xml php-intl

本教程将要搭配 apache 使用,所以需要加装 libapache2-mod-php 模块:(nginx 用户可跳过)

$ sudo apt install libapache2-mod-php

您现在已经安装了 PHP 组件。接下来,我们来看看 PHP 的性能优化。

设置 PHP(选读,可跳过)

PHP 的设置文件保存在 /etc/php/7.0/apache2/php.ini 里,请打开此文件:

$ sudo vi /etc/php/7.0/apache2/php.ini

/etc/php/7.0/cli 的配置文件是 PHP-cli(command-line)命令行用的,与 Apache 无关,请勿修改。

php.ini 中最值得关注的模块是 Resource Limits(资源限制),这里重点介绍 3 个参数。

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30  # 1、单个 PHP 文件最大运行时间,单位:秒

; Maximum amount of time each script may spend parsing request data. It's a good
; idea to limit this time on productions servers in order to eliminate unexpectedly
; long running scripts.
; Note: This directive is hardcoded to -1 for the CLI SAPI
; Default Value: -1 (Unlimited)
; Development Value: 60 (60 seconds)
; Production Value: 60 (60 seconds)
; http://php.net/max-input-time
max_input_time = 60  # 2、单个 PHP 文件接收数据花费的最大时间,单位:秒

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M  # 3、单个 PHP 文件可以占用的最大内存,单位:MiB

这上面的 3 个参数默认其实都偏大,正常的 PHP 脚本执行大约都不到 1 秒,如果某个 PHP 运行真的需要 30 秒,那肯定是有问题需要调试了。
所以这里不是越大越好。当然,如果是新手,这 3 条可以保持默认。

启用、禁用 PHP 模块(选读,可跳过)

默认情况下 PHP 会帮你安装最常用的模块,但有时程序需要一些特殊模块就需要你添加了,先来看看有哪些模块可以用:

$ sudo apt search php7.0-*

假设我们要安装 zip 模块,则可以

$ sudo apt install php7.0-zip

如果你要用 MySQL(选读,可跳过)

PHP 7.0 默认使用 MySQLi 和 PHP pdo_mysql,禁用了 MySQL
但有些老程序需要 MySQL,所以你可以用过下列方法启用

启用xx模块

$ sudo phpenmod mcrypt

最后重启 PHP

$ sudo service php7.0-fpm restart

Apache

Apache HTTP 服务器是世界上使用最广泛的 Web 服务器。它提供了许多强大的功能,包括可动态加载的模块、强大的媒体支持以及与其他流行软件的广泛集成。
负责把你网上的文字、图片发给读者。

安装

首先让我们来安装 Apache,请键入以下命令:(注意不要复制到 $ 符号,下同)

sudo apt update sudo apt install apache2 -y

检查是否安装正确

然后输入在浏览器中输入服务器的 IP 或者您网站的域名即可。 就像这样:http://{your hostname} 就能看到欢迎页面了。
3333

如果您不知道服务器的 IP 地址,可以使用此命令查看:

$ hostname -I

管理 Apache 的常用命令(选读,可跳过)

查看可用模块(输入模块名即可启用,可以输入多个,还支持通配符)

$ sudo a2enmod [模块名(可不填)]

查看已启用模块(输入模块名即可禁用,可以输入多个,还支持通配符)

$ sudo a2dismod [模块名(可不填)]

查看可用虚拟主机 a2ensite(输入虚拟主机即可启用,可以输入多个,还支持通配符)

$ sudo a2ensite [虚拟主机名(可不填)]

查看已启用虚拟主机 a2dissite(输入虚拟主机即可禁用,可以输入多个,还支持通配符)

$ sudo a2dissite [虚拟主机名(可不填)]

重启启动 apache :

$ sudo service apache2 restart

修改 Apache 配置文件

$ sudo sudo vi /etc/apache2/apache2.conf

查看 Apache 错误日志

$ sudo tail -f /var/log/apache2/error.log

优化 Apache(选读,可跳过)

隐藏敏感信息 http 头(选读,可跳过)

现在网站的的头信息包含太多的敏感内容、黑客可以根据这些信息来寻找突破口,例如 telnet localhost 80 可以看到如下信息:

$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HEAD / HTTP /1.0
HTTP/1.1 400 Bad Request
Date: Thu, 27 Jan 2022 14:39:30 GMT
Server: Apache/2.4.18 (Ubuntu)
Connection: close
Content-Type: text/html; charset=iso-8859-1
Connection closed by foreign host.

我们可以看到,服务器是 Ubuntu,Web 服务器是 Apache/2.4.18,黑客拿到这些信息后,更容易攻破服务器,所以我们最好隐藏起来。

打开配置文件:

sudo vi /etc/apache2/conf-enabled/security.conf

搜索 ServerTokens,把值从 OS 改为 Prod,然后重新加载 Apache 配置:

$ sudo service apache2 reload

我们现在再来检查一下,现在只显示 Apache,不显示版本了:

telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'. HEAD / HTTP / 1.0
HTTP/1.1 400 Bad Request
Date: Thu, 27 Jan 2022 14:53:42 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=iso-8859-1
Connection closed by foreign host.

DDoS 防范(选读,可跳过)(这部分技术测试未通过)

您也许听说过 DDoS,中文叫“分布式拒绝服务攻击”,就像肯德基里的霸占座位但不点餐的社会闲散人员占领了餐厅,正常就餐的顾客反倒没有桌子用餐的情况。

Apache 自带一个叫 mod-evasive 的模块,可以简单防护。

mod-evasive 的安装

$ sudo apt install libapache2-mod-evasive -y

如果弹出 Postfix Configuration 对话框,

– 第一个选择“Internet Site”,选好后用 Tab 切换到 OK 键,继续。(如图)


– 第二页输入域名,比如 abc.com

  • 回车后继续

安装后是自动生效的,下面我们来配置参数。新建一个文本文件:

$ sudo vi /etc/apache2/conf-available/local-evasive.conf

加入以下配置

<IfModule mod_evasive20.c>
#哈希表大小(无需修改)
DOSHashTableSize 3097

#允许单IP的最大并发连接(瞬间值)
DOSPageCount 10

#允许单台机在指定秒数内访问同一页的次数。 
DOSSiteCount 50

 #单页请求检测时间(默认1秒)
DOSPageInterval 1

#全站请求检测时间(默认1秒)
DOSSiteInterval 1

#黑名单封禁时间(默认3600秒)
DOSBlockingPeriod 3600

# 还可以加入下面的配置,不写也行
#有新黑名单记录通知管理员(可修改为邮箱)
DOSEmailNotify youremail@yourdomain.com

#进黑名单执行命令
DOSSystemCommand "iptables -A INPUT -s %s -j DROP"

#日志文件存储路径
DOSLogDir "/var/lock/mod_evasive"
# 还可以加入下面“白名单”的配置
DOSWhitelist 127.0.0.1
DOSWhitelist 127.0.0.*
</IfModule>

然后保存并启用此配置文件:

sudo a2enconf local-evasive sudo service apache2 restart

接下来我们测试 DDoS 攻击测试:

cd /usr/share/doc/libapache2-mod-evasive/examples perl test.pl

可以看到错误信息如下:(技术测试通过后再补)

安装 MariaDB

截止发稿时,Ubuntu 官方提供的 MariaDB 版本是 10.0,这个版本有问题,因此我们使用 MariaDB 官方的安装文档来安装 10.1,
请先在 Ubuntu 上执行这些安装命令:

sudo apt update sudo apt install mysql-server

安装过程中 MariaDB 会问你 root 用户的密码,root 用户很关键,是系统的超管,所以密码一定要复杂一些,别被破解。

输入密码后,即可完成安装。接下来我们运行安全检查脚本,帮助你加固 MariaDB 的安全:

sudo mysql_secure_installation

接下来会问你几个问题,能选 yes 的全选 yes,不能选 yes 的就按 1。问题都是要不要检查密码强度,禁用 root 远程登录、是否马上

Securing the MySQL server deployment.

Enter password for user root: #输入你的 root 密码

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are 
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root.

Estimated strength of the password: 100 #你的密码强度很好,100分
Change the password for root ? ((Press y|Y for Yes, any other key for No) :n(输入 n 不要更改 root 密码)
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : y # 问你要不要删除匿名帐户,当然要,输入 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 # 问你要不要禁止 root 远程登录,当然要,输入 y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : y # 问你要不要删除测试数据库,当然要,输入 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? (Press y|Y for Yes, any other key for No) : y # 问你要不要立刻生效这些配置,当然要,输入 y
Success.

All done!

至此,数据库就安装好了。
至此就我们就把 LAMP 环境搭建好了,可以上传你的 PHP 测试了。

如果您还想构建更安全的服务器,可以阅读这边文章《十条安全》。

参考网址:
– https://downloads.mariadb.org/mariadb/repositories/#mirror=neusoft&distro=Ubuntu&distro_release=xenial–ubuntu_xenial&version=10.1
– https://www.howtoforge.com/tutorial/apache-with-php-fpm-on-ubuntu-16-04/

欢迎转载,但请勿用于任何商业用途,谢谢你!请标注以下信息『 原文出处:快速搭建 LAMP 环境 Ubuntu 16.04 Apache MySQL | MariaDB PHP - 张林海博客 http://zhanglinhai.com/archives/359 』