在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应用程序,并看到配置的路由。