ThinkPHP运行需PHP+Web服务器+Composer组合,以下是Linux(以Ubuntu/Debian为例)的安装命令:
sudo apt update && sudo apt upgrade -ysudo apt install php php-fpm php-mysql php-mbstring php-xml php-curl -ysudo apt install nginx -ycurl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
验证安装:composer --version(显示版本号即成功)。ThinkPHP要求Web根目录指向项目public文件夹,并配置URL重写规则以支持PATHINFO。
sudo nano /etc/nginx/sites-available/thinkphpyourdomain.com为域名/IP,/var/www/html/your_project为项目路径):server {
listen 80;
server_name yourdomain.com;
root /var/www/html/your_project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string; # 关键:将请求转发给index.php
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据PHP版本调整(如php7.4-fpm.sock)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all; # 禁止访问.htaccess文件
}
}
sudo ln -s /etc/nginx/sites-available/thinkphp /etc/nginx/sites-enabled/sudo nginx -t(检查语法)→ sudo systemctl restart nginx。/var/www/html/your_project)。composer install(根据composer.json安装依赖)。runtime目录(存储缓存、日志)可写:sudo chown -R www-data:www-data /var/www/html/your_projectsudo chmod -R 755 /var/www/html/your_project/runtime。CREATE DATABASE your_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;。.env文件(若无则复制env.example),设置数据库参数:DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db
DB_USERNAME=your_user
DB_PASSWORD=your_password
⚠️ 注意:
.env文件包含敏感信息,需限制权限:chmod 640 .env。
config/app.php,设置'app_debug' => false。php.ini,取消opcache.enable=1注释。sudo apt install php-redis),并在config/cache.php中配置Redis作为缓存驱动。sudo systemctl status php8.2-fpm),确认Nginx配置中的fastcgi_pass路径与PHP-FPM的sock文件一致。try_files $uri $uri/ /index.php?$query_string;(关键规则,用于转发PATHINFO请求)。www-data)和权限(755)。通过以上步骤,ThinkPHP项目即可在Linux环境下正常运行。部署后访问http://yourdomain.com即可看到项目首页。