Apache Laravel在离线环境中与Gitlab runner持续集成(CentOS 7)

Apache Laravel在离线环境中与Gitlab runner持续集成(CentOS 7),apache,continuous-integration,centos7,gitlab-ci,gitlab-ci-runner,Apache,Continuous Integration,Centos7,Gitlab Ci,Gitlab Ci Runner,我正在开发一个完全离线的网站。另外,我使用gitlab runner for CI,主机是CentOS 7 问题是,gitlab runner在centos上使用gitlab runneruser来部署laravel应用程序,apache使用apacheuser来运行laravel。 我在apache上获得了权限被拒绝错误,直到我更改了文件的所有权。之后,我在apache日志中发现以下错误: Uncaught UnexpectedValueException:无法打开流或文件“storage/l

我正在开发一个完全离线的网站。另外,我使用gitlab runner for CI,主机是CentOS 7

问题是,gitlab runner在centos上使用
gitlab runner
user来部署laravel应用程序,apache使用
apache
user来运行laravel。 我在apache上获得了
权限被拒绝
错误,直到我更改了文件的所有权。之后,我在apache日志中发现以下错误:

Uncaught UnexpectedValueException:无法打开流或文件“storage/logs/laravel.log”:无法打开流:权限被拒绝

似乎有些供应商库,如
monolog
希望在
存储/logs/laravel.log
上写入错误或调试日志,但其权限被拒绝:(

.gitlab ci.yml

stages:
  - build
  - test
  - deploy

buildBash:
  stage: build
  script:
    - bash build.sh

testBash:
  stage: test
  script:
    - bash test.sh

deployBash:
  stage: deploy
  script:
    - sudo bash deploy.sh
build.sh

#!/bin/bash

set -xe

# creating env file from production file
cp .env.production .env

# initializing laravel
php artisan key:generate
php artisan config:cache

# database migration
php artisan migrate --force
#!/bin/bash

PWD=$(pwd)'/public'
STG=$(pwd)'/storage'

ln -s $PWD /var/www/html/public
chown apache.apache -R /var/www/html/public
chmod -R 755 /var/www/html/public
chmod -R 775 $STG
deploy.sh

#!/bin/bash

set -xe

# creating env file from production file
cp .env.production .env

# initializing laravel
php artisan key:generate
php artisan config:cache

# database migration
php artisan migrate --force
#!/bin/bash

PWD=$(pwd)'/public'
STG=$(pwd)'/storage'

ln -s $PWD /var/www/html/public
chown apache.apache -R /var/www/html/public
chmod -R 755 /var/www/html/public
chmod -R 775 $STG

我是否正确使用gitlab runner?如何修复权限被拒绝的错误?

SELinux

我发现了问题所在,它是selinux,就像往常一样,它是selinux,我一开始就忽略了它


有什么问题吗

您可以使用
ls-lZ
命令在文件上查看selinux上下文,默认情况下,www上的所有文件都是
httpd\u sys\u content\u t
,问题是selinux只允许apache读取这些文件。您应该更改
存储
引导/cache
上下文,使其可写

有4种apache上下文类型:

  • httpd_sys_content_t:只读目录和文件
  • httpd_sys_rw_content_t:Apache使用的可读写目录和文件
  • httpd_log_t:由Apache用于日志文件和目录
  • httpd_cache_t:由Apache用于缓存文件和目录
该做什么:

首先,安装
policycoreutils-python
以获得更好的命令

yum安装-y policycoreutils-python

安装
policycoreutils-python
后,
semanage
命令可用,因此您可以像这样更改文件上下文:


semanage fcontext-a-t httpd_sys_rw_content_t”/var/www/html/laravel/storage(/.*)”
semanage fcontext-a-t httpd_sys_rw_content_t”/var/www/html/laravel/bootstrap/cache(/.*)”

不要忘记通过以下命令提交更改:


restorecon-Rv/var/www/html/laravel/storage
restorecon-Rv/var/www/html/laravel/bootstrap/cache

问题解决了:)

参考: