在Ubuntu下配置ThinkPHP路由,你需要遵循以下步骤:
composer create-project topthink/think=latest_version your_project_name
将latest_version替换为最新的ThinkPHP版本,将your_project_name替换为你的项目名称。
sudo apt update
sudo apt install nginx
然后,创建一个新的Nginx配置文件,用于指向你的ThinkPHP项目:
sudo nano /etc/nginx/sites-available/your_project_name
将your_project_name替换为你的项目名称。在新创建的配置文件中,添加以下内容:
server {
listen 80;
server_name your_domain_or_ip;
root /path/to/your_project_name/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
将your_domain_or_ip替换为你的域名或IP地址,将/path/to/your_project_name替换为你的项目路径。
sites-enabled目录:sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
然后,测试Nginx配置是否正确:
sudo nginx -t
如果一切正常,重启Nginx服务:
sudo systemctl restart nginx
application/route.php文件(如果不存在,请创建一个)。在这个文件中,你可以定义你的路由规则。例如:<?php
use think\Route;
Route::get('/', 'index/Index/index'); // 首页
Route::get('/about', 'index/Index/about'); // 关于页面
Route::post('/submit', 'index/Index/submit'); // 提交表单
这些路由规则将分别映射到index控制器的index、about和submit方法。
现在,你的ThinkPHP项目应该已经成功配置了路由。访问你在Nginx配置中设置的域名或IP地址,你应该能看到相应的页面。