网站搜索

如何在 FreeBSD 上安装 Apache、MariaDB 和 PHP (FAMP) 堆栈


本指南将介绍如何在 FreeBSD 操作系统中安装和配置 FBAMP,这类似于 Linux 上的 LAMP 堆栈。 FBAMP 是一个缩写词,代表基于 FreeBSD 操作系统的软件集合,Apache HTTP 服务器,互联网上最流行的开源 Web 服务器,MariaDB< 关系数据库管理系统 (RDBMS),MySQL 数据库引擎和 PHP 服务器端的分支。

要求

  1. 全新安装 FreeBSD
  2. FreeBSD 初始配置
  3. 直接控制台访问或远程连接到 FreeBSD 时的 SSH。
  4. 在网络接口上配置的静态 IP 地址。

第 1 步:在 FreeBSD 上安装 Apache

1.我们要安装的第一个服务是Apache HTTP服务器。默认情况下,FreeBSD 为 Apache Web 服务器提供了具有不同运行时工作模块的多个版本。

这些版本被预编译成二进制包并由 FreeBSD PORTS 存储库提供。要显示 PORTS 提供的所有 Apache 软件包二进制文件,请发出以下命令。

ls /usr/ports/www/ | grep apache

您还可以通过发出以下命令在 FreeBSD 上搜索可用的预编译 Apache 软件包。

pkg search apache2

2. 接下来,通过发出以下命令安装最新版本的 Apache HTTP 服务器以及所有必需的模块。

pkg install apache24

3. 在系统上安装 Apache Web 服务器后,发出以下命令以在 FreeBSD 中启用系统范围内的守护程序。

sysrc apache24_enable="yes"

启用 Apache 守护进程的另一种方法是手动编辑并附加 /etc/rc.conf 文件中的 apache24_enable="yes" 行,如下面的屏幕截图所示。

4. 最后,要测试 Web 服务器是否正常工作,请通过发出以下命令启动 Apache 守护进程,并将浏览器指向 FQDN 的服务器 IP 地址( http://IP-orFQDN),如下面的屏幕截图所示。

service apache24 start

FreeBSD 11.x 中 Apache Web 服务器的默认 webroot 目录位于 /usr/local/www/apache24/data/ 系统路径中。在那里您会发现一个小的 index.html 文件,您可以根据需要进行编辑。

第 2 步:在 FreeBSD 上安装 PHP

5. FreeBSD 11.x 提供了多个版本的 PHP 解释型服务器端语言,打包在预编译的二进制文件中。要获取 FreeBSD Ports 存储库提供的所有可用 PHP 版本包的列表,请发出以下命令。

ls /usr/ports/lang/ | grep php

搜索所有可用 FreeBSD PHP 软件包版本的另一种方法是运行以下命令。

pkg search -o php

6. 要搜索 FreeBSD 为特定 PHP 版本(当前为 57 版本)提供的所有可用二进制文件,请运行以下命令。使用 less 命令来缩小和浏览输出。

pkg search php5 |less
pkg search php7

7. 要更具体地了解自定义 PHP 版本提供的模块,请按如下所述运行以下命令,该命令将显示 PHP 7.1 版本的所有可用模块。

pkg search php71

8. 在本指南中,我们将为 FBAMP 堆栈安装 PHP 7.1 版本。发出以下命令来安装 PHP 以及典型 CMS 安装所需的一些最重要的模块。

pkg install php71 mod_php71 php71-mbstring php71-mcrypt php71-zlib php71-curl php71-gd php71-json 

9. 接下来,我们需要在 /usr/local/etc/apache24/Includes/ 中为 Apache Web 服务器创建 php.conf 配置文件> 包含以下内容的系统路径。

nano /usr/local/etc/apache24/Includes/php.conf

将以下行添加到 php.conf 文件中。

<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>

10. 为了测试 PHP 网关是否按预期与 Apache Web 服务器一起工作,请在 /usr/local/www/ 中创建 PHP info.php 文件apache24/data/system路径,Apache Web服务器默认的Web文档根路径。

echo '<?php phpinfo();  ?>' | tee -a /usr/local/www/apache24/data/info.php

重新启动 Apache 守护进程以应用更改。

service apache24 restart

接下来,在浏览器中访问以下 URI 以查看 PHP 摘要。

http://IP-or-FQDN/info.php 

11. 为了激活 PHP ini 配置文件以进行生产,请发出以下命令。您可以修改 php.ini 生产文件以更改 FBAMP 堆栈中的各种 PHP 设置。

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini-production.bakup
ln -s /usr/local/etc/php.ini-production /usr/local/etc/php.ini

第 3 步:在 FreeBSD 上安装 MariaDB

12. FBAMP 堆栈中最后缺少的组件是 MySQL 数据库服务器。 FreeBSD 11.x 为不同的数据库提供了1000 多个软件包。

要显示可用于 MariaDBMySQL 数据库的组件,请发出以下命令。在本指南中,我们将通过 MySQL(现在由 Oracle 拥有和积极开发)安装 MariaDB 数据库。

ls -al /usr/ports/databases/ | grep mariadb
pkg search -o mariadb
ls -al /usr/ports/databases/ | grep mysql
pkg search -o mysql5

13. 在本指南中,我们将在 FreeBSD 中安装最新版本的 MariaDB 数据库服务器,目前以 mariadb102 二进制包版本为代表。

运行以下命令以安装 MariaDB 服务器和客户端以及通过 Apache 服务器网关访问数据库所需的 PHP 7.1 模块。

pkg install mariadb102-server mariadb102-client php71-mysqli

14. 接下来,在系统范围内启用 MariaDB 服务器并通过运行以下命令启动数据库守护进程。

sysrc mysql_enable="yes" 
service mysql-server start

15.要保护数据库,请运行mysql_secure_installation脚本。使用以下脚本输出摘录来强化 MariaDB。

/usr/local/bin/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!

16. 默认情况下,MariaDB 守护进程在端口 3306/TCP 上监听本地主机外部的网络连接。运行 netstat、lsof 或 sockstat 命令来获取 MariaDB 套接字状态。这种配置很危险,会使服务遭受外部网络攻击。

lsof -i4 -i6
sockstat -4 -6

17.如果您不需要远程访问 MariaDB,请通过发出以下命令确保 MariaDB 守护进程仅侦听本地主机。随后,重新启动 MariaDB 服务以应用更改。

sysrc mysql_args="--bind-address=127.0.0.1"
service mysql-server restart
or
/usr/local/etc/rc.d/mysql-server restart

18. 再次运行 netstat、lsof 或 sockstat 命令以列出 MariaDB 网络套接字。套接字现在应该在本地主机上绑定并侦听,如下图所示。

lsof -i4 | grep mysql
netstat -an | grep 3306
sockstat -4 | grep 3306

19. 要从控制台测试 MariaDB 数据库连接,请发出以下命令。在提示符中输入 MySQL root 密码,默认数据库列表应显示在控制台屏幕中,如下图所示。

mysql -u root -p -e "show databases"

就这样!您已经在 FreeBSD 中成功安装了带有 MariaDB 数据库和 PHP 解释器的 Apache Web 服务器。您现在可以立即开始部署 WordPress 网站。

在下一个教程中,我们将讨论一些高级 FPBAMP 主题,例如如何启用和创建 Apache 虚拟主机、启用 .htaccess 文件所需的重写模块以正常运行以及如何使用自签名证书或 Let's Encrypt 实体提供的免费证书。