在CentOS系统中配置Laravel路由,你需要遵循以下步骤:
composer global require laravel/installer
确保将Composer的全局bin目录添加到系统的PATH环境变量中。
laravel new project-name
这将在当前目录下创建一个名为project-name
的新Laravel项目。
public
目录下找到.htaccess
文件,并确保其内容如下:<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
nginx.conf
文件中添加以下配置:server {
listen 80;
server_name example.com; # 替换为你的域名或公网IP
root /path/to/your/laravel/project/public; # 替换为你的Laravel项目的public目录路径
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; # 对于PHP-FPM
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
routes/web.php
文件,你可以在这里定义你的路由。例如:Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about');
});
sudo systemctl restart httpd
对于Nginx,运行以下命令:
sudo systemctl restart nginx
现在,你的Laravel路由已经配置完成。访问你在Web服务器配置中设置的域名或IP地址,你应该能看到Laravel应用程序的欢迎页面。