在FreeBSD 10.2中安装Nginx,MariaDB和PHP(FEMP)堆栈

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

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

正如我们可能所知,FEMP是FreeBSD,Nginx(Engine X),Mariadb/MySQL和PHP的首字母缩略词。

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

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

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

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  theitroad.local www.theitroad.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指令(onitad.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.

打开Web浏览器并导航到: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配置文件/ususr/local/etc/php.ini-production复制到/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

现在,打开Web浏览器并导航到: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 : Hyman@theitroad
WWW : http://www.php.net/
Comment : The curl shared extension for php
[...]

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

# pkg install php56-curl