下面以 Linux 服务器(常见 CentOS / Ubuntu) 为例,说明 ThinkPHP 的配置方法,从环境到项目配置一步步来。
ThinkPHP(以 TP6 为例)需要:
sudo apt update
sudo apt install php php-cli php-fpm php-mysql php-mbstring php-xml php-curl php-zip
查看版本:
php -v
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer -v
composer create-project topthink/think tp6
cd tp6
目录结构(重点):
tp6
├── app
├── config
├── public
├── runtime
└── .env
编辑:
vim config/database.php
示例(MySQL):
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'test',
'username' => 'root',
'password' => '123456',
'hostport' => '3306',
'charset' => 'utf8mb4',
],
],
];
✅ 推荐用 .env 配置(更安全)
cp .example.env .env
vim .env
APP_DEBUG = true
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = root
PASSWORD = 123456
HOSTPORT = 3306
config/app.php
return [
'app_name' => 'MyApp',
'app_debug' => true,
];
route/app.php
use think\facade\Route;
Route::get('hello/:name', 'Index/hello');
server {
listen 80;
server_name www.example.com;
root /var/www/tp6/public;
index index.php index.html;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
重启 Nginx:
nginx -t
systemctl restart nginx
开启 rewrite:
a2enmod rewrite
public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
chmod -R 755 tp6
chown -R www-data:www-data tp6/runtime
浏览器访问:
http://服务器IP/
看到 ThinkPHP 欢迎页面 ✅
runtime 权限runtime/logapp_debug=false 时路由必须正确composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
如果你用的是 CentOS / 宝塔 / Docker / TP5,或者想配置 HTTPS / 多应用 / 队列,可以告诉我具体环境,我可以给你更精确的配置。