游乐游手机版
首页/系统平台/文章详情

FreeBSD 7.0编译安装Nginx+PHP+MySQL教程

时间:2026-06-19 07:46
本文以 FreeBSD 7 0 为基础环境,系统自带的工具链虽然版本较旧,但胜在稳定可靠。所有软件统一安装至 usr local vhost 目录下,如需更换路径,请同步修改后续所有相关目录。 一、下载源代码 首先通过 Ports 树拉取源码,这样能够自动处理版本依赖关系。依次执行以下命令: cd

本文以 FreeBSD 7.0 为基础环境,系统自带的工具链虽然版本较旧,但胜在稳定可靠。所有软件统一安装至 /usr/local/vhost 目录下,如需更换路径,请同步修改后续所有相关目录。

一、下载源代码

首先通过 Ports 树拉取源码,这样能够自动处理版本依赖关系。依次执行以下命令:

cd /usr/ports/databases/mysql50-server/
make fetch

cd /usr/ports/www/nginx-devel/
make fetch

cd /usr/ports/lang/php5
make fetch

cd /usr/ports/www/lighttpd
make fetch

二、编译安装

MySQL

cd /usr/ports/distfiles/
tar -jxf mysql-5.0.51a.tar.gz 
cd mysql-5.0.51a
./configure --prefix=/usr/local/vhost/mysql --with-charset=utf8 --with-extra-charsets=all --with-big-tables --with-pthread
make && make install

Nginx

Nginx 依赖 PCRE 正则库,请先安装它:

cd /usr/ports/devel/pcre
make install clean

cd /usr/ports/distfiles/
tar -jxf nginx-0.6.30.tar.gz 
cd nginx-0.6.30
./configure --prefix=/usr/local/vhost/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_sub_module
make && make install

Lighttpd

Lighttpd 仅用于获取 spawn-fcgi 工具,以便启动 PHP 的 FastCGI 进程。配置完成后可将其删除以节省磁盘空间。

cd /usr/ports/distfiles/
tar -jxf lighttpd-1.4.19.tar.bz2 
cd lighttpd-1.4.19
./configure --prefix=/usr/local/vhost/lighttpd
make && make install

PHP

编译 PHP 之前需要先安装几个依赖库:libxml2、curl、jpeg、png、gettext 等。请逐一执行安装:

cd /usr/ports/textproc/libxml2
make install clean

cd /usr/ports/ftp/curl
make install clean

cd /usr/ports/graphics/jpeg
make install clean

cd /usr/ports/graphics/png
make install clean

cd /usr/ports/devel/gettext
make install clean

接着开始编译 PHP 本身,务必要开启 FastCGI 支持:

cd /usr/ports/distfiles/
tar -jxf php-5.2.6.tar.bz2 
cd php-5.2.6
./configure --prefix=/usr/local/vhost/php --with-mysql=/usr/local/vhost/mysql -enable-fastcgi --enable-sockets --enable-ftp --enable-zip --enable-mbstring --enable-mbregex --enable-calendar --with-curl=/usr/local/include --with-curlwrappers --disable-debug --enable-inline-optimization --with-zlib --with-gd --with-kerberos --with-gettext --enable-force-cgi-redirect --with-jpeg-dir=/usr/local/include --with-png-dir=/usr/local/include --with-bz2 --enable-pcntl --with-iconv
make && make install
cp php.ini-dist /usr/local/vhost/php/lib/php.ini

三、配置

MySQL

首先创建 MySQL 专用用户,初始化数据库,并设置启动脚本:

cd /usr/local/vhost/mysql
pw adduser mysql -d /dev/null -s /sbin/nologin
bin/mysql_install_db
cp share/mysql/mysql.server ./
chmod +x mysql.server
chown -R mysql ./

然后启动 MySQL 服务:

/usr/local/vhost/mysql/mysql.server start

配置 Nginx

先创建 web 用户,并将 Lighttpd 编译生成的 spawn-fcgi 复制至 Nginx 目录,之后即可彻底删除 Lighttpd:

cd /usr/local/vhost/nginx/
pw adduser webuser -d /dev/null -s /sbin/nologin
cp /usr/local/vhost/lighttpd/bin/spawn-fcgi ./sbin/
rm -rf /usr/local/vhost/lighttpd

编写一个用于启动 PHP FastCGI 的脚本 sbin/php.sh

#!/bin/sh
/usr/local/vhost/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 8 -u webuser -f /usr/local/vhost/php/bin/php-cgi

赋予脚本执行权限:

chmod +x sbin/php.sh

启动 PHP 解析器:

sbin/php.sh

接下来配置 Nginx 以支持 PHP 解析。新建一个 conf/enable_php 文件:

location ~ \.php$ {
    root            html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/vhost/nginx/html$fastcgi_script_name;
    fastcgi_param  QUERY_STRING        $query_string;
    fastcgi_param  REQUEST_METHOD      $request_method;
    fastcgi_param  CONTENT_TYPE        $content_type;
    fastcgi_param  CONTENT_LENGTH      $content_length;
    fastcgi_param  SCRIPT_NAME         $fastcgi_script_name;
    fastcgi_param  REQUEST_URI         $request_uri;
    fastcgi_param  DOCUMENT_URI        $document_uri;
    fastcgi_param  DOCUMENT_ROOT       $document_root;
    fastcgi_param  SERVER_PROTOCOL     $server_protocol;
    fastcgi_param  GATEWAY_INTERFACE   CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE     nginx/$nginx_version;
    fastcgi_param  REMOTE_ADDR         $remote_addr;
    fastcgi_param  REMOTE_PORT         $remote_port;
    fastcgi_param  SERVER_ADDR         $server_addr;
    fastcgi_param  SERVER_PORT         $server_port;
    fastcgi_param  SERVER_NAME         $server_name;
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS     200;
}

编辑 conf/nginx.conf,写入基础配置。注意这里使用 FreeBSD 特有的 kqueue 事件模型:

user  webuser webuser;
worker_processes  1;

events {
    worker_connections  4096;
    use kqueue;
}

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

    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '
    #                  '"$status" $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;

    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;
    client_max_body_size   5m;
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    gzip  on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;

    output_buffers   1 32k;
    postpone_output  1460;
    sendfile         on;
    tcp_nopush       on;
    tcp_nodelay      on;
    send_lowat       12000;
    keepalive_timeout  75 20;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        if (-d $request_filename){
            rewrite ^/(.*)([^/])$ https://$host/$1$2/ permanent;
        }

        location /nginx_status {
            stub_status on;
            access_log   off;
        }

        include enable_php;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

测试 Nginx 配置是否正确:

sbin/nginx -t
2008/05/08 11:50:19 [info] 3336#0: the configuration file /usr/local/vhost/nginx/conf/nginx.conf syntax is ok
2008/05/08 11:50:19 [info] 3336#0: the configuration file /usr/local/vhost/nginx/conf/nginx.conf was tested successfully

编写一个简单的 PHP 探针用于验证:

vi html/phpinfo.php

启动 Nginx:

/usr/local/vhost/nginx/sbin/nginx

在浏览器中访问 https://192.168.29.128/phpinfo.php,如果一切顺利,即可看到 PHP 信息页面。

安装配置 phpMyAdmin

为了方便管理 MySQL 数据库,再安装一次 phpMyAdmin:

cd /usr/ports/databases/phpmyadmin/
make fetch

cd /usr/ports/distfiles
tar -jxf phpMyAdmin-2.11.6-all-languages.tar.bz2 
mv phpMyAdmin-2.11.6-all-languages /usr/local/vhost/nginx/html/dbadmin

此时 MySQL 的 root 用户尚未设置密码,可直接通过 https://192.168.29.128/dbadmin/index.php 登录管理。强烈建议立即为 root 设置一个强密码,避免暴露在公网时产生安全风险。

来源:https://www.jb51.net/os/Unix/1465.html
上一篇FreeBSD实时网络流量查看方法 下一篇FreeBSD系统修改IP地址与绑定多个IP的方法
本站内容用于信息整理与展示,如有侵权或内容问题请及时联系处理。

相关推荐

补充同频道和同主题内容,方便继续浏览更多相关内容。

同类最新

继续查看同栏目最近更新的文章。

更多
微软详解Win11时间点还原 默认每24小时创建恢复点
系统平台 · 2026-06-30

微软详解Win11时间点还原 默认每24小时创建恢复点

微软今日推送了最新的 6 月可选更新,并发布博客详细解读了 Win11 全新的“时间点还原”(Point-in-time restore)功能——这一功能本质上是对系统恢复体验的一次全面升级,旨在让用户更轻松地应对电脑故障。 微软表示,面向 Windows 11 客户端用户的“时间点还原”功能现已正

Win11 26H1六月可选更新KB5095091 优化放大镜改善装机体验
系统平台 · 2026-06-30

Win11 26H1六月可选更新KB5095091 优化放大镜改善装机体验

微软今天推送了Windows 11 26H1设备的6月可选更新KB5095091,安装完成后系统版本号会升级到Build 28000 2340。值得一提的是,这次更新并非面向所有设备,而是专门为搭载高通骁龙X2系列芯片的机型准备的——包括骁龙X2 Plus、X2 Elite和X2 Elite Ext

Win11六月可选更新KB5095093修复回收站弹窗异常
系统平台 · 2026-06-30

Win11六月可选更新KB5095093修复回收站弹窗异常

微软已悄然推送Windows 11六月可选更新,编号KB5095093。本次更新覆盖两个版本:24H2用户安装后版本号升级至Build 26100 8737,而25H2用户则更新至Build 26200 8737。 本次更新并非仅是小修小补,而是带来了多项实质性新功能。下面我们就来详细解析这些更新内

苹果macOS 27 Beta2封堵Siri AI跳过候补名单漏洞
系统平台 · 2026-06-30

苹果macOS 27 Beta2封堵Siri AI跳过候补名单漏洞

科技媒体 Cult of Mac 昨日(6月23日)发布博文指出,苹果在 macOS 27 Beta 2 更新中悄然封堵了一个此前可用的后门——用户曾能通过一条终端命令绕过候补名单,直接启用新版 Siri AI,如今这一方法已失效。 简要回顾一下:在 macOS 27 Beta 1 阶段,只需在 M

微软加速Win11 25H2推送 覆盖所有符合条件家用PC
系统平台 · 2026-06-30

微软加速Win11 25H2推送 覆盖所有符合条件家用PC

近日(6月23日),科技媒体 Windows Latest 发布了一则值得关注的动态:微软已进一步扩大 Windows 11 25H2 的推送范围,所有满足硬件要求、且不受 IT 部门管理的家庭版和专业版设备,现在均可顺利接收本次更新。 此次升级有一个显著特点——采用“启用包”(eKB)方式进行推送