在 Ubuntu 下配置 ThinkPHP 的路由,你需要遵循以下步骤:
首先,确保你已经在 Ubuntu 系统上安装了 PHP 和 Composer。然后,通过 Composer 安装 ThinkPHP:
composer create-project topthink/think=latest-version your_project_name
将 your_project_name 替换为你的项目名称。
在 Ubuntu 下,你可以使用 Nginx 或 Apache 作为 Web 服务器。这里以 Nginx 为例:
a. 安装 Nginx:
sudo apt update
sudo apt install nginx
b. 创建一个新的 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/your_project_name
将 your_project_name 替换为你的项目名称。
c. 在配置文件中添加以下内容:
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 替换为你的项目路径。
d. 创建一个符号链接,将配置文件链接到 sites-enabled 目录:
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
e. 重启 Nginx 服务:
sudo systemctl restart nginx
在 ThinkPHP 中,路由配置文件位于 application/route.php。你可以在这里定义你的路由规则。例如:
<?php
// 应用全局的中间件定义文件
use think\facade\Route;
Route::get('/', 'index/Index/index'); // 首页
Route::get('/hello/:name', 'index/Index/hello'); // 带参数的路由
Route::post('/login', 'index/Index/login'); // POST 请求路由
现在,你的 ThinkPHP 项目已经在 Ubuntu 上配置好了,并且可以使用定义的路由规则访问。