在 CentOS 系统中配置 Laravel 路由,你需要遵循以下步骤:
首先,确保你已经在 CentOS 上安装了 PHP 和 Composer。然后,通过 Composer 全局安装 Laravel 安装程序:
composer global require laravel/installer 将 Composer 的全局 bin 目录添加到你的系统 PATH 中。这样你就可以在任何地方运行 laravel 命令。
使用以下命令创建一个新的 Laravel 项目:
laravel new project-name 将 project-name 替换为你的项目名称。这将在当前目录下创建一个新的 Laravel 项目。
在 CentOS 上,我们通常使用 Nginx 或 Apache 作为 Web 服务器。下面是配置 Nginx 和 Apache 的示例。
Nginx 配置
编辑 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf),并添加以下内容:
server { listen 80; server_name example.com; # 替换为你的域名或公网 IP 地址 root /path/to/your/laravel/project/public; # 替换为你的 Laravel 项目 public 目录的路径 index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } Apache 配置
启用 mod_rewrite 模块:
sudo a2enmod rewrite 编辑 Apache 配置文件(通常位于 /etc/httpd/conf/httpd.conf 或 /etc/apache2/sites-available/000-default.conf),并添加以下内容:
<VirtualHost *:80> ServerName example.com # 替换为你的域名或公网 IP 地址 DocumentRoot /path/to/your/laravel/project/public # 替换为你的 Laravel 项目 public 目录的路径 <Directory /path/to/your/laravel/project/public> AllowOverride All Require all granted </Directory> </VirtualHost> 打开 Laravel 项目中的 routes/web.php 文件,开始配置路由。例如:
Route::get('/', function () { return 'Hello, World!'; }); Route::get('/about', function () { return 'About page'; }); 对于 Nginx:
sudo systemctl restart nginx 对于 Apache:
sudo systemctl restart httpd 现在,你应该可以通过访问你的域名或公网 IP 地址来查看 Laravel 应用程序,并看到配置的路由生效。