# Installer wp-cli sur un serveur ovh mutualisé





Rappel: voici ce dont on a a accès lorsque que l'on se connecte en ssh:

```bash
myuser@ssh1.300gp.ha.ovh.net (php/7.1/production/stable) ~ $ l
total 2855
drwxr-xr-x+ 14 myuser users      22 sept. 15 12:44 .
drwxr-xr-x   4 root     root       80 sept. 17 05:46 ..
-rw-------+  1 myuser users   11671 sept. 17 06:24 .bash_history
-rw-r--r--+  1 myuser users     246 sept. 15 14:22 .bash_profile
-rw-r--r--+  1 myuser users      49 sept. 15 06:59 ._bashrc
drwx---r-x+  2 myuser users       2 oct.  15  2008 cgi-bin
drwxr-xr-x+  3 myuser users       6 sept. 15 06:52 .composer
drwxr-xr-x+  3 myuser users       3 août   3  2016 git
-rw-r--r--+  1 myuser users     447 mai   24  2003 LISEZ-MOI
drwxr-xr-x+  4 myuser users       4 août  25 06:30 opt
-rw----r--+  1 myuser users     104 janv.  1  2018 .ovhconfig
-rw----r--+  1 myuser users      23 janv. 13  2017 php.ini
drwx---r-x+ 36 myuser users      38 oct.  15  2008 requetes
drwx---r-x+  2 myuser users       2 mai   24  2003 sessions
drwx------+  2 myuser users       4 sept. 15 05:40 .ssh
drwxr-xr-x+  5 myuser users       7 sept. 15 09:21 .wp-cli
drwx---r-x+ 18 myuser users      39 sept. 17 05:57 www


```



### Composer

On commence par [installer Composer](https://getcomposer.org/download/) (dans opt/bin):

```bash
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

$ php composer-setup.php --install-dir=opt/bin

$ php -r "unlink('composer-setup.php');"

```

On vérifie la version installée:

```bash
myuser@ssh2.300gp.ha.ovh.net (php/7.1/production/stable) ~ $ composer -V
Composer version 1.7.2 2018-08-16 16:57:12
```



### wp-cli

Pas moyen de télécharger directement wp-cli (Connection refused):

```bash
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
```

Il faut d'abord le télécharger en local puis le renvoyer sur ovh.

On le rend exécutable et on le déplace dans opt/bin.

```bash
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar opt/bin/wp
```

On vérifie que tout va bien avec `wp --info`

On peut ajouter un fichier de configuration `config.yml` dans `~/.wp-cli/`

```yaml
path: ../www/wordpress
url: https://clicclac.info/wordpress
core config:
    dbhost: db.mysql.db
    dbname: dbwp
    dbuser: userwp
    dbpass: passwd
```

Finalement...

```bash
$ wp --info
OS:	Linux 4.14.61-ovh-vps-grsec-zfs-classid #1 SMP Tue Aug 7 02:27:59 CEST 2018 x86_64
Shell:	/bin/ovh_ssh
PHP binary:	/usr/local/php7.1/bin/php
PHP version:	7.1.17
php.ini used:	/usr/local/php7.1/etc/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/home/myuser
WP-CLI packages dir:	/homez.65/myuser/.wp-cli/packages/
WP-CLI global config:	/homez.65/myuser/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version:	2.0.1
```



Une fois wp-cli installé, <u>on ne peut pas y rajouter de package</u>. La connection est toujours refusée.

```bash
~/www/wordpress $ wp package install wp-cli/doctor-command

Error: The "https://wp-cli.org/package-index/packages.json" file could not be downloaded: failed to open stream: Connection refused

~/www/wordpress $ wp package install git@github.com:wp-cli/doctor-command.git

Error: Failed to get url 'https://raw.githubusercontent.com/wp-cli/doctor-command/master/composer.json': cURL error 7: Failed to connect to raw.githubusercontent.com port 443: Connection refused.
```

Même juste afficher la liste des packages pose problème:

```bash
~/www/wordpress $ wp package list
Warning: The "https://wp-cli.org/package-index/packages.json" file could not be downloaded: failed to open stream: Connection refused
+--------------------------+------------------+------------+--------+----------------+
| name                     | authors          | version    | update | update_version |
+--------------------------+------------------+------------+--------+----------------+
| trepmal/wp-revisions-cli |                  | dev-master | error  | error          |
| wp-cli/doctor-command    | Daniel Bachhuber | dev-master | none   |                |
+--------------------------+------------------+------------+--------+----------------+
```





L'astuce est de passer par un script php et la fonction shell_exec:

```php
$install = shell_exec('wp package install git@github.com:wp-cli/doctor-command.git 2>&1');
```

Pensez à bien renseigner les variables d'environnement au début du script.



**install_wp_package.php**

```php
<?php

putenv('PATH=/usr/local/php7.1/bin:/homez.65/funnymac/.wp-cli:/homez.65/funnymac/opt/bin:/usr/local/bin:/usr/bin:/bin');
putenv('COMPOSER_HOME=/homez.65/funnymac/opt/bin/composer');
putenv('WP_CLI_PACKAGES_DIR=/homez.65/funnymac/.wp-cli/packages');

$composer = shell_exec('which composer 2>&1');
echo "$ which composer: <br /><code>$composer</code>";

echo "<br /><br />";

$composer_v = shell_exec('composer -V 2>&1');
echo "$ composer -V: <br /><code>$composer_v</code>";

echo "<br /><br />";

$wp = shell_exec('which wp 2>&1');
echo "$ which wp: <br /><code>$wp</code>";

echo "<br /><br />";

$wp_info = shell_exec('wp --info 2>&1');
echo "$ wp --info: <br /><pre>$wp_info</pre>";

$pwd = shell_exec('pwd');
echo "$ pwd: <br /><code>$pwd</code>";

echo "<br /><br />";

#$output2 = shell_exec('cd wordpress;wp package path');
$output2 = shell_exec('wp package path');
echo "$ wp package path: <br /><code>$output2</code>";

echo "<br /><br />";

$package_list = shell_exec('wp package list');
echo "$ wp package list: <br /><pre>$package_list</pre>";

#$install = shell_exec('wp package install git@github.com:wp-cli/doctor-command.git 2>&1');
#$install = shell_exec('wp package install trepmal/wp-revisions-cli 2>&1');
echo "<pre>$install</pre>";

echo "Update all package $ wp package update"

#$themes = shell_exec('wp theme list --path=wordpress 2>&1');
#echo "$ wp theme list: <br /><pre>$themes</pre>";


?>
```

