nginx “无法创建可锁定文件”Laravel / Azure(应用服务)

7fyelxc5  于 2023-05-06  发布在  Nginx
关注(0)|答案(1)|浏览(141)

我在向我的应用程序API发出请求时收到间歇性的500:
“无法创建可锁定文件:/home/site/wwwroot/storage/framework/cache/data/d1/38/d138002af2c582ff05ad3ca47d0e367ed67f8162.请确保您有权在此位置创建文件。”
查看堆栈跟踪,问题似乎源于“ThrottleRequests”中间件。
我怀疑有一个进程改变了文件的权限,阻止了'ThrottleRequests'类写入该高速缓存。
1.)我验证了用户在未发生错误时执行写入操作:

sh getfileusers.sh /home/site/wwwroot/storage/framework/cache/data/d1/38/d138002af2c582ff05ad3ca47d0e367ed67f8162

getfileusers.sh内容:

#!/bin/sh

FILE=$1

while true; do 
    lsof "${FILE}" 
done > /home/fileusers.log

fileusers.log内容:

COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME 
php-fpm 14667 www-data   7r   REG  0,104       14 13450135 /home/site/wwwroot/storage/framework/cache/data/d1/38/d138002af2c582ff05ad3ca47d0e367ed67f8162

2.)由于“www-data”可以写入而不会出错,因此我更新了部署脚本,以确保执行artisan命令的其他进程正在使用www-data用户。部署脚本中的相关行:

配置调度程序

(crontab -l 2>/dev/null; echo "* * * * * su www-data -c '/usr/local/bin/php /home/site/wwwroot/artisan schedule:run >> /home/cronresult.txt 2>&1' -s /bin/sh www-data")|crontab

配置主管服务器

echo "[program:laravel-worker]
process_name=%(program_name)s
command=/usr/local/bin/php /home/site/wwwroot/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
redirect_stderr=true
stdout_logfile=/home/site/wwwroot/storage/logs/laravel-worker.log
stopwaitsecs=3600" >> /etc/supervisor/conf.d/laravel-worker.conf

3.)部署脚本中的其他相关行:

php artisan cache:clear
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

有人有什么见解吗?先谢谢你了。

qfe3c7zg

qfe3c7zg1#

确保cron / supervisor像www-data用户那样处理创建的文件。
在部署代码后,我仍然看到这个问题。只需更改部署脚本中的操作顺序即可解决该问题。
这是我的最终部署脚本。希望对某人有帮助:

echo "[INFO]: Replace nginx config with Laravel specific configuration"
cp /home/site/wwwroot/default /etc/nginx/sites-available/default

echo "[INFO]: Restarting server . . ."
service nginx reload

echo "[INFO]: Installing 'node' and 'sudo' . . ."
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
sudo bash /tmp/nodesource_setup.sh
sudo apt-get install -y nodejs

echo "[INFO]: Executing Application Commands:"
cd /home/site/wwwroot

echo "[INFO]: Turning on maintenance mode . . ."
php artisan down || true

echo "[INFO]: Installing/Updating Composer Dependencies . . ."
composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev

echo "[INFO]: Clear Cache . . ."
php artisan cache:clear

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

echo "[INFO]: Clear Expired Password Reset Tokens . . ."
php artisan auth:clear-resets

echo "[INFO]: Clear and Cache Routes . . ."
php artisan route:cache

echo "[INFO]: Clear and Cache Config . . ."
php artisan config:cache

echo "[INFO]: Clear and Cache Views . . ."
php artisan view:cache

echo "[INFO]: Running 'npm install' . . ."
npm install

echo "[INFO]: Running 'npm run build' . . ."
npm run build

echo "[INFO]: Turning off maintenance mode . . ."
php artisan up

echo "[INFO]: Install 'cron'"
## configure cron ##
apt-get update -qq && apt-get install cron -yqq

apt-get install sudo

(crontab -l 2>/dev/null; echo "* * * * * su www-data -c '/usr/local/bin/php /home/site/wwwroot/artisan schedule:run >> /home/cronresult.txt 2>&1' -s /bin/sh www-data")|crontab

service cron start

echo "[INFO]: Install and configure 'supervisor'"
## configure supervisor ##
apt-get install -y supervisor

#Create supervisor configuration file
touch /etc/supervisor/conf.d/laravel-worker.conf

# Add contents to Supervisor configuration file
echo "[program:laravel-worker]
process_name=%(program_name)s
command=/usr/local/bin/php /home/site/wwwroot/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
redirect_stderr=true
stdout_logfile=/home/site/wwwroot/storage/logs/laravel-worker.log
stopwaitsecs=3600" >> /etc/supervisor/conf.d/laravel-worker.conf

service supervisor start
supervisorctl reload
supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker:*

echo "[INFO]: Deployment Complete !!!!!!!!!!!!!!!!!!!!"

相关问题