在Debian系統中配置Laravel路由,你需要遵循以下步驟:
composer global require laravel/installer
然后,將Composer的全局二進制文件目錄添加到系統的PATH環境變量中。編輯~/.bashrc或~/.zshrc文件,添加以下行(取決于你使用的shell):
export PATH="$HOME/.composer/vendor/bin:$PATH"
保存文件并運行source ~/.bashrc或source ~/.zshrc使更改生效。
laravel new project-name
將project-name替換為你的項目名稱。這將在當前目錄下創建一個新的Laravel項目。
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
/etc/nginx/sites-available/laravel的新文件:sudo nano /etc/nginx/sites-available/laravel
server {
listen 80;
server_name example.com; # 替換為你的域名
root /var/www/laravel/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/php7.4-fpm.sock; # 根據你的PHP版本修改
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
保存并關閉文件。
創建一個符號鏈接,將站點配置文件鏈接到sites-enabled目錄:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
routes/web.php文件,開始定義你的路由。例如:use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about');
});
cd /var/www/laravel
php artisan serve
現在,你可以在瀏覽器中訪問你的Laravel應用程序,并看到配置的路由。