温馨提示×

ubuntu怎么配置thinkphp

小樊
46
2026-07-29 09:44:18
栏目: 编程语言

在 Ubuntu 上配置 ThinkPHP 一般分为几个步骤:安装 PHP 环境 → 安装 Web 服务器(Nginx/Apache)→ 安装 Composer → 部署 ThinkPHP → 配置站点。下面我以 Ubuntu 20.04/22.04 + Nginx + PHP 8.x + ThinkPHP 6 为例,给你一个完整流程。


一、安装 PHP 及必要扩展

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装 PHP 和扩展

sudo apt install -y php php-cli php-fpm php-common \
php-curl php-mysql php-mbstring php-xml php-zip php-gd

查看 PHP 版本:

php -v

二、安装 Nginx(推荐)

sudo apt install -y nginx

启动并设置开机自启:

sudo systemctl start nginx
sudo systemctl enable nginx

浏览器访问服务器 IP,看到 Welcome to nginx 说明成功。


三、安装 Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

验证:

composer -V

四、安装 ThinkPHP

方式一:Composer 安装(推荐)

cd /var/www
composer create-project topthink/think tp6

目录结构:

/var/www/tp6

方式二:手动下载

也可以从 ThinkPHP 官网下载压缩包解压。


五、配置 Nginx 站点

1. 修改 Nginx 配置

新建配置文件:

sudo nano /etc/nginx/sites-available/tp6

内容示例(PHP 8.x):

server {
    listen 80;
    server_name your_domain_or_ip;

    root /var/www/tp6/public;
    index index.php index.html index.htm;

    location / {
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

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

2. 启用配置

sudo ln -s /etc/nginx/sites-available/tp6 /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

六、目录权限设置(非常重要)

sudo chown -R www-data:www-data /var/www/tp6
sudo chmod -R 755 /var/www/tp6

七、测试 ThinkPHP

浏览器访问:

http://服务器IP

看到 ThinkPHP 欢迎页面 ✅


八、常见问题排查

1. 页面空白 / 500

tail -f /var/www/tp6/runtime/log/*

2. PHP 不解析

检查 PHP-FPM 是否运行:

systemctl status php8.1-fpm

3. Nginx 404

确认:

  • root 指向 public
  • rewrite 规则正确

九、数据库配置(可选)

编辑:

/var/www/tp6/config/database.php

或者 .env

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=123456

十、总结

✅ Ubuntu
✅ Nginx + PHP-FPM
✅ Composer
✅ ThinkPHP 6
✅ 权限 + 伪静态

如果你用的是 Apache / PHP 7.x / ThinkPHP 5,或者需要 HTTPS / 多站点 / Docker,告诉我,我可以给你对应配置。

0