发布于2026-07-04 阅读(0)
扫一扫,手机访问
先说说背景:ThinkPHP作为国内最流行的PHP框架之一,部署起来其实不算复杂,关键是把几个基础组件的版本和配置对齐。下面我们一步步拆解,从零开始搭建一个可用的生产/开发环境。

ThinkPHP对底层的要求很清晰——PHP + Web服务器 + Composer,三者缺一不可。先把系统软件包拉平,避免后续依赖冲突。
sudo apt update && sudo apt upgrade -y;CentOS/RHEL:sudo yum update -ypdo_mysql(数据库)、mbstring(字符集)、xml(解析)、curl(网络请求)等。命令示例:
# Ubuntu/Debian
sudo apt install php php-cli php-fpm php-mysql php-mbstring php-xml php-curl -y
# CentOS/RHEL
sudo yum install php php-cli php-fpm php-mysqlnd php-mbstring php-xml php-curl -y# Ubuntu/Debian
sudo apt install nginx -y
# CentOS/RHEL
sudo yum install epel-release && sudo yum install nginx -ycurl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
验证:运行 composer --version 看到版本号即为成功。关键在于让Nginx正确解析PHP文件,同时支持ThinkPHP的PATHINFO路由模式。做法如下:
/etc/nginx/sites-a vailable/thinkphp.conf,内容模板如下(将 /path/to/tp6/public 替换为项目实际路径):
server {
listen 80;
server_name yourdomain.com; # 替换为域名或IP
root /path/to/tp6/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string; # 关键:支持PATHINFO
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock; # 根据PHP版本调整(如php8.2-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-a vailable/thinkphp.conf /etc/nginx/sites-enabled/
sudo nginx -t # 测试配置语法
sudo systemctl restart nginx如果习惯Apache,需要先启用mod_rewrite模块,然后配置虚拟主机:
sudo a2enmod rewrite
sudo systemctl restart apache2
编辑 /etc/apache2/sites-a vailable/000-default.conf,将 DocumentRoot 指向项目public目录(如 /var/www/html/tp6/public),并在 中添加 AllowOverride All,确保 .htaccess 能生效。
两种方式:
composer create-project topthink/think tp6 /var/www/html/tp6
替换为你的实际项目目录即可。/var/www/html/tp6)。注意:如果使用Nginx,需要删除 public/.htaccess 文件(Nginx不依赖它)。.env 文件(如果没有,从 config/.env.example 复制而来),配置数据库信息:
DATABASE_TYPE=mysql
DATABASE_HOSTNAME=127.0.0.1
DATABASE_NAME=your_dbname
DATABASE_USERNAME=your_username
DATABASE_PASSWORD=your_password
DATABASE_PORT=3306runtime 目录可写,用于存放缓存和日志:
sudo chown -R www-data:www-data /var/www/html/tp6/runtime # Ubuntu/Debian
sudo chmod -R 755 /var/www/html/tp6/runtimeconfig/app.php 中 'app_debug' => true),便于排查问题;生产环境务必关闭,否则可能泄露敏感信息。sudo systemctl start nginx # 或 apache2
sudo systemctl enable nginx # 设置开机自启
浏览器访问 http://yourdomain.com(或IP),如果看到ThinkPHP的默认欢迎页面,恭喜,环境配置成功。
fastcgi_pass 路径与PHP-FPM的实际监听路径不匹配。先 sudo systemctl status php-fpm 检查状态,再核对路径(比如 /run/php/php-fpm.sock)。try_files $uri $uri/ /index.php?$query_string; 这一行,它是支持ThinkPHP PATHINFO模式的核心规则。composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 即可。config/app.php 中的 'app_debug' 设为 false,并清理 runtime 目录中的调试文件。sudo certbot --nginx -d yourdomain.com,其余自动化处理。php.ini 中设置 opcache.enable=1),并将会话存储切换到Redis——修改 config/session.php 中的 'type' => 'redis',效果立竿见影。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8