The Uploaded File Could Not Be Moved to Wp-content/uploads Efs Aws
Edifice a WordPress Site using AWS ElasticBeanstalk and EFS
In today's era, most of the popular websites need high availability, scalability too as resiliency. Now, this is provided past the AWS Cloud Services and one must know how to employ the tools and services bachelor in AWS to Architect their surroundings for all these benefits. But what if you are more of a developer and merely want to upload the lawmaking and somehow through magic all these things get automatically configured for them. Oh, wait a minute…... At that place is already a service available that is provided by AWS which allows yous to upload your code and all architecting will be then managed by AWS itself so you don't need to worry about anything regarding the architecting of the environment. The service is AWS Elastic Beanstalk.
Today in this article I am going to prove y'all how to build a WordPress site using this service known as ElasticBeanstalk and using EFS equally our storage device.
Now there are many WordPress sites available and I am hither because the one which deals with uploading the images which volition be shown to the user every bit a mail as soon as they sign-in on the site using a browser.
You can follow the article to find out:
How to design the WordPress site code so all the uploaded images are stored on EFS( this is useful when y'all desire to have mutual storage service for all instances/servers as they volition exist behind an Elastic LoadBalancer and user might not always connect to the aforementioned case and so having common storage is benign as the user volition not need to worry about the example through which they connect equally they are going to run across the images which are stored in EFS(Elastic File Organisation)).
So here is the file model which I am using as my WordPress code.
You tin too follow on your lawmaking files even if they are different as nosotros are going to configure this by creating a new folder named .ebextensions
In .ebextensions folder add together the following config file and name information technology efs.config:
option_settings:
aws:elasticbeanstalk:awarding:environment:
FILE_SYSTEM_ID: YOUR_EFS_FILE_ID
MOUNT_DIRECTORY: '/efs' packages:
yum:
amazon-efs-utils: [] commands:
01_mount:
command: "/tmp/mount-efs.sh" files:
"/tmp/mount-efs.sh":
manner: "000755"
content : |
#!/bin/bash EFS_MOUNT_DIR=$(/opt/elasticbeanstalk/bin/get-config environment -g MOUNT_DIRECTORY)
EFS_FILE_SYSTEM_ID=$(/opt/elasticbeanstalk/bin/go-config environment -chiliad FILE_SYSTEM_ID) repeat "Mounting EFS filesystem ${EFS_FILE_SYSTEM_ID} to directory ${EFS_MOUNT_DIR} ..." echo 'Stopping NFS ID Mapper...'
service rpcidmapd status &> /dev/aught
if [ $? -ne 0 ] ; then
echo 'rpc.idmapd is already stopped!'
else
service rpcidmapd stop
if [ $? -ne 0 ] ; then
repeat 'Fault: Failed to end NFS ID Mapper!'
exit i
fi
fi echo 'Checking if EFS mount directory exists...'
if [ ! -d ${EFS_MOUNT_DIR} ]; then
echo "Creating directory ${EFS_MOUNT_DIR} ..."
mkdir -p ${EFS_MOUNT_DIR}
if [ $? -ne 0 ]; then
echo 'ERROR: Directory creation failed!'
exit 1
fi
else
echo "Directory ${EFS_MOUNT_DIR} already exists!"
fi mountpoint -q ${EFS_MOUNT_DIR}
if [ $? -ne 0 ]; then
echo "mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}"
mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}
if [ $? -ne 0 ] ; and so
echo 'ERROR: Mount command failed!'
exit 1
fi
chmod 777 ${EFS_MOUNT_DIR}
runuser -l ec2-user -c "touch ${EFS_MOUNT_DIR}/it_works"
if [[ $? -ne 0 ]]; then
echo 'ERROR: Permission Error!'
exit 1
else
runuser -l ec2-user -c "rm -f ${EFS_MOUNT_DIR}/it_works"
fi
else
repeat "Directory ${EFS_MOUNT_DIR} is already a valid mountpoint!"
fi echo 'EFS mount complete.' repeat "creating folders (uploads)"
mkdir -p /efs/uploads
chown webapp:webapp /efs/uploads container_commands: 01-rm-wp-content-uploads:
command: rm -rf /var/app/ondeck/wp-content/uploads
02-symlink-uploads:
command: ln -snf /efs/uploads /var/app/ondeck/wp-content/uploads
At present let's sympathise the above lawmaking and where you need to do changes. On the third line, you can see the FILE_SYSTEM_ID: 'YOUR_EFS_FILE_ID'.
Here replace 'YOUR_EFS_FILE_ID' with the File Id of your EFS which you have in your AWS account and which you desire to attach to your servers/instances. If you want to know how to create EFS File in AWS please refer to this documentation of AWS.
Now don't do any other changes in code until you come across container_commands. Now what container_commands is trying to do is to create a symlink betwixt yourwp-content/uploads
and the uploads folder created in the EFS i.e. /efs/uploads
( This file is created by the command mkdir -p /efs/uploads
in the to a higher place code) and and so delete the uploads binder in wp-content
. Now what the symlink is then basically it will create a hard link betwixt your wp-content/uploads
folder and the uploads folder in EFS i.east. /efs/uploads
. Now those who take this code folders as mentioned in Figure 1, know that basically when 1 will try to upload the file it volition be stored in the wp-content/uploads
. And so now when we create a symlink it will create a link that volition transfer all the files that are uploaded to this destination to the destination in EFS i.e. in our example /efs/uploads
.
The line chown webapp:webapp /efs/uploads
is giving the rights to the web users who are going to upload the images. By default when you will meet the rights of this folder information technology would exist ec2-user. You can check the rights of your folder using ls -ltr
. If yous don't have these webapp rights you volition face a problem that when users will upload the image using the browser the image won't go uploaded. So remember to accept webapp rights for the /efs/uploads
. Although here our in a higher place lawmaking will take intendance of all of that and so no need to worry if you are using the above code for your config file.
Merely Caution: If you lot have some images already in your wp-content > uploads and you desire to transfer those to /efs/uploads, do so using the beneath lawmaking and ignore the higher up code. The higher up code is for those who are starting from fresh. Here we are going to delete this uploads binder in wp-content then before deleting do copy all images to efs.
option_settings:
aws:elasticbeanstalk:awarding:surround:
FILE_SYSTEM_ID: YOUR_EFS_FILE_ID
MOUNT_DIRECTORY: '/efs' packages:
yum:
amazon-efs-utils: [] commands:
01_mount:
command: "/tmp/mountain-efs.sh" files:
"/tmp/mount-efs.sh":
mode: "000755"
content : |
#!/bin/bash EFS_MOUNT_DIR=$(/opt/elasticbeanstalk/bin/get-config environment -k MOUNT_DIRECTORY)
EFS_FILE_SYSTEM_ID=$(/opt/elasticbeanstalk/bin/get-config environment -m FILE_SYSTEM_ID) echo "Mounting EFS filesystem ${EFS_FILE_SYSTEM_ID} to directory ${EFS_MOUNT_DIR} ..." echo 'Stopping NFS ID Mapper...'
service rpcidmapd status &> /dev/null
if [ $? -ne 0 ] ; and then
echo 'rpc.idmapd is already stopped!'
else
service rpcidmapd stop
if [ $? -ne 0 ] ; and so
repeat 'Error: Failed to finish NFS ID Mapper!'
exit 1
fi
fi echo 'Checking if EFS mount directory exists...'
if [ ! -d ${EFS_MOUNT_DIR} ]; then
echo "Creating directory ${EFS_MOUNT_DIR} ..."mkdir -p ${EFS_MOUNT_DIR}
if [ $? -ne 0 ]; then
echo 'ERROR: Directory creation failed!'
exit 1
fi
else
echo "Directory ${EFS_MOUNT_DIR} already exists!"
fi mountpoint -q ${EFS_MOUNT_DIR}
if [ $? -ne 0 ]; and so
repeat "mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}"
mount -t efs -o tls ${EFS_FILE_SYSTEM_ID}:/ ${EFS_MOUNT_DIR}
if [ $? -ne 0 ] ; then
echo 'Error: Mount command failed!'
go out 1
fi
chmod 777 ${EFS_MOUNT_DIR}
runuser -l ec2-user -c "touch ${EFS_MOUNT_DIR}/it_works"
if [[ $? -ne 0 ]]; so
echo 'Mistake: Permission Error!'
exit ane
else
runuser -l ec2-user -c "rm -f ${EFS_MOUNT_DIR}/it_works"
fi
else
repeat "Directory ${EFS_MOUNT_DIR} is already a valid mountpoint!"
fi echo 'EFS mount complete.' echo "creating folders (uploads)"
mkdir -p /efs/uploads
chown webapp:webapp /efs/uploads container_commands:
01-cp-wp-content-uploads:
control: cp -r /var/app/ondeck/wp-content/uploads /efs/uploads
02-rm-wp-content-uploads:
control: rm -rf /var/app/ondeck/wp-content/uploads
03-symlink-uploads:
command: ln -snf /efs/uploads /var/app/ondeck/wp-content/uploads
After you did symlink using any of the in a higher place code according to your requirement when you SSH into your example y'all might see that the wp-content > uploads folder will exist of a different colour(in my case light blue) than the other folders present there which means symlink is a success.
Also, I would like to share that above some of you might come up beyond a thought that even I came through while I was doing for the first time is that we are looking into /var/app/current/wp-content
but we are using /var/app/ondeck/wp-content
in the to a higher place config file. Why and then are we using ondeck instead of current? Fifty-fifty when you try to supervene upon ondeck with current in the above config code it will work and ElasticBeanstalk will bear witness the success of all commands executed simply then when you will SSH into the server you won't see the uploads symlink created which is our primary purpose. This is because when we deploy an application it is start done to /var/app/ondeck
and after which all our container commands are run on information technology. Only later everything is ready, the files are moved to /var/app/current
. So, basically running commands on current won't help at this stage at all as that folder contains the previous version of the app.
Now, whenever the user volition upload the images it will be stored on the EFS instead of instance storage.
Now we understood the lawmaking so permit's upload our files to ElasticBeanstalk. Remember to keep the above-created config file in the folder named .ebextensions which will be at the root location. Zippo these files and upload them to AWS ElasticBeanstalk. Follow these steps to upload:
Create an application on Rubberband Beanstalk and give the name according to your desire or requirement.
Create an environs in that application and instead of selecting sample application choose to upload file and upload your zip file we created earlier. Y'all tin change the configuration according to your convenience by selecting configure more options button beside create environment at the lesser .
Once deployed successfully you tin can see the EC2 instances being spun automatically after sometime and if yous take minimum 2 instances in auto scaling then open up both on the browser using their public IP. Login and Upload prototype on ane of the instance and when uploaded open the second server on the browser using its respective public IP and after login, you can see that aforementioned file which we only uploaded on dissimilar server. So as nosotros can run into the prototype on both the instances we can say that our EFS is working and we are seeing these images from our EFS. The aforementioned volition happen even if there would be more than than 2 instances behind load-balancer.
So now we can say that we have successfully created a Beanstalk environment where EFS file will exist mounted to our instances and then we are good to go!!
Source: https://hetulsheth.medium.com/building-a-wordpress-site-using-aws-elasticbeanstalk-and-efs-5ab30771731
0 Response to "The Uploaded File Could Not Be Moved to Wp-content/uploads Efs Aws"
Enregistrer un commentaire