以下是Linux环境下配置ThinkPHP的步骤:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo yum update -y # CentOS/RHEL
# Ubuntu/Debian
sudo apt install php php-fpm php-mysql php-mbstring php-xml php-curl -y
# CentOS/RHEL
sudo yum install php php-fpm php-mysqlnd php-mbstring php-curl php-xml -y
sudo apt install nginx # Ubuntu/Debian
sudo yum install nginx # CentOS/RHEL
sudo apt install apache2 # Ubuntu/Debian
sudo yum install httpd # CentOS/RHEL
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer create-project topthink/think tp6 # 替换为项目目录名
/var/www/html/tp6),确保public目录可访问。/etc/nginx/sites-available/tp6(新建文件),添加:server {
listen 80;
server_name yourdomain.com;
root /var/www/html/tp6/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock; # 根据PHP版本调整
}
}
启用配置并重启Nginx:sudo ln -s /etc/nginx/sites-available/tp6 /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx
mod_rewrite模块:sudo a2enmod rewrite
sudo service apache2 restart
在项目根目录创建.htaccess文件,内容参考。修改项目根目录.env文件:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_user
DB_PASSWORD=your_db_password
sudo chown -R www-data:www-data /var/www/html/tp6 # Ubuntu/Debian
sudo chown -R apache:apache /var/www/html/tp6 # CentOS/RHEL
sudo chmod -R 755 /var/www/html/tp6/runtime
php think run。http://yourdomain.com。.env中APP_DEBUG=false。fastcgi_pass路径正确。try_files规则。参考来源: