I have a LAMP server configured in docker for local development. It was an Ubuntu 16, Apache, PHP 7.2, MySQL 5.6 setup, so I can run something like this:
docker run -d -p 80:80 -v ${PWD}:/app --name website_test lamp-server:latest
I'm trying to upgrade it to Ubuntu 22, PHP 8, MySQL 8.0. Here is part of the Dockerfile:
FROM ubuntu:jammyMAINTAINER Ray Hwang <ray.hwang@originalfunction.com># Install packagesENV DEBIAN_FRONTEND noninteractiveRUN apt-get update && \ apt-get -y install libaio1 libaio-dev supervisor git apache2 curl mysql-server php libapache2-mod-php8.0 php-mbstring php-mysql php-curl php-gd && \ echo "ServerName localhost" >> /etc/apache2/apache2.conf# Add image configuration and scriptsADD docker/start-apache2.sh /start-apache2.shADD docker/start-mysqld.sh /start-mysqld.shADD docker/mysql-setup.sh /mysql-setup.shADD docker/run.sh /run.shRUN chmod 755 /*.shADD docker/my.cnf /etc/mysql/conf.d/my.cnfADD docker/supervisord-apache2.conf /etc/supervisor/conf.d/supervisord-apache2.confADD docker/supervisord-mysqld.conf /etc/supervisor/conf.d/supervisord-mysqld.conf# Add MySQL utilsRUN usermod -d /var/lib/mysql/ mysql# config to enable .htaccessADD docker/apache_default /etc/apache2/sites-available/000-default.confRUN a2enmod rewrite# Configure /app folderRUN mkdir -p /app && rm -fr /var/www/html && ln -s /app /var/www/html#Environment variables to configure phpENV PHP_UPLOAD_MAX_FILESIZE 128MENV PHP_POST_MAX_SIZE 128M# Add volumes for MySQL VOLUME ["/etc/mysql", "/var/lib/mysql", "/app" ]EXPOSE 80 3306CMD ["/run.sh"]
After building the image, everything seems to work fine, except connecting to MySQL through Apache.
Here is what does work:
- Connecting to MySQL via CLI
$ mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 22Server version: 8.0.36-0ubuntu0.22.04.1 (Ubuntu)Copyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
- Connecting use PHP from CLI
$ cat test.php<?php$conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD);if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}echo "Connected successfully";$ php test.phpConnected successfully
But when try the same exact PHP script, but executed through Apache, it fails.
[Sun Mar 03 04:55:07.434839 2024] [php:error] [pid 172] [client 172.17.0.1:43990] PHP Fatal error: Uncaught mysqli_sql_exception: Permission denied in /app/index.php:12\nStack trace:\n#0 /app/index.php(12): mysqli->__construct()\n#1 {main}\n thrown in /app/index.php on line 12
After hours of search, it seems like this is either caused by SELinux or Docker. I don't think SELinux is installed? (at least sestatus
returns command not found
). I also tried running the container using --privileged
and/or --security-opt label:disable
, no luck. Someone suggested running chcon -t httpd_sys_content_t
on the files, but it only returns chcon: can't apply partial context to unlabeled
. I also tried installing SELinux (policycoreutils selinux-utils selinux-basics
), but I can never get it to enable?
Hopefully someone knows the root cause of the issue, and possibly a solution.