下面以 Ubuntu + Nginx/Apache + PHP + ThinkPHP 为例,给你一份从零开始的可操作配置指南(以 ThinkPHP 6/8 为例,5.1 也类似)。
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 -y
查看版本:
php -v
sudo apt install nginx -y
sudo apt install composer -y
cd /var/www
composer create-project topthink/think tpapp
目录结构:
/var/www/tpapp
sudo chown -R www-data:www-data /var/www/tpapp
sudo chmod -R 755 /var/www/tpapp
sudo nano /etc/nginx/sites-available/tpapp
server {
listen 80;
server_name yourdomain.com; # 或 localhost
root /var/www/tpapp/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/tpapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo apt install apache2 libapache2-mod-php -y
sudo a2enmod rewrite
<VirtualHost *:80>
DocumentRoot /var/www/tpapp/public
<Directory /var/www/tpapp/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo systemctl restart apache2
/var/www/tpapp/config/
// config/database.php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'tpdb',
'username' => 'tpuser',
'password' => 'password',
'charset' => 'utf8mb4',
]
]
];
tail -f /var/www/tpapp/runtime/log/*
public/.htaccess(Apache)try_filessudo chmod -R 777 runtime
浏览器访问:
http://服务器IP
看到 ThinkPHP 欢迎页即成功 ✅
如果你告诉我:
我可以给你完全对应的配置文件。