在部署前需安装Web服务器(Nginx/Apache)、PHP环境、Composer(依赖管理)、MySQL/MariaDB(数据库),确保系统具备运行ThinkPHP的基础条件。
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo yum install php php-fpm php-mysqlnd php-gd php-mbstring php-curl php-xml php-zip -y
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
根据选择的Web服务器调整配置,确保能正确解析PHP请求并指向项目目录。
/etc/nginx/conf.d/thinkphp.conf(或新建站点配置),添加以下内容:server {
listen 80;
server_name your_domain.com; # 替换为你的域名/IP
root /path/to/your/thinkphp/project/public; # 必须指向public目录(ThinkPHP入口)
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string; # 路由转发到index.php
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock; # 与php-fpm监听端口一致
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all; # 禁止访问.htaccess文件
}
}
测试配置并重启Nginx:sudo nginx -t
sudo systemctl restart nginx
mod_rewrite模块,并创建虚拟主机配置,确保AllowOverride All(允许.htaccess生效)。将项目代码上传至服务器指定目录(如/var/www/html/thinkphp),推荐使用Git或FTP工具。
cd /path/to/your/project
git clone https://github.com/your/thinkphp-project.git .
--no-dev跳过开发依赖):composer install --no-dev --optimize-autoloader
.env文件(优先级高于config/database.php),修改数据库配置:DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE your_database_name;
SOURCE /path/to/your/thinkphp/project/database/init.sql; # 导入初始化表结构(若有)
确保项目目录权限正确,避免因权限问题导致无法访问或写入。
sudo chown -R nginx:nginx /path/to/your/thinkphp/project # 若使用Nginx,用户为nginx
sudo chmod -R 755 /path/to/your/thinkphp/project
sudo chmod -R 775 /path/to/your/thinkphp/project/runtime # runtime目录需可写
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
http://your_domain.com,若看到ThinkPHP欢迎页面,说明部署成功。.env文件,将APP_DEBUG=true改为APP_DEBUG=false,避免泄露敏感信息。sudo yum install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com
php.ini,启用OPcache加速PHP执行:opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
.env配置)。sudo systemctl status php-fpm),确认Nginx配置中的fastcgi_pass路径与php-fpm监听端口一致。try_files $uri $uri/ /index.php?$query_string;,且PATH_INFO参数正确传递。nginx),runtime目录是否可写。