Azure / wordpress-linux-appservice

MIT License
113 stars 71 forks source link

How to use wp cli? #72

Closed kubami closed 1 year ago

kubami commented 1 year ago

I see wpcli is installed in the service but I cannot seem to make it work.

/var/www/wordpress# wp --allow-root plugin list

Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /var/www/wordpress/wp-includes/class-wpdb.php on line 2019
Error: `No such file or directory`
Error establishing a database connection

I presume it has something to do with the env variables for the db connection not being present in the shell I am in?

I open the ssh connection through Web App -> App Service plan -> Development Tools -> SSH.

rabollin commented 1 year ago

Currently issue is being investigated, there are some challenges with user permissions in accessing wp-cli.

ZubaeyrMSFT commented 1 year ago

@kubami

If you are running interactively via WebSSH, then you need to first update the wp-config.php file with database connection details. The reason is that database connection details are wired into wp-config.php file from the Application Settings and those are accessible only by the main thread that starts the container.

Look for this block of code in wp-config.php file

$connectstr_dbhost = getenv('DATABASE_HOST');                                                                
$connectstr_dbname = getenv('DATABASE_NAME');                                                                
$connectstr_dbusername = getenv('DATABASE_USERNAME');                                                        
$connectstr_dbpassword = getenv('DATABASE_PASSWORD');

Comment it out and add a new block with actual database connection details.

//$connectstr_dbhost = getenv('DATABASE_HOST');                                                                
//$connectstr_dbname = getenv('DATABASE_NAME');                                                                
//$connectstr_dbusername = getenv('DATABASE_USERNAME');                                                        
//$connectstr_dbpassword = getenv('DATABASE_PASSWORD');

$connectstr_dbhost = 'servername.mysql.database.azure.com';                                                    
$connectstr_dbname = 'database_name';                                                              
$connectstr_dbusername = 'db_username';                                                        
$connectstr_dbpassword = 'db_password';

Now try to run the below sample wp-cli commands from WebSSH console.

wp help --path=/home/site/wwwroot --allow-root
wp plugin list --path=/home/site/wwwroot --allow-root 

Note: You can retrieve database connection details from your App Service dashboard in Azure Portal. (Configuration -> Application Settings -> Advanced edit)

rabollin commented 1 year ago

@kubami - Can you confirm if your issue is resolved and can close this GH issue.

ZubaeyrMSFT commented 1 year ago

@kubami WP-CLI is now fully enabled in production. You can now access and use it from SSH console of your SCM site.

Please refer to the following document for more details: https://github.com/Azure/wordpress-linux-appservice/blob/main/WordPress/how_to_use_wpcli_tool.md