网站搜索

在 FreeBSD 10.2 中安装 Nginx、MariaDB 和 PHP


在之前的教程中,我们描述了如何在 FreeBSD 中设置 FAMP 堆栈。

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

现在,我们将向您展示如何在 FreeBSD 10.2 服务器中安装 Nginx、MariaDB 和 PHP (FEMP) 堆栈。

您可能知道,FEMP 是 FreeBSD、Nginx (engine x)、MariaDB/M

出于本教程的目的,我将使用以下测试机器。

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

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

1. 更新你的 FreeBSD

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

su

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

freebsd-update fetch install

2.安装Nginx

使用命令安装 Nginx Web 服务器:

# pkg install nginx

配置 Nginx

Nginx默认配置文件是/usr/local/etc/nginx/nginx.conf。

我们先备份配置文件,万一出现问题,我们可以轻松地从备份中恢复。

为此,请运行:

mv /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/nginx.conf.bak

接下来,创建一个新的 Nginx 配置文件:

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

添加以下行:

user www;
worker_processes  1;
error_log /var/log/nginx/error.log info;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log /var/log/nginx/access.log;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  ostechnix.local www.ostechnix.local;
        root /usr/local/www/nginx;
        index index.php index.html index.htm;

        location / {
            try_files $uri $uri/ =404;
        }

        error_page      500 502 503 504  /50x.html;
        location = /50x.html {
            root /usr/local/www/nginx-dist;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                include fastcgi_params;
        }
    }
}

将 server_name 指令 (ostechnix.local) 替换为您自己的指令。保存并关闭文件。

接下来,创建必要的日志文件。这些日志文件对于将来的任何故障排除和错误分析非常有用。

mkdir -p /var/log/nginx
touch /var/log/nginx/access.log
touch /var/log/nginx/error.log

接下来,我们需要配置正确的Web根目录。默认情况下,Web 根目录为 /usr/local/www/nginx,但它只是到“nginx-dist ” 目录的符号链接。

我们应该删除该链接并将其指向正确的位置。

因此,让我们使用命令删除 Web 根目录:

rm -rf /usr/local/www/nginx

然后使用命令重新创建 Web 根目录:

mkdir /usr/local/www/nginx

接下来,我们应该将 index.html 文件复制到 Web 根目录,因为在前面的步骤中删除 Web 根目录时该文件已被删除。

cp /usr/local/www/nginx-dist/index.html /usr/local/www/nginx

使用命令测试nginx配置文件是否正确:

nginx -t

示例输出:

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

最后,我们需要启用并启动 Nginx 服务。为此,请运行:

sysrc nginx_enable=yes
service nginx start

测试 Nginx

打开网络浏览器并导航至:http://IP-address/ 或 http://localhost。您将看到以下 Nginx 测试页面。

恭喜! Nginx Web 服务器安装成功并且运行良好。

接下来,我们需要安装 MariaDB。

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 root 用户在安装时是空的,不建议这样做。因此,为了保护 MariaDB root 用户,必须为 root 用户设置强密码。为此,请运行:

# 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 php56-mysql php56-mysqli

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

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

如果您进行任何更改,则必须使用命令更新更改:

rehash

现在,我们需要使用 Nginx Web 服务器配置 PHP。

使用 Nginx Web 服务器配置 PHP

编辑 /usr/local/etc/php-fpm.conf 文件:

nano /usr/local/etc/php-fpm.conf

找到以下行:

listen = 127.0.0.1:9000

并将其替换为:

listen = /var/run/php-fpm.sock

然后,找到以下行并取消注释。

listen.owner = www
listen.group = www
listen.mode = 0660

保存并关闭文件。

测试 PHP

在 Web 根目录中创建示例 PHP 文件:

nano /usr/local/www/nginx/test.php

添加以下行:

<?php phpinfo(); ?>

保存并关闭文件。

然后,使用以下命令启用并启动 PHP-FPM 服务:

sysrc php_fpm_enable=yes
service php-fpm start

最后,使用命令重新启动 nginx 服务:

service nginx restart

现在,打开您的网络浏览器并导航至:http://IP-Address/info.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 : ale@FreeBSD.org
WWW : http://www.php.net/
Comment : The curl shared extension for php
[...]

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

pkg install php56-curl

恭喜!在此阶段,FEMP 堆栈已准备好托管您的网站或任何基于 Web 的应用程序。

目前为止就这样了。感谢您阅读本教程。如果您有任何疑问,请随时在下面的评论部分提问。我们会尽快解决。

如果您发现本教程有用,请在您的社交网络上分享并支持 OSTechNix。

干杯!

相关文章