第 2 步 - 安装 Apache 网络服务器
我们将使用 pkg 命令安装 apache 2.4.
# pkg install apache24
如果我们第一次在操作系统中使用 pkg 命令,我们应该会收到安装包管理工具的通知,只需选择 y
# pkg install apache24 The package management tool is not yet installed on your system. Do you want to fetch and install it now? [y/N]: y
接下来,我们使用以下命令启用 apache 在启动时运行:
# sysrc apache24_enable=yes
使用以下命令启动 apache 服务:
# service apache24 start
测试 apache 网络服务:
在我们测试apache之前,我们需要配置apache配置。
使用 nano /usr/local/etc/apache24/httpd.conf 编辑此文件。
# nano /usr/local/etc/apache24/httpd.conf
根据 ip 地址或者主机名查找并编辑此变量,在本例中我们使用 ip 地址 192.168.14.126:
ServerName 192.168.14.126:80
退出并保存文件。
然后使用以下命令重新启动 apache 服务:
# service apache24 restart
现在,打开 Web 浏览器并导航到:http://IP-address/ 或者 http://localhost/。
我们应该会看到 Apache 测试页面。
准备工作
在我们开始阅读本教程之前,我们至少应该具备 linux 或者 unix 的基本知识,了解 linux 或者 unix 的基本 shell 命令,以 root 用户身份登录,当然我们需要在 PC 或者服务器上安装 FreeBSD 11.
第 6 步 - 安装 PHP 模块(扩展)
为了让 PHP 脚本充分发挥作用,有时我们需要安装一些另外的 PHP 模块(扩展),如果我们不需要安装任何扩展,则可以跳过此步骤。
要查看可用模块的列表,只需运行:
# pkg search php56
示例输出:
# pkg search php56 mod_php56-5.6.26 PHP Scripting Language php56-5.6.26 PHP Scripting Language php56-bcmath-5.6.26 The bcmath shared extension for php php56-bz2-5.6.26 The bz2 shared extension for php php56-calendar-5.6.26 The calendar shared extension for php php56-ctype-5.6.26 The ctype shared extension for php ...
我们可以从上面输出的注释部分验证每个模块的作用,或者只运行以下命令:
# pkg search -f php56-curl
示例输出:
# pkg search -f php56-curl php56-curl-5.6.26 Name : php56-curl Version : 5.6.26 Oroirn : ftp/php56-curl Architecture : freebsd:11:x86:64 Prefix : /usr/local ...
要安装 php 扩展,例如我们将安装 php56-curl,使用以下命令运行:
# pkg install php56-curl
要在安装后进行更改,需要重新启动 apache web 服务:
# service apache24 restart
现在我们已在 FreeBSD 11 上成功安装 Apache、MariaDB 和 PHP (FAMP) 堆栈,现在我们已准备好托管网站或者任何基于 Web 的应用程序。
第 3 步 - 安装 MariaDB
MariaDB 是 MySQL 的替代品。
它具有相同的进程名称、相同的语法和配置。
要安装运行以下 pkg 命令:
# pkg install mariadb100-server
将 MariaDB 示例配置从目录“/usr/local/share/mysql/”复制到“/usr/local/etc/”:
# cp /usr/local/share/mysql/my-medium.cnf /usr/local/etc/my.cnf
启用并启动 MariaDB 服务:
# sysrc mysql_enable=yes # service mysql-server start
默认情况下,MariaDB 安装 root 密码为空。
出于安全原因,我们将使用以下命令创建 root 密码:
# mysql_secure_installation
当提示“输入当前root密码”时,只需按ENTER键并设置密码两次。
然后只需按 Y 即可接受默认值。
示例输出:
# 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] ... 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] ... 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] - 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] ... 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!
步骤 1 - 更新 FreeBSD
在我们继续安装任何软件之前,我们需要先更新我们的 FreeBSD 11 操作系统。
在 FreeBSD 服务器上运行以下命令。
$ su # freebsd-update fetch # freebsd-update install
如果你已经安装了lates update 你应该得到这样的输出
# freebsd-update fetch src component not installed, skipped Looking up update.FreeBSD.org mirrors... 4 mirrors found. Fetching public key from update5.freebsd.org... done. Fetching metadata signature for 11.0-RELEASE from update5.freebsd.org... done. Fetching metadata index... done. Fetching 1 metadata files... done. Inspecting system... done. Preparing to download files... done. No updates needed to update system to 11.0-RELEASE-p0.
# freebsd-update install src component not installed, skipped No updates are available to install. Run '/usr/sbin/freebsd-update fetch' first.
在本教程中,我们将介绍如何在 FreeBSD 11 上安装 Apache、MariaDB 和 PHP。
如果我们不知道 FAMP 是 FreeBSD、Apache、MySQL/MariaDB、PHP 的首字母缩写词。
FreeBSD 11 是撰写本文时的最新版本。
第 5 步 - 测试 PHP
为了测试 PHP,我们将使用 nano 创建示例 php 脚本:
# nano /usr/local/www/apache24/data/test.php
添加以下行:
<?php phpinfo(); ?>
保存并关闭文件。
打开 Web 浏览器并导航到服务器地址 http://IP-Address/test.php。
第 4 步 - 安装 PHP
我们需要 php 来服务器动态内容,要安装 PHP,请运行以下命令:
# pkg install mod_php56 php56-mysql php56-mysqli
安装成功后,我们需要复制php.ini示例配置:
# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
使用此命令更新更改:
# rehash
我们需要使用 apache 网络服务器配置 PHP,所以我们需要使用 nano 编辑 apache 配置文件:
# nano /usr/local/etc/apache24/httpd.conf
找到 DirectoryIndex 部分并在现有的 index.html 前面添加 index.php,如下所示。
[...] <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> [...]
然后,在Apache配置文件的底部添加以下几行
<FilesMatch "\.php$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch "\.phps$"> SetHandler application/x-httpd-php-source </FilesMatch>
保存并关闭文件。
我们需要重新启动 Apache Web 服务器以更改配置:
# service apache24 restart