Salvatore
Salvatore

Reputation: 1435

Laravel - AWS Beanstalk - Storage symlink not working (403 error)

I am using elastic beanstalk to deploy my laravel application. Everything is working fine except for my images as I need to create a symbolic link with storage to access it publicly.

P.S. Works fine on my local

My .ebextensions file is as follows -

commands:
  composer_update:
    command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update

option_settings:
  - namespace: aws:elasticbeanstalk:application:environment
    option_name: COMPOSER_HOME
    value: /root

container_commands:
  01-install_dependencies:
    command: "php /usr/bin/composer.phar install"
    cwd: "/var/app/ondeck"
  02_storage_sym_link:
    command: "php artisan storage:link"
    cwd: "/var/app/ondeck"
    leader_only: true

Below is the log from my ec2 instance to confirm that the command worked just fine and the link was created successfully.

[2019-04-21T15:47:16.899Z] INFO  [21538] - [Application update symlink alt2@208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Test for Command 02_storage_sym_link] : Starting activity...
[2019-04-21T15:47:16.903Z] INFO  [21538] - [Application update symlink alt2@208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Test for Command 02_storage_sym_link] : Completed activity. Result:

  Completed successfully.
[2019-04-21T15:47:16.903Z] INFO  [21538] - [Application update symlink alt2@208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Command 02_storage_sym_link] : Starting activity...
[2019-04-21T15:47:17.014Z] INFO  [21538] - [Application update symlink alt2@208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Command 02_storage_sym_link] : Completed activity. Result:
  The [public/storage] directory has been linked.

The error I am getting is as follows which makes the images unavailable for public access.

Forbidden
You don't have permission to access /storage/blog/images/8Yhb4OZJQIKwlMGaGE803niTxyjfzNSVTj2BiPaP.gif on this server.

Any help guiding me to the right path is appreciated. Cheers!


EDIT 1 :

container_commands:
  01-install_dependencies:
    command: "php /usr/bin/composer.phar install"
    cwd: "/var/app/ondeck"
  02_storage_sym_link:
    command: "ln -s storage/app/public public/storage"
    cwd: "/var/app/ondeck"
  03_give_ec2_user_perm_1:
    command: "sudo chown -R ec2-user /var/app/current"
  03_give_ec2_user_perm_2:
    command: "sudo chmod -R 777 /var/app/current"

Tried creating the symlink manually plus gave permission to the ec2-user. But still no luck :(

Upvotes: 5

Views: 2763

Answers (2)

Salvatore
Salvatore

Reputation: 1435

Putting down an alternate option which does the job for people who are/might have a similar issue -

Use S3 for file storage in your Laravel application.

To make it happen -

  1. Create a public S3 bucket.
  2. Create an IAM user which has full access to S3 bucket. (With the access key, the application will have permissions to read and write in the S3 bucket.)
  3. Update the config file filesystems.php to use the S3 bucket. (This config handles the storage config of the application.)

Refer the Laravel Doc for more info.

Thanks to @PranavanSp for his suggestion.

Upvotes: 3

DR.
DR.

Reputation: 593

Container commands are run as root, that is why when you tried to run it as the ec2-user you couldn't. The ec2-user is not in the root user group.

Then when you create symlinks, try to do it in the actual app directory(current):

container_commands:
  01-install_dependencies:
    command: "php /usr/bin/composer.phar install"
    cwd: "/var/app/ondeck"
  02_storage_sym_link:
    command: "ln -s storage/app/public public/storage"
    cwd: "/var/app/current"

Or try to link directly as so:

container_commands:
  01-install_dependencies:
    command: "php /usr/bin/composer.phar install"
    cwd: "/var/app/ondeck"
  02_storage_sym_link:
    command: "ln -s /var/app/ondeck/storage/app/public  /var/app/current/public/storage"

EBS Files can be annoying to get right at first but worth it in the end. If this still doesn't work, maybe the user(appache I assume) that runs the server does not have access to that folder. To just quickly verify this just do a:

sudo chmod -R 755  /var/app/ondeck/storage/app/public

Upvotes: 2

Related Questions