导读 本文介绍如何在FreeBSD 13系统中安装Nginx、MySQL、和PHP服务。
系统环境

FreeBSD 13.0-RELEASE

更新系统

在安装任何软件之前更新系统是一个好习惯,以便检查系统更新:

root@freebsd:~ # freebsd-update fetch
root@freebsd:~ # freebsd-update install
安装Nginx

使用pkg包管理器安装nginx:

root@freebsd:~ # pkg install -y nginx

启动nginx

要在系统启动时运行 Nginx服务,需要在/etc/rc.conf配置文件的末尾添加一行nginx_enable="yes"。运行下面的,自动将nginx_enable="yes"附加到rc.conf文件中,然后启动服务:

root@freebsd:~ # sysrc nginx_enable=yes
root@freebsd:~ # service nginx start
Performing sanity check on nginx configuration:
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
Starting nginx.


nginx相关配置

nginx的配置文件在/usr/local/etc/nginx目录中,名称为nginx.conf,可以使用vim编辑器打开配置文件。

root@freebsd:~ # vim /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  localhost;
        location / {
            root   /usr/local/www/nginx;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
        location ~ \.php$ {
            root           /usr/local/www/nginx;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

安装MySQL

使用下面来安装mysql:

root@freebsd:~ # pkg install mysql80-server mysql80-client

要在系统启动时启用它,使用下面命令将mysql_enable=yes添加到rc.conf文件中:

root@freebsd:~ # sysrc mysql_enable=yes


启动mysql:

root@freebsd:~ # service mysql-server start
Starting mysql.
安装PHP

PHP 是一种服务器端语言,与 HTML 结合使用以创建动态 Web 内容。它还连接到 MySQL 数据库以检索和上传内容。下面命令来安装php8.0和常用的模块。

root@freebsd:~ # pkg install -y php80 php80-mysqli php80-mbstring php80-zlib php80-curl php80-gd php80-json
配置php-fpm

下面将php.ini-production配置文件复制一份,改名为php.ini:

root@freebsd:/usr/local/etc # cp /usr/local/etc/php.ini{-production,}

打开文件/usr/local/etc/php.ini并取消注释;cgi.fix_pathinfo=1,将其值更改为0

root@freebsd:~ # cat /usr/local/etc/php.ini | grep '^cgi.fix_pathinfo'
cgi.fix_pathinfo=0

设置php-fpm开机启动:

root@freebsd:~ # sysrc php_fpm_enable=yes
php_fpm_enable:  -> yes
root@freebsd:~ # service php-fpm start
Performing sanity check on php-fpm configuration:
[03-Jun-2021 18:51:27] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.
root@freebsd:~ # 

访问php测试页

/usr/local/www/nginx目录下创建一个php测试文件,名为:test.php:

root@freebsd:~ # vim /usr/local/www/nginx/test.php 


浏览器访问一下查看是否可看到测试页:

测试没问题了,可以从服务器中删除测试文件,以避免将有关服务器的信息暴露。

root@freebsd:~ # rm -rf /usr/local/www/nginx/test.php 
总结

目前位置已经在 FreeBSD 系统上安装了 Nginx、MySQL 和 PHP!

本文原创地址://gulass.cn/freebsd-install-femp.html编辑:逄增宝,审核员:逄增宝