3 namespace Friendica\Core\Console;
5 use Asika\SimpleConsole\Console;
7 use Friendica\BaseObject;
8 use Friendica\Core\Config;
9 use Friendica\Core\Install;
10 use Friendica\Core\Theme;
11 use Friendica\Database\DBA;
14 require_once 'mod/install.php';
15 require_once 'include/dba.php';
17 class AutomaticInstallation extends Console
19 protected function getHelp()
22 Installation - Install Friendica automatically
24 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
27 Installs Friendica with data based on the local.ini.php file or environment variables
30 Not checking .htaccess/URL-Rewrite during CLI installation.
33 -h|--help|-? Show help information
34 -v Show more debug information.
35 -a All setup checks are required (except .htaccess)
36 -f|--file <config> prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
37 -s|--savedb Save the DB credentials to the file (if environment variables is used)
38 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
39 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
40 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
41 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
42 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
43 -b|--phppath <path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
44 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
45 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
46 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
49 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
50 MYSQL_PORT The port of the mysql/mariadb database
51 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
52 MYSQL_PASSWORD The password of the mysql/mariadb database login
53 MYSQL_DATABASE The name of the mysql/mariadb database
54 FRIENDICA_PHP_PATH The path of the PHP binary
55 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
56 FRIENDICA_TZ The timezone of Friendica
57 FRIENDICA_LANG The langauge of Friendica
60 bin/console autoinstall -f 'input.ini.php
61 Installs Friendica with the prepared 'input.ini.php' file
63 bin/console autoinstall --savedb
64 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
66 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
67 Installs Friendica with a local mysql database with credentials
71 protected function doExecute()
74 $this->out("Initializing setup...\n");
76 $a = BaseObject::getApp();
78 // if a config file is set,
79 $config_file = $this->getOption(['f', 'file']);
81 if (!empty($config_file)) {
82 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.ini.php') {
84 $this->out("Copying config file...\n");
85 if (!copy($a->basepath . DIRECTORY_SEPARATOR . $config_file, $a->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
86 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '$a->basepath" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.ini.php' manually.\n");
90 $db_host = $a->getConfigValue('database', 'hostname');
91 $db_user = $a->getConfigValue('database', 'username');
92 $db_pass = $a->getConfigValue('database', 'password');
93 $db_data = $a->getConfigValue('database', 'database');
95 // Creating config file
96 $this->out("Creating config file...\n");
98 $save_db = $this->getOption(['s', 'savedb'], false);
100 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
101 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
102 $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
103 $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
104 $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
105 $php_path = $this->getOption(['b', 'phppath'], (!empty('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : '');
106 $admin_mail = $this->getOption(['A', 'admin'], (!empty('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : '');
107 $tz = $this->getOption(['T', 'tz'], (!empty('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : '');
108 $lang = $this->getOption(['L', 'lang'], (!empty('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : '');
110 Install::createConfig(
112 ((!empty($db_port)) ? $db_host . ':' . $db_port : $db_host),
123 $this->out(" Complete!\n\n");
126 $this->out("Checking basic setup...\n");
129 $checkResults['basic'] = $this->runBasicChecks($a);
130 $errorMessage = $this->extractErrors($checkResults['basic']);
132 if ($errorMessage !== '') {
133 throw new RuntimeException($errorMessage);
136 $this->out(" Complete!\n\n");
138 // Check database connection
139 $this->out("Checking database...\n");
141 $checkResults['db'] = array();
142 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
143 $errorMessage = $this->extractErrors($checkResults['db']);
145 if ($errorMessage !== '') {
146 throw new RuntimeException($errorMessage);
149 $this->out(" Complete!\n\n");
152 $this->out("Inserting data into database...\n");
154 $checkResults['data'] = Install::installDatabaseStructure();
156 if ($checkResults['data'] !== '') {
157 throw new RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
160 $this->out(" Complete!\n\n");
163 $this->out("Installing theme\n");
164 if (!empty(Config::get('system', 'theme'))) {
165 Theme::install(Config::get('system', 'theme'));
166 $this->out(" Complete\n\n");
168 $this->out(" Theme setting is empty. Please check the file 'config/local.ini.php'\n\n");
171 $this->out("\nInstallation is finished\n");
180 private function runBasicChecks($app)
184 Install::checkFunctions($checks);
185 Install::checkImagick($checks);
186 Install::checkLocalIni($checks);
187 Install::checkSmarty3($checks);
188 Install::checkKeys($checks);
190 if (!empty(Config::get('config', 'php_path'))) {
191 Install::checkPHP(Config::get('config', 'php_path'), $checks);
193 throw new RuntimeException(" ERROR: The php_path is not set in the config.\n");
196 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
208 private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
211 'title' => 'MySQL Connection',
218 if (!DBA::connect($db_host, $db_user, $db_pass, $db_data)) {
219 $result['status'] = false;
220 $result['help'] = 'Failed, please check your MySQL settings and credentials.';
227 * @param array $results
230 private function extractErrors($results)
233 $allChecksRequired = $this->getOption('a') !== null;
235 foreach ($results as $result) {
236 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
237 $errorMessage .= "--------\n";
238 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
242 return $errorMessage;