在Linux环境下设置Laravel路由,你需要遵循以下步骤:
composer global require laravel/installer
laravel new project-name
这将在当前目录下创建一个名为project-name的新文件夹,其中包含Laravel应用程序的所有文件。
public目录。这是Nginx和Apache的配置示例:server {
listen 80;
server_name example.com;
root /path/to/your/laravel-project/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;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
<VirtualHost *:80>
ServerName example.com
DocumentRoot /path/to/your/laravel-project/public
<Directory /path/to/your/laravel-project/public>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
记得将example.com替换为你的域名,将/path/to/your/laravel-project替换为你的Laravel项目的实际路径。
storage和bootstrap/cache目录具有可写权限:sudo chmod -R 775 /path/to/your/laravel-project/storage
sudo chmod -R 775 /path/to/your/laravel-project/bootstrap/cache
php artisan serve
现在,你可以在浏览器中访问你的Laravel应用程序,例如:http://example.com。要定义路由,请打开routes/web.php文件并添加你需要的路由规则。例如:
Route::get('/', function () {
return 'Hello, World!';
});
保存文件后,刷新浏览器页面,你应该看到"Hello, World!"的输出。这就是在Linux环境下设置Laravel路由的基本过程。