商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > ubuntu下如何实现thinkphp的分布式部署

ubuntu下如何实现thinkphp的分布式部署

  发布于2026-07-07 阅读(0)

扫一扫,手机访问

在 Ubuntu 下搭建 ThinkPHP 的分布式环境,选 Nginx 配合 PHP-FPM 的组合可以说是最经典的方案之一。整个过程清晰直接,只要按部就班走下来,基本不会遇到坑。下面直接上手。

ubuntu下如何实现thinkphp的分布式部署

1. 安装 Nginx 和 PHP-FPM

先把包列表更新一下,然后装上 Nginx 和 PHP-FPM,几条命令就搞定:

sudo apt update
sudo apt install nginx php-fpm

2. 配置 Nginx

配置文件可以直接修改默认的 /etc/nginx/sites-a vailable/default,也可以新建一个单独的站点配置。这里以新建为例:

sudo nano /etc/nginx/sites-a vailable/thinkphp

写入以下内容(记得把域名和项目路径换成你自己的):

server {
    listen 80;
    server_name your_domain.com;  # 替换为你的域名或 IP 地址
    root /path/to/your/thinkphp/project;  # 替换为你的 ThinkPHP 项目路径

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;  # 根据你的 PHP 版本调整
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

保存退出后,创建一个软链接启用它:

sudo ln -s /etc/nginx/sites-a vailable/thinkphp /etc/nginx/sites-enabled/

检查一下配置有没有语法错误:

sudo nginx -t

没问题就重新加载 Nginx:

sudo systemctl reload nginx

3. 配置 PHP-FPM

PHP-FPM 的配置文件在 /etc/php/7.4/fpm/pool.d/www.conf(版本号按你实际安装的来):

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

找到 listen 这一行,确认它的值和你 Nginx 配置里的一致:

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

保存后重启 PHP-FPM:

sudo systemctl restart php7.4-fpm

4. 部署 ThinkPHP 项目

把项目文件上传到前面指定的目录(比如 /path/to/your/thinkphp/project)。然后调整一下权限,确保 Web 服务器能正常读写:

sudo chown -R www-data:www-data /path/to/your/thinkphp/project
sudo chmod -R 755 /path/to/your/thinkphp/project

5. 启动并设置开机自启

让 Nginx 和 PHP-FPM 跑起来,并设置为开机自动启动:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm

6. 配置负载均衡(可选)

如果目标是分布式部署,Nginx 自带的负载均衡功能就能搞定。编辑主配置文件 /etc/nginx/nginx.conf,在 http 块里定义一个 upstream:

http {
    upstream thinkphp_cluster {
        server unix:/var/run/php/php7.4-fpm.sock;
        server unix:/var/run/php/php7.4-fpm2.sock;
        # 可以继续添加更多 PHP-FPM 实例
    }

    server {
        listen 80;
        server_name your_domain.com;
        root /path/to/your/thinkphp/project;
        index index.php index.html index.htm;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass thinkphp_cluster;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location ~ /\.ht {
            deny all;
        }
    }
}

保存后重新加载 Nginx:

sudo systemctl reload nginx

别忘了把每个 PHP-FPM 实例都启动起来,比如第二个实例:

sudo systemctl start php7.4-fpm2

到这一步,ThinkPHP 的分布式部署基本就成型了。实际生产环境可能还需要根据业务量调整 PHP-FPM 的进程数、开启缓存、配置 HTTPS 等,但核心框架就是上面这些。操作起来并不复杂,关键是理解 Nginx 和 PHP-FPM 之间的通信方式,以及负载均衡的 upstream 用法。

本文转载于:https://www.yisu.com/ask/84490693.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注