Deploy Lamp Stack on Kubernetes Cluster Task

May we can use environment variables in the wp-config.php

<?php // ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', getenv('DB_NAME') ); /** MySQL database username */ define( 'DB_USER', getenv('DB_USER') ); /** MySQL database password */ define( 'DB_PASSWORD', getenv('DB_PASSWORD') ); /** MySQL hostname */ define( 'DB_HOST', getenv('DB_HOST') );

I did try that. But it didn’t work out for me.
here are the changes that I made…

site was also working fine… but I got this 'Mysql database must not be hardcoded in ‘wp-config.php’

here is the screenshot of the site

That is strange. Makes zero sense to me. The database name is clearly not hardcoded and is read from an environment variable. Does the file wp-config.php needs to be protected with an .htaccess file? Also change file permission to read only by the owner and word press? Chmod 400. At this point I am not sure what is the right way

I think getenv() is a function which returns a value. From the validation message, l think when using getenv(), this value is stored in wp-config.php file in a clear text format. Therefore we should reference to database info in another way.

@Devops I also tried the following method in which I referenced the values from the server but with no avail and it also returned error to me:
Reading wp-config.php env vars in database settings · Issue #2365 · wp-cli/wp-cli · GitHub

If you do succeed in doing the task please let me know. Thanks

@satagrawal Yes Sir, clearly the values were referenced from the Environment Variables , at least that’s what I thought. In my opinion , there has to be some kind of error in the validation process.

i have got same error as you, task was failed " Mysql database must not be hardcoded in ‘wp-config.php’ ",

i was trying to inject variables in wordpress section in the forme bellow which permit to fill the ‘wp-config.php’ automatically with a plein text value.

  • env:
    - name: WORDPRESS_DB_NAME
    valueFrom:
    secretKeyRef:
    key: database
    name: mysql-db-url
    - name: WORDPRESS_DB_USER
    valueFrom:
    secretKeyRef:
    key: username
    name: mysql-user-pass
    - name: WORDPRESS_DB_PASSWORD
    valueFrom:
    secretKeyRef:
    key: password
    name: mysql-user-pass
    - name: WORDPRESS_DB_HOST
    valueFrom:
    secretKeyRef:
    key: host
    name: mysql-host
    image: wordpress:php7.4-apache
    name: httpd-php-wp-container

@mouador could you give us a sign especially you have already completed this task, tahnks in advance.

@yelmir

You explicitly write the name of database in wp-config.php file

The task should notify clearly about this.

1 Like

Hello Yassine, I’ve just try to test in my local lab, I’ve not got that task before ^^

I read an answer that is works like that : (I’m not sure)

define('DB_NAME', $_SERVER['MYSQL_DATABASE']);
define('DB_USER', $_SERVER['MYSQL_USER']);
define('DB_PASSWORD', $_SERVER['MYSQL_ROOT_PASSWORD']);
define('DB_HOST', $_SERVER['MYSQL_HOST']);

or trying to parse a cmd like that in wp container

wp core config --dbname=${MYSQL_DATABASE} --dbuser=${MYSQL_USER} --dbpass=${MYSQL_ROOT_PASSWORD}```
1 Like

variables must be set in /etc/httpd/conf/httpd.conf
link : Wordpress + Heroku + PostgreSQL + Amazon S3 = ¥0 / 無料でサイト運営 - Qiita

1 Like

@yelmir, sorry for the issue, this is marked success for you.

2 Likes

@Narendra096 Check with @rahul456 to see if he can mark this task as a success for you?

1 Like

@Narendra096, sorry for the issue, this is marked success for you.

@Narendra096 I just tried this task and it succeeded. The only other additional steps I did was besides providing the database settings as environment variables, I moved the wp-config.php file to a level above i.e. from /var/www/html to var/www and changed the file permissions to 400 i.e. chmod 400 wp-config.php. Site came up without issues. Hope this helps for future tasks Thanks for you input

In my case, I tried to configure the parameter from the UI (port 30008), and it showed no error message. It’s expected to explicitly write the wp-config.php through environment variable?

thanks assi Mouad for your availability, and your help.

1 Like

U’re welcome brother

1 Like

KIndly let me know what wrong i did.
thor@jump_host /$ kubectl exec -it lamp-wp-b84f6db5c-5pf76 -c mysql-container – mysql -uroot -pR00t kodekloud_db3 < /tmp/db.sql
Unable to use a TTY - input is not a terminal or the right kind of file
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘root’@‘10.44.0.0’ (using password: YES)
command terminated with exit code 1

did you have any luck with this, I have created the containers and pods are running. But I have no idea how to access or see the results anywhere.

Can anyone please tell how to do the below task.

a) import the db.sql dump to the database which you are created in mysql-container. I tried below steps but getting error

thor@jump_host /$ kubectl exec -it lamp-wp-b84f6db5c-5pf76 -c mysql-container – mysql -uroot -pR00t kodekloud_db3 < /tmp/db.sql
Unable to use a TTY - input is not a terminal or the right kind of file
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘root’@‘10.44.0.0’ (using password: YES)
command terminated with exit code 1