在 Ubuntu 上安装 ThinkPHP,一般分两种情况:
1️⃣ 全新项目(用 Composer 安装)
2️⃣ 已有项目(直接拉代码运行)
下面以 ThinkPHP 8(最新稳定版) 为例说明。
sudo apt update
sudo apt upgrade -y
sudo apt install php php-cli php-fpm php-mysql php-mbstring php-xml php-curl php-zip unzip -y
检查 PHP 版本:
php -v
(建议 PHP ≥ 8.0)
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
验证:
composer -V
composer create-project topthink/think tp8
这会在当前目录生成 tp8 文件夹。
进入项目:
cd tp8
启动内置服务器测试:
php think run
浏览器访问:
http://localhost:8000
✅ 看到 ThinkPHP 欢迎页面即成功。
git clone https://github.com/top-think/think.git
cd think
composer install
安装 Nginx:
sudo apt install nginx -y
新建站点配置:
sudo nano /etc/nginx/sites-available/tp8
示例配置:
server {
listen 80;
server_name example.com;
root /var/www/tp8/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
}
启用站点:
sudo ln -s /etc/nginx/sites-available/tp8 /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo apt install apache2 libapache2-mod-php -y
开启重写:
sudo a2enmod rewrite
项目根目录指向 public
tp8/
├── app/ # 应用代码
├── public/ # 网站根目录(必须对外可访问)
├── config/ # 配置
├── route/ # 路由
├── think # 命令行入口
chmod -R 755 runtime
chown -R www-data:www-data runtime
public/index.phpphp think version
如果你告诉我:
我可以给你 更精确的安装命令和配置。