下面给你一套在 Ubuntu 上部署 ThinkPHP 项目的常见、稳妥做法(以 ThinkPHP 6 / 8 + Nginx + PHP-FPM 为例,生产环境最常用)。
sudo apt update
sudo apt upgrade -y
sudo apt install nginx -y
sudo apt install php8.1-fpm php8.1-cli php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl php8.1-zip -y
查看 PHP 是否安装成功:
php -v
假设项目放在:
/var/www/thinkphp
可用方式:
cd /var/www/thinkphp
composer install --no-dev
如果没有 composer:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chown -R www-data:www-data /var/www/thinkphp
sudo chmod -R 755 /var/www/thinkphp
sudo chmod -R 777 /var/www/thinkphp/runtime
新建配置文件:
sudo nano /etc/nginx/sites-available/thinkphp
示例配置(重点:rewrite 到 index.php):
server {
listen 80;
server_name yourdomain.com;
root /var/www/thinkphp/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
启用站点:
sudo ln -s /etc/nginx/sites-available/thinkphp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
编辑:
/var/www/thinkphp/.env
示例:
APP_DEBUG = false
DB_HOST = 127.0.0.1
DB_NAME = tp_db
DB_USER = tp_user
DB_PASS = password
sudo apt install mysql-server -y
sudo mysql_secure_installation
创建数据库:
CREATE DATABASE tp_db;
CREATE USER 'tp_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON tp_db.* TO 'tp_user'@'localhost';
FLUSH PRIVILEGES;
✅ 确认:
publictry_files 配置正确tail -f /var/www/thinkphp/runtime/log/*
sudo chmod -R 777 runtime
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
✅ 推荐生产环境组合:
如果你告诉我:
我可以给你更精确的部署方案。