在FreeBSD 10.2上安装Apache,MariaDB和PHP(FAMP)堆栈

时间:2020-03-21 11:45:15  来源:igfitidea点击:

本教程描述了如何在FreeBSD 10.2中安装Apache,MariaDB和PHP(FAMP)堆栈。
我们可能知道,FAMP是FreeBSD,Apache,MySQL/MariaDB,PHP的缩写。

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

  • 操作系统:FreeBSD 10.2 64位系统
  • 主机名:freebsd.theitroad.local
  • IP地址:192.168.1.103/24

好了,现在让我们开始在FreeBSD 10.2上部署FAMP堆栈。

1.更新FreeBSD

像任何其他操作系统一样,我们必须在安装任何软件之前更新FreeBSD。
为此,请切换到root用户:

$su

并一一运行以下命令以更新FreeBSD服务器:

# freebsd-update fetch
# freebsd-update install

2.安装Apache

使用以下命令安装Apache Web服务器:

# pkg install apache24

接下来,我们需要启用并启动Apache服务。
为此,请运行:

# sysrc apache24_enable=yes
# service apache24 start

测试Apache Web服务

我们已经安装了Apache Web服务器。
现在,我们将测试它是否正常工作。

为此,请编辑Apache配置文件/usr/local/etc/apache24/httpd.conf。

# nano /usr/local/etc/apache24/httpd.conf

找到以下几行并进行更改,如下所示。
用我们自己的值替换它们。

[...]
ServerAdmin Hyman@theitroad
.
.
.
ServerName localhost:80
[...]

然后,在Apache根目录中创建一个示例HTML文件,如下所示。

# nano /usr/local/www/apache24/data/index.html

添加以下内容:

<html>
<title>CONGRATULATIONS</title>
<body>
<h2>Welcome theitroad web server</h2>
</body>
</html>

重新启动Apache Web服务。

# service apache24 restart

输出示例:

Performing sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 3265.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.

现在,打开Web浏览器并导航至:http://IP-address /或者http://localhost /。
我们将看到以下Apache测试页。

3.安装MariaDB

MariaDB是MySQL的直接替代品。
要安装MariaDB,请运行:

# 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用户密码

我们可能知道,MariaDB根用户在安装时为空,不建议这样做。
因此,为了保护MariaDB根用户,必须为根用户设置一个强密码。
为此,请运行:

# mysql_secure_installation

当提示“输入用于root的当前密码”时,只需按ENTER键并设置两次密码即可。
然后只需按Y接受默认值。

输出示例:

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): ## Press Enter
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] ## Press Enter
New password: ## Enter password
Re-enter new password: ## Re-enter 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] ## Press Enter
 ... 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] ## Press Enter
 ... 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] ## Press Enter
 - 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] ## Press Enter
 ... 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!

MariaDB现在已安装并保护。
让我们继续安装PHP。

4.安装PHP

要安装PHP,请运行:

# pkg install mod_php56 php56-mysql php56-mysqli

安装PHP之后,将示例PHP配置文件/usr/local/etc/php.ini-production复制到/usr/local/etc /目录,如下所示。

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

要更新更改,请运行:

# rehash

现在,我们需要使用Apache Web服务器配置PHP。
为此,请编辑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>

保存并关闭文件。

测试PHP

我们已经安装了PHP并将其配置为可与Apache Web服务器一起使用。
现在,让我们检查PHP是否有效。
为此,请在Apache根目录中创建一个test.php文件:

# nano /usr/local/www/apache24/data/test.php

添加以下行:

<?php phpinfo(); ?>

保存并关闭文件。

重新启动Apache Web服务器以使更改生效。

# service apache24 restart

打开Web浏览器,然后导航到http://IP-Address/test.php。
我们会受到PHP测试配置页面的欢迎。

安装PHP模块

我们需要安装PHP模块(扩展)以增强PHP的功能。
这是可选的,如果不需要安装任何扩展,可以跳过它。

要查看可用模块的列表,只需运行:

# pkg search php56

我们可以从上面的输出中的注释部分验证每个模块的功能,或者只运行以下命令

# pkg search -f php56-curl

输出示例:

php56-curl-5.6.18
Name : php56-curl
Version : 5.6.18
Origin : ftp/php56-curl
Architecture : freebsd:10:x86:64
Prefix : /usr/local
Repository : FreeBSD [pkg+http://pkg.FreeBSD.org/FreeBSD:10:amd64/quarterly]
Categories : ftp
Licenses : PHP301
Maintainer : Hyman@theitroad
WWW : http://www.php.net/
Comment : The curl shared extension for php
[...]

要安装PHP扩展,例如php56-curl-5.6.18,请运行:

# pkg install php56-curl

恭喜你!在此阶段,FAMP堆栈已准备就绪,可以托管或者任何基于Web的应用程序。