10up / wp-local-docker-v2

ARCHIVED: A simple Docker based development environment for WordPress.
MIT License
484 stars 82 forks source link

Mails not working, `/etc/msmtprc: Permission denied` #318

Closed tomalec closed 1 year ago

tomalec commented 2 years ago

Troubleshooting

Describe the bug

Sending emails does not work. Fails with an error PHP Mailer - Could not instantiate mail function..

10updocker logs shows:

phpfpm_1     | ln: failed to create symbolic link '/etc/php.d/15-xdebug.ini': No such file or directory
phpfpm_1     | Added $DOCKER_INTERNAL_HOST to hosts /etc/hosts
phpfpm_1     | /entrypoint.sh: line 3: /etc/msmtprc: Permission denied
phpfpm_1     | /entrypoint.sh: line 10: /etc/php-fpm.d/www.conf: Permission denied
phpfpm_1     | /entrypoint.sh: line 28: /etc/php/7.4/mods-available/newrelic.ini: Permission denied
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: fpm is running, pid 1
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: ready to handle connections
phpfpm_1     | [03-Oct-2022 18:54:07] NOTICE: systemd monitor disabled
phpfpm_1     | [03-Oct-2022 18:54:09] WARNING: [pool www] child 23 said into stderr: "NOTICE: PHP message: Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-("
⋮
phpfpm_1     | [03-Oct-2022 18:54:18] WARNING: [pool www] child 23 said into stderr: "sendmail: account default not found in /etc/msmtprc"

MailCatcher is completely empty

Steps to Reproduce

  1. 10updocker upgrade and 10updocker create with all the defaults
  2. Send any email from the WordPress instance
  3. check the logs
  4. check the MailCatcher http://localhost:1080/

Screenshots, screen recording, code snippet

Environment information

No response

Code of Conduct

tomalec commented 1 year ago

What helped me a little:

  1. cd to env folder.
  2. Edit .containers/php-fpm add RUN chmod u+x /etc/msmtprc
  3. run docket-compose build
kmgalanakis commented 1 year ago

What helped me a little:

  1. cd to env folder.
  2. Edit .containers/php-fpm add RUN chmod u+x /etc/msmtprc
  3. run docket-compose build

How can you verify that this change actually worked? I did the modification shown in step 2 but if I rebuild the image and restart the container, I'm still unable to make the emails work.

kmgalanakis commented 1 year ago

@tomalec OK, I figured this out.

The docker image used for the phpfpm container is the 10up/wp-php-fpm. This image has an entrypoint.sh file, which I'm assuming is executed every time the instance boots.

If you check this file closely, you will notice that very early on this file, on line 3, the script attempts to write to the /etc/msmtprc file, which generates that error mentioned in your (and my) logs.

phpfpm_1 | /entrypoint.sh: line 3: /etc/msmtprc: Permission denied

So, apparently, the script cannot write to the /etc/msmtprc file due to wrong permissions.

Following your suggestion, I had to edit .containers/php-fpm and add RUN chmod a+x /etc/msmtprc. So to give a full walkaround I did the following:

  1. cd to the env folder
  2. Edit .containers/php-fpm and add RUN chmod a+x /etc/msmtprc
  3. Run docker-compose build
jg314 commented 1 year ago

I'm also trying to get MailCatcher to work with no luck. I'm using Windows 11, WP Local Docker v3.0.2 and WSL 2. Here's what I tried:

  1. Opened .containers/php-fpm
  2. Under the line that reads RUN touch /usr/local/etc/msmtprc && chown ${CALLING_USER} $_ I added RUN chmod a+x /etc/msmtprc. I also tried RUN chmod a+x /usr/local/etc/msmtprc with no luck.
  3. I ran docker compose build, then 10updocker stop all, then 10updocker start.
  4. I triggered an email from WordPress, then visited http://localhost:1080/. There are still no emails.

Any ideas on where to go from here? If there are error logs I should be looking at please let me know where to find them. Thanks a ton for the help.

kmgalanakis commented 1 year ago

@jg314 if you do cat /etc/msmtprc is the file empty? If it is then the command on the 3rd line of the entrypoint.sh script is failing to write to the file. If it's not empty, then something else must be wrong.

jg314 commented 1 year ago

If I run cat /etc/msmtprc in the WSL 2 terminal I get a No such file or directory error. Is that what you were running into before you added RUN chmod a+x /etc/msmtprc to the php-fm file?

Rahmon commented 1 year ago

I'm using Linux and I believe I'm facing the same or similar issue faced by @jg314. When an email is sent, I get this error in the log file: sendmail: account default not found in /etc/msmtprc.

I can confirm that the /etc/msmtprc file is empty by running and getting an empty output:

docker exec CONTAINER_NAME-test_phpfpm_1 sh -c "cat /etc/msmtprc"

Other than that, if I try to run the same command in entrypoint.sh to write the default account:

docker exec CONTAINER_NAME-test_phpfpm_1 sh -c "cat > /etc/msmtprc <<EOF      
account default
host ${MAILER_HOST:-mailcatcher}
port ${MAILER_PORT:-1025}
auto_from on
EOF"

I get: cannot create /etc/msmtprc: Permission denied

if we take a look at the permissions of the /etc/msmtprc file, we can see that the ownership (www-data instead of root) is different from the other files:

-rw-r--r-- 1 root     root     72029 Mar 21  2022 /etc/mime.types
-rw-r--r-- 1 root     root       744 Jan  8  2022 /etc/mke2fs.conf
-rw-r--r-- 1 www-data www-data    56 Apr 16 14:01 /etc/msmtprc

So, maybe it's necessary to change the /etc/msmtprc file's ownership before trying to write the default account or run the command with sudo in the entrypoint.sh file.

To "fix" this issue I run the command with sudo (replace CONTAINER_NAME-test_phpfpm_1 with the appropriate container name, you can figure it out by running docker ps):

docker exec CONTAINER_NAME-test_phpfpm_1 sudo sh -c "cat > /etc/msmtprc <<EOF      
account default
host ${MAILER_HOST:-mailcatcher}
port ${MAILER_PORT:-1025}
auto_from on
EOF"

After running the command above, the content of the /etc/msmtprc file should be:

account default
host mailcatcher
port 1025
auto_from on
dustinrue commented 1 year ago

In the .containers/php-fpm file for your project, can you add, under the last RUN line:

RUN chown ${CALLING_USER} /etc/msmtprc

Then rebuild the image. Let me know if this resolves the problem for you.

Thank you

Rahmon commented 1 year ago

It fixed :tada:

  1. Add the command RUN chown ${CALLING_USER} /etc/msmtprc to .containers/php-fpm file:

    ...
    RUN chown ${CALLING_USER} /run/php-fpm
    RUN chown ${CALLING_USER} /var/log/php*log
    RUN chown ${CALLING_USER} /etc/msmtprc
    ...
  2. Run docker-compose build

Thanks @dustinrue :heart:

kmgalanakis commented 1 year ago

@Rahmon and @dustinrue this is the php-fpm file I ended up using

ARG PHP_IMAGE=10up/wp-php-fpm-dev:7.4-ubuntu
FROM ${PHP_IMAGE}

ARG CALLING_USER=www-data
ARG CALLING_UID=33

LABEL "com.10up.wp-local-docker"="user-image"

USER root

RUN useradd ${CALLING_USER} -u ${CALLING_UID}
RUN mkdir -p /run/php-fpm
RUN chown ${CALLING_USER} /run/php-fpm
RUN chown ${CALLING_USER} /var/log/php*log

# Section start - Fix email sending issues
RUN chmod a+w /etc/msmtprc
# Section end

# Section start - Fix "permission denied issue" when executing the "entrypoint.sh" script.
RUN chmod a+w /etc/php-fpm.d/www.conf
# Section end

# Section start - Fix nvm installation issues
# Image "10up/wp-php-fpm-dev:7.4-ubuntu" installs nvm for the "www-data" user so
# it also needs to be "installed" for the "CALLING_USER".
RUN mkhomedir_helper ${CALLING_USER}
RUN cp -a /home/www-data/. /home/${CALLING_USER}
RUN chown -R ${CALLING_USER}:${CALLING_USER} /home/${CALLING_USER}/.
# Section end

USER ${CALLING_USER}

Then I had to run docker-compose build to rebuild the image.