温馨提示×

怎样在ubuntu中配置thinkphp路由

小樊
46
2026-01-03 05:54:38
栏目: 编程语言

在 Ubuntu 中配置 ThinkPHP 路由,你需要遵循以下步骤:

  1. 安装 ThinkPHP 框架:首先,确保你已经在 Ubuntu 服务器上安装了 PHP 和 Composer。然后,通过 Composer 安装 ThinkPHP 框架。在终端中运行以下命令:
composer create-project topthink/think=latest-version your_project_name

your_project_name 替换为你的项目名称。

  1. 配置 Web 服务器:这里以 Nginx 为例。首先,确保你已经安装了 Nginx。然后,创建一个新的 Nginx 配置文件,例如 /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$ {
        fastcgi_pass unix:/var/run/php/php7.4-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;
    }
}

your_domain_or_ip 替换为你的域名或 IP 地址,将 /path/to/your_project_name 替换为你的项目实际路径。

  1. 启用新的 Nginx 配置:创建一个符号链接,将新配置文件链接到 /etc/nginx/sites-enabled 目录:
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
  1. 重启 Nginx 服务:
sudo service nginx restart
  1. 配置 ThinkPHP 路由:进入你的项目目录,打开 application/route.php 文件。在这个文件中,你可以定义你的路由规则。例如:
<?php
// 应用入口文件

use think\facade\Route;

Route::get('/', 'index/Index/index'); // 首页
Route::get('/about', 'index/Index/about'); // 关于页面
Route::post('/submit', 'index/Index/submit'); // 提交表单

现在,你的 ThinkPHP 项目已经配置好了路由。访问你在 Nginx 配置中设置的域名或 IP 地址,你应该能看到相应的页面。

0