Amazon EFS: Changing Wordpress upload directory to outside root - wordpress

I have multiple AWS EC2 instances which are updated from a Git repository via CodeDeploy. However, since keeping the wp-content/uploads folder in Git is messy and hard to maintain, I'm instead trying to move all uploads to a directory which I have mounted as an EFS filesystem. That way I should be able to share the uploads between multiple EC2 instances.
However, now I'm running into a new problem; there's no way for me to set the WP uploads folder to be outside of the WP root.
WordPress is located at /opt/bitnami/apps/wordpress/htdocs, which is also where our domain points. The EFS system is mounted to /home/bitnami/efs. Since the EFS directory is located outside of the WP root there's no way for me to link to it.
I have gotten this to work using a symlink directing the default wp-content/uploads folder to my desired path; however, this doesn't really solve my issue since I can't rely on the symlink to not be overwritten during CodeDeploy deployments.
So my questions are as follows:
Is it possible to have this setup work without using a symlink, so I can make deployments without worrying about by uploads being affected?
If a symlink is the only/best way to make this work, is there any way to add the symlink to the git repository, or any other way to make absolutely sure it persists during CodeDeploy deployments?

You can directly mount your EFS to /opt/bitnami/apps/wordpress/htdocs/wp-content/uploads to avoid symlinks.
In your appspec.yml add two hooks:
hooks:
BeforeInstall:
- location: /deploy/BeforeInstall.sh
timeout: 3000
runas: root
AfterInstall:
- location: /deploy/AfterInstall.sh
timeout: 3000
runas: root
Then create directory deploy and two files in it BeforeInstall.sh and AfterInstall.sh
BeforeInstall.sh unmount the EFS if its mounted
#!/bin/bash
if mount | grep /opt/bitnami/apps/wordpress/htdocs/wp-content/uploads > /dev/null; then
sudo umount /opt/bitnami/apps/wordpress/htdocs/wp-content/uploads
fi
Then mount EFS again via AfterInstall.sh after deployment
#!/bin/bash
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-fxxxxxx.efs.us-west-2.amazonaws.com:/ /opt/bitnami/apps/wordpress/htdocs/wp-content/uploads
Note: You can also mount entire wp-contents directory, not just uploads folder, so that update in themes and plugins also get reflected in other EC2s automatically.
Also, symlink is a better solution, so you don't have to ever worry about accidentally removing the EFS mount during deployment if umount ever fails. You can just recreate symlink again after deployment using AfterInstall hook and add code in file AfterInstall.sh
#!/bin/bash
ln -s /home/bitnami/efs /opt/bitnami/apps/wordpress/htdocs/wp-content/uploads

Related

Not able to mount directory after updating fstab

On a Windows file share I created a folder and shared it with a service account.
I have added a mount point entry to fstab like this:
//server/folder /mnt/folder cifs credentials=/root/creds/creds,noperm
Where the creds file contains credentials for above mentioned service account.
Then run mount -a to activate the mount point.
It gives an error like:
mount error(2): No such file or directory
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
Couldn't chdir to /mnt/folder: No such file or directory
I have tried mounting the directory manually and receive the same error.
What the heck am I missing?
Ah. I had not created folder in /mnt/folder on the UNIX side. Did mkdir /mnt/folder.
I was then able to see folder in /mnt but it was empty.
Did a mount -a and can now see the contents of the Windows share in /mnt/folder
I seems like I had to do mount -a from /etc as when I issued the command from /mnt it just hung for a while until I killed it and then tried from /etc

File ownership on docker named volumes

I'm having a folder ownership issue when I try to run WordPress on Docker containers. Folders like wp-content and themes are owned by root, not allowing me to install themes and plugins from the web interface.
Goals
Run WordPress on Docker.
Obtain a theme from a git repo (owned by us).
Be able to run WP-CLI from its official Docker image (wordpress:cli).
Be able to install themes and plugins from the web interface.
Have all files and folders under /var/www/html be owned by www-data (uid 33).
Specs
Docker version 18.09.5, build e8ff056
docker-compose version 1.24.0, build 0aa59064
single container instances for each service - no kubernetes, swarm, stack, etc.
My docker-compose.yml
My dockerfile (copied from the git repo):
FROM wordpress:5
COPY --chown=33 ./ /var/www/html/wp-content/themes/theme/
Volumes
I don't declare volumes on my dockerfile -- it's only those two lines above, nothing else. In fact, this image exists only to copy a folder into the WordPress image. The WordPress image (which my image derives from) declares a volume in its dockerfile, though.
I do declare volumes on my docker-compose file but when omitting them the issue persists
Results
File and folder ownerships...
when using a custom image with named volumes
when using a custom image without named volumes
when using the default image with named volumes
when using the default without named volumes
UPDATE
There's some issue going on with the COPY step on the Docker build, but I can't figure out what.
I changed my dockerfile to
FROM alpine
COPY ./ /var/www/html/wp-content/themes/theme/
RUN chown -R 33:33 /var/www/html
RUN ls -n /var/www/html
If I build from alpine, uid 33 is the owner:
Step 4/4 : RUN ls -n /var/www/html
---> Running in e9850fa85800
total 4
drwxr-xr-x 1 33 33 4096 Apr 12 19:34 wp-content
I change the first line to FROM wordpress, now root is the owner:
Step 4/4 : RUN ls -n /var/www/html
---> Running in 2810cc37aaba
total 4
drwxr-xr-x. 3 0 0 4096 Apr 12 19:38 wp-content
How do I proceed to obtain the results that I want (that is, the theme files on /var/www/html/wp-content/themes/theme/ and all files and folders owned by www-data (uid 33))?
you can set owner using ADD or COPY command in dockerfile, for your COPY command, try COPY --chown:www-data:www-data ./ /var/www/html/wp-content/themes/theme/.

Remote editing of nginx conf file present in an AWS ec2 instance

So, I have installed nginx in an EC2 instance that is running a RHEL OS. Everything is fine, except that I can't remotely edit the nginix conf file using SFTP client Cyberduck.
The problem is AWS EC2 seems to have a restriction on SFTP using root user. I installed nginx with sudo, guess it is saving the conf files with root priviledges. So when I sftp using ec2-user (since root is not allowed), Cyberduck simply complains that you can not save the file because conf file is owned by root.
So nginx developers out there, how do you handle this situation? For me it is really tedious to edit the conf file via ssh and vi editor in EC2 instance. That is why I prefer to edit it locally and sync. But I am not sure how to achieve this. I tried giving access to the conf folder to the ec2-user as per this post too. But invain!. Any help is appreciated.
PS: I installed the nginx using sources and here is the configure command with options I used:
./configure --sbin-path=/usr/bin/nginx --conf-path=/home/ec2-user/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path==/var/log/nginx/access.log --with-pcre --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_v2_module
I finally figured out that we can change the permissions of the root folder from where nginx is serving its contents, in this case /var/share/nginx/html to 777. Also ensure the permissions for files inside this folder have 666 permission.
sudo chmod 777 /var/share/nginx/html
sudo chmod 666 /var/share/nginx/html/*
Also enabled read/write permissions for all users for the configuration files as well. This enabled me remote editing the configs as well as html contents.

Manage wordpress files in google container Engine and kubernetes

I am in the middle of no where. Following this tutorial https://cloud.google.com/container-engine/docs/tutorials/persistent-disk
I deployed wordpress to google container engine. Now i have no idea how to access wordpress files on this Persistent Disks either with ftp or sftp. I can access project files with sftp on filezilla but can't find wordpress core files in it. Is there any way i can access these wordpress files?
The persistent disk containing the wordpress files are attached to the wordpress pod. This disk is mounted on /var/www/html folder under the wordpress pod. You can access these files by connecting to the wordpress pod. First get the name of the running pod by executing the following command,
kubectl get pods
Now use the name of the wordpress pod in the following command. This runs a remote shell on the wordpress pod,
kubectl exec -it <POD_NAME> sh
In the shell, run the ls command to see the list of wordpress files,
# ls
This will list the wordpress files. If you want to edit these files, you need to install vim or nano.
# apt-get update
# apt-get install vim
# apt-get install nano
# vi wp-config.php
Note that the vim/nano will be removed if the wordpress pod is restarted. If you really want them inside your pod, you will need to create a custom container.
First take a look here: https://stackoverflow.com/a/46011597/1197205
This plugin uses Google Cloud Storage so it's easy to access via the UI
Another solution (only if you run 1 pod because otherwise you'd need to sync between disks): use an sftp container as a sidecar for the wordpress pod

How to mount a directory in the docker container to the host?

It's quite easy to mount a host directory in the docker container.
But I need the other way around.
I use a docker container as a development environment for developing WordPress plugins. This docker container contains everything needed to run WordPress (MySQL, Apache, PHP and WordPress). I mount my plugin src folder from the host in the docker container, so that I can test my plugin during development.
For debugging it would be helpful if my IDE running on the host has read access to the WordPress files in the docker container.
I found two ways to solve the problem but both seem really hacky.
Adding a data volume to the docker container, with the path to the WordPress files
docker run ... -v /usr/share/wordpress/ ...
Docker adds this directory to the path on the host /var/lib/docker/vfs/dir... But you need to look up the actual path with docker inspect and you need root access rights to see the files.
Mounting a host directory to the docker container and copying the WordPress files in the container to that mounted host directory. A symlink doesn't seem to work.
Is there a better way to do that? Without copying files or changing access rights?
Thank you!
Copying the WordPress files to the mounted folder was the solution.
I move the files in the container from the original folder to the mounted folder and use symbolic links to link them back to the original folder.
The important part is, the container can follow symbolic links in the container and but the host can't. So just using symbolic links from the original folder to the mounted folder doesn't work, because the host cannot follow symbolic links in the container!
You can share the files using smb with svendowideits samba container like this:
docker run --rm -v $(which docker):/docker -v /var/run/docker.sock:/docker.sock svendowideit/samba <container name>
It's possible to do if you use volume instead of filesystem path. It's created for you automatically, if it already doesn't exist.
docker run -d -v usr_share_wordpress:/usr/share/wordpress --name your_container ... image
After you stop or remove your container, your volume will be stored on your filesystem with files from container.
You can inspect volume content during lifetime of your_container with busybox image. Something like:
docker run -it --rm --volumes-from your_container busybox sh
After shutdown of your_container you can still check volume with:
docker run -it --rm -v usr_share_wordpress:/usr/share/wordpress busybox sh
List volumes with docker volume ls.
I had a similar need of exposing the files from container to the host. There is an open issue on this as of today. One of the work-arounds mentioned, using binds, is pretty neat; it works when the container is up and running:
container_root=$(docker inspect --format {{.State.Pid}} "$container_name")/root
sudo bindfs --map=root/"$USER" "$container_root/$app_folder" "$host_folder"
PS: I am not sure this is good for production, but it should work in development scenarios!
Why not just do: docker run ... -v /usr/share/wordpress/:/usr/share/wordpress. Now your local /usr/share/wordpress/ is mapped to /usr/share/wordpress in the Docker container and both have the same files. You could also mount elsewhere in the container this way. The syntax is host_path:container_path, so if you wanted to mount /usr/share/wordpress from your host to /my/new/path on the container, you'd just do: docker run ... -v /usr/share/wordpress/:/my/new/path.

Resources