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 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
44 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
45 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
46 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
47 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
50 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
51 MYSQL_PORT The port of the mysql/mariadb database
52 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
53 MYSQL_PASSWORD The password of the mysql/mariadb database login
54 MYSQL_DATABASE The name of the mysql/mariadb database
55 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
56 FRIENDICA_PHP_PATH The path of the PHP binary
57 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
58 FRIENDICA_TZ The timezone of Friendica
59 FRIENDICA_LANG The langauge of Friendica
62 bin/console autoinstall -f 'input.ini.php
63 Installs Friendica with the prepared 'input.ini.php' file
65 bin/console autoinstall --savedb
66 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
68 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
69 Installs Friendica with a local mysql database with credentials
73 protected function doExecute()
76 $this->out("Initializing setup...\n");
78 $a = BaseObject::getApp();
80 // if a config file is set,
81 $config_file = $this->getOption(['f', 'file']);
83 if (!empty($config_file)) {
84 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.ini.php') {
86 $this->out("Copying config file...\n");
87 if (!copy($a->basepath . DIRECTORY_SEPARATOR . $config_file, $a->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
88 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");
92 $db_host = $a->getConfigValue('database', 'hostname');
93 $db_user = $a->getConfigValue('database', 'username');
94 $db_pass = $a->getConfigValue('database', 'password');
95 $db_data = $a->getConfigValue('database', 'database');
97 // Creating config file
98 $this->out("Creating config file...\n");
100 $save_db = $this->getOption(['s', 'savedb'], false);
102 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
103 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
104 $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
105 $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
106 $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
107 $url_path = $this->getOption(['u', 'urlpath'], (!empty('FRIENDICA_URL_PATH')) ? getenv('FRIENDICA_URL_PATH') : null);
108 $php_path = $this->getOption(['b', 'phppath'], (!empty('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : '');
109 $admin_mail = $this->getOption(['A', 'admin'], (!empty('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : '');
110 $tz = $this->getOption(['T', 'tz'], (!empty('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : '');
111 $lang = $this->getOption(['L', 'lang'], (!empty('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : '');
113 Install::createConfig(
115 ((!empty($db_port)) ? $db_host . ':' . $db_port : $db_host),
126 $this->out(" Complete!\n\n");
129 $this->out("Checking basic setup...\n");
132 $checkResults['basic'] = $this->runBasicChecks($a);
133 $errorMessage = $this->extractErrors($checkResults['basic']);
135 if ($errorMessage !== '') {
136 throw new RuntimeException($errorMessage);
139 $this->out(" Complete!\n\n");
141 // Check database connection
142 $this->out("Checking database...\n");
144 $checkResults['db'] = array();
145 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
146 $errorMessage = $this->extractErrors($checkResults['db']);
148 if ($errorMessage !== '') {
149 throw new RuntimeException($errorMessage);
152 $this->out(" Complete!\n\n");
155 $this->out("Inserting data into database...\n");
157 $checkResults['data'] = Install::installDatabaseStructure();
159 if ($checkResults['data'] !== '') {
160 throw new RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
163 $this->out(" Complete!\n\n");
166 $this->out("Installing theme\n");
167 if (!empty(Config::get('system', 'theme'))) {
168 Theme::install(Config::get('system', 'theme'));
169 $this->out(" Complete\n\n");
171 $this->out(" Theme setting is empty. Please check the file 'config/local.ini.php'\n\n");
174 $this->out("\nInstallation is finished\n");
183 private function runBasicChecks($app)
187 Install::checkFunctions($checks);
188 Install::checkImagick($checks);
189 Install::checkLocalIni($checks);
190 Install::checkSmarty3($checks);
191 Install::checkKeys($checks);
193 if (!empty(Config::get('config', 'php_path'))) {
194 Install::checkPHP(Config::get('config', 'php_path'), $checks);
196 throw new RuntimeException(" ERROR: The php_path is not set in the config.\n");
199 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
211 private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
214 'title' => 'MySQL Connection',
221 if (!DBA::connect($db_host, $db_user, $db_pass, $db_data)) {
222 $result['status'] = false;
223 $result['help'] = 'Failed, please check your MySQL settings and credentials.';
230 * @param array $results
233 private function extractErrors($results)
236 $allChecksRequired = $this->getOption('a') !== null;
238 foreach ($results as $result) {
239 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
240 $errorMessage .= "--------\n";
241 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
245 return $errorMessage;