网站搜索

如何在 FreeBSD 上安装 Nginx、MariaDB 和 PHP (FEMP) 堆栈


本教程将指导您如何在 FreeBSD 11.x 最新版本中安装和配置 FBEMP。 FBEMP 是一个缩写词,描述了以下软件集合:

FreeBSD 11.1 类 Unix 发行版、Nginx Web 服务器、MariaDB 关系数据库管理系统(MySQL 的社区分支)和在服务器端运行的 PHP 动态编程语言。

要求

  1. 安装 FreeBSD 11.x
  2. 安装 FreeBSD 后要做的 10 件事

第 1 步:在 FreeBSD 上安装 Nginx Web 服务器

1. 我们将为 FreeBSD 中的 FBEMP 堆栈安装的第一个服务是 Web 服务器,由 Nginx 代表> 软件。

Nginx Web 服务器在 FreeBSD 11.x PORTS 中提供了更多预编译的软件包。为了从 Ports 存储库获取 Nginx 二进制文件列表,请在服务器终端中发出以下命令。

ls /usr/ports/www/ | grep nginx
pkg search -o nginx

2. 在此特定配置中,我们将通过发出以下命令来安装 Nginx 的主软件包版本。 pkg 软件包管理器会询问您是否要继续安装 nginx 软件包。回答“是”(命令行中的y)即可开始安装过程。

pkg install nginx

3. 在系统中安装 Nginx Web 服务器包后,执行以下命令以在系统范围内启用守护程序并启动系统中的服务。

sysrc nginx_enable="yes"
service nginx start

4. 接下来,使用 sockstat 命令,通过发出以下命令验证 Nginx 服务网络套接字(如果它们绑定在 80/TCP 端口上)命令。 sockstat 命令的输出将通过 grep 实用程序进行管道传输,以便将返回结果仅减少为 nginx 字符串。

sockstat -4 | grep nginx

5.最后,在网络中的台式电脑上打开浏览器,通过HTTP协议访问Nginx默认网页。在浏览器的 URL 字段中写入您机器的 FQDN 或您的域名或服务器的 IP 地址,以请求 Nginx Web 服务器默认网页。您的浏览器中应显示消息“欢迎使用 nginx!”,如下面的屏幕截图所示。

http://yourdomain.com
http://your_server_IP
http://your_machine_FQDN

6. Nginx Web 内容的默认 weboot 目录位于 /usr/local/www/nginx/ 绝对系统路径中。您应该在此位置为您的网站创建、复制或安装 Web 内容文件,例如 .html.php 文件。

要更改此位置,请编辑 nginx 主配置文件并更改 root 指令以反映新的 Webroot 路径。

nano /usr/local/etc/nginx/nginx.conf

在这里,搜索并更新以下行以反映您的新 Webroot 路径:

root	/path/to/new/webroot;

第 2 步:在 FreeBSD 上安装 PHP

7.Apache HTTP服务器不同,Nginx不具备本地处理PHP代码的能力。作为回报,Nginx Web 服务器将 PHP 请求传递给 PHP 解释器,例如 php-fpm FastCGI 守护进程,该守护进程检查并执行代码。然后,生成的代码返回到 Nginx,Nginx 将代码重新组装回请求的 html 格式,并将代码进一步发送到访问者的 Web 浏览器。

FreeBSD 11.x Ports 存储库为 PHP 编程语言提供多个二进制版本,例如 PHP 5.6PHP 7.0PHP 7.1< 发布。要显示 FreeBSD 11.x 中所有可用的预编译 PHP 版本,请运行以下命令。

pkg search -o php
ls /usr/ports/lang/ | grep php

8. 您可以选择安装您认为最适合您系统中运行的 Web 应用程序的 PHP 版本。但是,在本指南中我们将安装 PHP 最新版本。

要安装 PHP 7.1 版本以及各种 Web 应用程序所需的一些 PHP 重要模块,请运行以下命令。

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

9. 在系统中安装 PHP 软件包后,打开 Nginx 的 PHP-FPM 配置文件,并调整用户和组值以匹配 Nginx 运行时上的值用户,即 www。首先,使用以下命令备份文件。

cp /usr/local/etc/php-fpm.d/www.conf{,.backup}

然后,打开文件并更新以下行,如以下示例所示。

user = www
group = www

10. 另外,通过发出以下命令创建用于生产的 PHP 配置文件。在此文件中,您可以进行自定义更改,这些更改将在运行时应用于 PHP 解释器。

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

例如,更改 PHP 解释器的 date.timezone 设置,以更新您的计算机物理位置,如下例所示。 PHP 时区列表可以在这里找到:http://php.net/manual/en/timezones.php。

vi /usr/local/etc/php.ini

添加以下时区(根据您所在的国家/地区设置时区)。

date.timezone = Europe/London

您还可以调整其他 PHP 变量,例如上传文件的最大文件大小,可以通过修改以下值来增加:

upload_max_filesize = 10M
post_max_size = 10M

11.完成 PHP 的自定义设置后,启用并启动 PHP-FPM 守护进程,以便通过发出以下命令应用新配置。

sysrc php_fpm_enable=yes
service php-fpm start

12. 默认情况下,FreeBSD 中的 PHP-FPM 守护进程绑定在端口 9000/TCP 上的本地网络套接字上。要显示 PHP-FPM 网络套接字,请执行以下命令。

sockstat -4 -6| grep php-fpm

13. 为了让 Nginx Web 服务器将 PHP 脚本传递给正在侦听 127.0.0.1:9000 套接字的 FastCGI 网关服务器,打开 Nginx 主配置文件并添加以下代码块,如以下示例所示。

vi /usr/local/etc/nginx/nginx.conf

nginx 的 FastCGI 代码块:

 location ~ \.php$ {
        root	/usr/local/www/nginx;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;    
        include        fastcgi_params;
        	}

14. 为了查看服务器当前的 PHP 信息,请通过发出以下命令在 Nginx weboot 路径中创建 info.php 文件。

echo "<?php phpinfo(); ?>" | tee /usr/local/www/nginx/info.php

15. 然后,测试并重新启动 Nginx 守护进程以应用 PHP FastCGI 设置并在浏览器中访问 info.php 页面。

nginx -t # Test nginx configuration file for syntax errors
service nginx restart

相应地替换以下链接中的 IP 地址或域名。 PHP 信息页面应显示如下屏幕截图所示的信息。

http://yourdomain.com/info.php
http://server_IP-or-FQDN/info.php

第 3 步:在 FreeBSD 上安装 MariaDB

16. 数据库中 FEMP 堆栈中缺少最后一个组件。 MariaDB/MySQL 是与用于部署动态网站的 Nginx Web 服务器最相关的开源 RDBMS 软件之一。

实际上,MariaDB/MySQL 是世界上最常用的关系数据库之一。通过搜索FreeBSD Ports,您可以找到MariaDB/MySQL的多个版本。

在本指南中,我们将安装 MariaDB 数据库,它是 MySQL 数据库的社区分支。要搜索 MariaDB 的可用版本,请在终端中发出以下命令。

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

17. 要安装最新版本的 MariaDB 数据库服务器,请执行以下命令。您还应该安装 PHP 脚本用于连接 MySQL 的 PHP 关系数据库驱动程序模块。

pkg install mariadb102-server php71-mysqli

18.数据库安装完成后,运行以下命令启用MySQL守护进程并启动数据库服务。

sysrc mysql_enable="YES" 
service mysql-server start

19. 另外,请确保重新启动 PHP-FPM 守护进程以加载 MySQL 驱动程序扩展。

service php-fpm restart
20. On the next step, secure MariaDB database by launching mysql_secure_installation script. Use the below sample of the installation script in order to answer the questions. Basically, say yes (y) for all asked questions to secure the database and type a strong password for MySQL root user.
/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!

21. 要从控制台测试 MariaDB 数据库连接,请执行以下命令。

mysql -u root -p -e "show status like ‘Connections’"

22. 为了进一步保护 MariaDB(默认情况下侦听 0.0.0.0:3306/TCP 套接字上的传入网络连接),请发出以下命令来强制该服务绑定到环回接口并完全禁止远程访问。然后,重新启动 MySQL 服务以应用新配置。

sysrc mysql_args="--bind-address=127.0.0.1"
service mysql-server restart

通过运行 netstat 命令验证 localhost 绑定是否已成功应用,如下例所示。

netstat -an -p tcp

就这样!您已经在 FreeBSD 中成功安装了 Nginx Web 服务器、MariaDB 关系数据库和 PHP 服务器端编程语言。您现在可以开始构建动态网页来为访问者提供 Web 内容。