3 namespace Friendica\Core\Console;
5 use Asika\SimpleConsole\Console;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Core\Install;
9 use Friendica\Core\Theme;
10 use Friendica\Database\DBA;
11 use Friendica\Database\DBStructure;
14 require_once 'include/dba.php';
16 class AutomaticInstallation extends Console
18 protected function getHelp()
21 Installation - Install Friendica automatically
23 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
26 Installs Friendica with data based on the local.ini.php file or environment variables
29 Not checking .htaccess/URL-Rewrite during CLI installation.
32 -h|--help|-? Show help information
33 -v Show more debug information.
34 -a All setup checks are required (except .htaccess)
35 -f|--file <config> prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
36 -s|--savedb Save the DB credentials to the file (if environment variables is used)
37 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
38 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
39 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
40 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
41 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
42 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
43 -b|--phppath <php_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_URL_PATH The URL path of Friendica (f.e. '/friendica')
55 FRIENDICA_PHP_PATH The path of the PHP binary
56 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
57 FRIENDICA_TZ The timezone of Friendica
58 FRIENDICA_LANG The langauge of Friendica
61 bin/console autoinstall -f 'input.ini.php
62 Installs Friendica with the prepared 'input.ini.php' file
64 bin/console autoinstall --savedb
65 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
67 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
68 Installs Friendica with a local mysql database with credentials
72 protected function doExecute()
75 $this->out("Initializing setup...\n");
77 $a = BaseObject::getApp();
79 $install = new Install();
81 // if a config file is set,
82 $config_file = $this->getOption(['f', 'file']);
84 if (!empty($config_file)) {
85 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.ini.php') {
87 $this->out("Copying config file...\n");
88 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
89 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.ini.php' manually.\n");
93 $db_host = $a->getConfigValue('database', 'hostname');
94 $db_user = $a->getConfigValue('database', 'username');
95 $db_pass = $a->getConfigValue('database', 'password');
96 $db_data = $a->getConfigValue('database', 'database');
98 // Creating config file
99 $this->out("Creating config file...\n");
101 $save_db = $this->getOption(['s', 'savedb'], false);
103 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
104 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
105 $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
106 $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
107 $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
108 $url_path = $this->getOption(['u', 'urlpath'], (!empty('FRIENDICA_URL_PATH')) ? getenv('FRIENDICA_URL_PATH') : null);
109 $php_path = $this->getOption(['b', 'phppath'], (!empty('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : null);
110 $admin_mail = $this->getOption(['A', 'admin'], (!empty('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : '');
111 $tz = $this->getOption(['T', 'tz'], (!empty('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : '');
112 $lang = $this->getOption(['L', 'lang'], (!empty('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : '');
114 $install->createConfig(
117 ((!empty($db_port)) ? $db_host . ':' . $db_port : $db_host),
128 $this->out(" Complete!\n\n");
131 $this->out("Checking basic setup...\n");
135 $this->runBasicChecks($install);
137 $checkResults['basic'] = $install->getChecks();
138 $errorMessage = $this->extractErrors($checkResults['basic']);
140 if ($errorMessage !== '') {
141 throw new RuntimeException($errorMessage);
144 $this->out(" Complete!\n\n");
146 // Check database connection
147 $this->out("Checking database...\n");
149 $checkResults['db'] = array();
150 $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
151 $errorMessage = $this->extractErrors($checkResults['db']);
153 if ($errorMessage !== '') {
154 throw new RuntimeException($errorMessage);
157 $this->out(" Complete!\n\n");
160 $this->out("Inserting data into database...\n");
162 $checkResults['data'] = DBStructure::update(false, true, true);
164 if ($checkResults['data'] !== '') {
165 throw new RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
168 $this->out(" Complete!\n\n");
171 $this->out("Installing theme\n");
172 if (!empty(Config::get('system', 'theme'))) {
173 Theme::install(Config::get('system', 'theme'));
174 $this->out(" Complete\n\n");
176 $this->out(" Theme setting is empty. Please check the file 'config/local.ini.php'\n\n");
179 $this->out("\nInstallation is finished\n");
185 * @param Install $install the Installer instance
187 private function runBasicChecks(Install $install)
189 $install->resetChecks();
190 $install->checkFunctions();
191 $install->checkImagick();
192 $install->checkLocalIni();
193 $install->checkSmarty3();
194 $install->checkKeys();
196 if (!empty(Config::get('config', 'php_path'))) {
197 if (!$install->checkPHP(Config::get('config', 'php_path'), true)) {
198 throw new RuntimeException(" ERROR: The php_path is not valid in the config.\n");
201 throw new RuntimeException(" ERROR: The php_path is not set in the config.\n");
204 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
215 private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
218 'title' => 'MySQL Connection',
225 if (!DBA::connect($db_host, $db_user, $db_pass, $db_data)) {
226 $result['status'] = false;
227 $result['help'] = 'Failed, please check your MySQL settings and credentials.';
234 * @param array $results
237 private function extractErrors($results)
240 $allChecksRequired = $this->getOption('a') !== null;
242 foreach ($results as $result) {
243 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
244 $errorMessage .= "--------\n";
245 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
249 return $errorMessage;