在Debian环境中,为Laravel项目设置权限控制需要遵循以下步骤:
sudo apt-get update
sudo apt-get install nginx php-fpm mysql-server
/etc/nginx/sites-available/yourdomain.com。将以下内容添加到server块中:location / {
try_files $uri $uri/ /index.php?$query_string;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
请根据实际情况替换php7.4-fpm为你的PHP版本。
sudo systemctl restart nginx
sudo chown -R www-data:www-data /path/to/your/laravel/project
sudo find /path/to/your/laravel/project -type f -exec chmod 644 {} \;
sudo find /path/to/your/laravel/project -type d -exec chmod 755 {} \;
这将把所有文件的所有者更改为www-data(Nginx默认用户),并设置适当的文件权限。
storage和bootstrap/cache),可以运行以下命令:sudo chmod -R 775 /path/to/your/laravel/project/storage
sudo chmod -R 775 /path/to/your/laravel/project/bootstrap/cache
然后,为这些目录设置访问控制。编辑Nginx配置文件,添加以下内容:
location /storage {
deny all;
}
location /bootstrap/cache {
deny all;
}
接下来,创建一个新的Nginx配置文件,例如/etc/nginx/conf.d/yourdomain.com.storage,并添加以下内容:
server {
listen 80;
server_name yourdomain.com;
location /storage {
allow 127.0.0.1;
allow ::1;
allow your_ip_address;
deny all;
root /path/to/your/laravel/project;
}
}
将your_ip_address替换为你自己的IP地址。这将允许你从特定IP地址访问storage目录。
sudo systemctl restart nginx
现在,Laravel项目的权限控制已经设置好了。你可以根据需要进一步自定义权限和访问控制。