在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應用程序的歡迎頁面。