在CentOS環境下部署ThinkPHP靜態資源,可以遵循以下步驟:
首先,確保已經安裝了Nginx。如果尚未安裝,請按照以下命令進行安裝:
sudo yum install epel-release
sudo yum install nginx
編輯Nginx配置文件,通常位于/etc/nginx/nginx.conf
或/etc/nginx/conf.d/default.conf
。在server
塊中,添加以下配置:
server {
listen 80;
server_name your_domain.com; # 替換為你的域名或公網IP
root /path/to/your/thinkphp/project; # 替換為你的ThinkPHP項目路徑
index index.php index.html index.htm;
location /
{
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$
{
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht
{
deny all;
}
}
在ThinkPHP項目中,將靜態資源(如CSS、JavaScript、圖片等)放置在public
目錄下。例如:
/your/thinkphp/project/public/css
/your/thinkphp/project/public/js
/your/thinkphp/project/public/img
保存配置文件并重啟Nginx以應用更改:
sudo systemctl restart nginx
現在,你應該可以通過訪問http://your_domain.com/static/css/style.css
(將URL替換為實際的靜態資源路徑)來訪問ThinkPHP項目中的靜態資源了。