3 namespace Friendica\Core\Console;
5 use Asika\SimpleConsole\Console;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Core\Installer;
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.config.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.config.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.config.php
62 Installs Friendica with the prepared 'input.config.php' file
64 bin/console autoinstall --savedb
65 Installs Friendica with environment variables and saves them to the 'config/local.config.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 $installer = new Installer();
81 $this->out(" Complete!\n\n");
84 $this->out("Checking environment...\n");
86 $installer->resetChecks();
88 if (!$this->runBasicChecks($installer)) {
89 $errorMessage = $this->extractErrors($installer->getChecks());
90 throw new RuntimeException($errorMessage);
93 $this->out(" Complete!\n\n");
95 // if a config file is set,
96 $config_file = $this->getOption(['f', 'file']);
98 if (!empty($config_file)) {
99 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
101 $this->out("Copying config file...\n");
102 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
103 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
107 $db_host = $a->getConfigValue('database', 'hostname');
108 $db_user = $a->getConfigValue('database', 'username');
109 $db_pass = $a->getConfigValue('database', 'password');
110 $db_data = $a->getConfigValue('database', 'database');
112 // Creating config file
113 $this->out("Creating config file...\n");
115 $save_db = $this->getOption(['s', 'savedb'], false);
117 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
118 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
119 $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
120 $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
121 $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
122 $url_path = $this->getOption(['u', 'urlpath'], !empty('FRIENDICA_URL_PATH') ? getenv('FRIENDICA_URL_PATH') : null);
123 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
124 $admin_mail = $this->getOption(['A', 'admin'], !empty('FRIENDICA_ADMIN_MAIL') ? getenv('FRIENDICA_ADMIN_MAIL') : '');
125 $tz = $this->getOption(['T', 'tz'], !empty('FRIENDICA_TZ') ? getenv('FRIENDICA_TZ') : '');
126 $lang = $this->getOption(['L', 'lang'], !empty('FRIENDICA_LANG') ? getenv('FRIENDICA_LANG') : '');
128 if (empty($php_path)) {
129 $php_path = $installer->getPHPPath();
132 $installer->createConfig(
135 (!empty($db_port) ? $db_host . ':' . $db_port : $db_host),
146 $this->out(" Complete!\n\n");
148 // Check database connection
149 $this->out("Checking database...\n");
151 $installer->resetChecks();
153 if (!$installer->checkDB($db_host, $db_user, $db_pass, $db_data)) {
154 $errorMessage = $this->extractErrors($installer->getChecks());
155 throw new RuntimeException($errorMessage);
158 $this->out(" Complete!\n\n");
161 $this->out("Inserting data into database...\n");
163 $installer->resetChecks();
165 if (!$installer->installDatabase()) {
166 $errorMessage = $this->extractErrors($installer->getChecks());
167 throw new RuntimeException($errorMessage);
170 $this->out(" Complete!\n\n");
173 $this->out("Installing theme\n");
174 if (!empty(Config::get('system', 'theme'))) {
175 Theme::install(Config::get('system', 'theme'));
176 $this->out(" Complete\n\n");
178 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
181 $this->out("\nInstallation is finished\n");
187 * @param Installer $installer the Installer instance
189 * @return bool true if checks were successfully, otherwise false
191 private function runBasicChecks(Installer $installer)
195 $installer->resetChecks();
196 if (!$installer->checkFunctions()) {
199 if (!$installer->checkImagick()) {
202 if (!$installer->checkLocalIni()) {
205 if (!$installer->checkSmarty3()) {
208 if (!$installer->checkKeys()) {
213 if (!empty(Config::get('config', 'php_path'))) {
214 $php_path = Config::get('config', 'php_path');
217 if (!$installer->checkPHP($php_path, true)) {
221 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
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;