在Ubuntu上部署ThinkPHP前,需安装Web服务器(Nginx/Apache)、PHP环境及数据库(MySQL),并配置Composer依赖管理工具。
sudo apt update && sudo apt upgrade -y
sudo apt install nginx -y
sudo systemctl start nginx && sudo systemctl enable nginx
sudo apt install php php-cli php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
ThinkPHP需将public目录设为Web根目录,并配置URL重写以支持PATHINFO。
sudo nano /etc/nginx/sites-available/thinkphp_project
your_domain_or_ip和/var/www/html/thinkphp_project):server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html/thinkphp_project/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:/var/run/php/php8.1-fpm.sock; # 根据PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sudo ln -s /etc/nginx/sites-available/thinkphp_project /etc/nginx/sites-enabled/
sudo nginx -t # 测试配置语法
sudo systemctl restart nginx
/var/www/html/thinkphp_project(或其他目录)。cd /var/www/html/thinkphp_project
composer install --no-dev --optimize-autoloader
.env文件(位于项目根目录),修改数据库连接信息:DB_TYPE=mysql
DB_HOST=127.0.0.1
DB_NAME=your_database
DB_USER=your_username
DB_PASSWORD=your_password
DB_PORT=3306
www-data)有读写权限:sudo chown -R www-data:www-data /var/www/html/thinkphp_project
sudo chmod -R 755 /var/www/html/thinkphp_project
sudo chmod -R 775 runtime # runtime目录需可写
sudo systemctl start php8.1-fpm && sudo systemctl enable php8.1-fpm
.env文件,设置APP_DEBUG=false。sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain_or_ip
/etc/php/8.1/fpm/php.ini,取消opcache.enable=1注释。sudo systemctl status php8.1-fpm),确认Nginx配置中的fastcgi_pass路径正确。try_files $uri $uri/ /index.php?$query_string;,且location /块未被覆盖。www-data:www-data)和runtime目录可写性。