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 class AutomaticInstallation extends Console
16 protected function getHelp()
19 Installation - Install Friendica automatically
21 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
24 Installs Friendica with data based on the local.config.php file or environment variables
27 Not checking .htaccess/URL-Rewrite during CLI installation.
30 -h|--help|-? Show help information
31 -v Show more debug information.
32 -a All setup checks are required (except .htaccess)
33 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
34 -s|--savedb Save the DB credentials to the file (if environment variables is used)
35 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
36 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
37 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
38 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
39 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
40 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
41 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
42 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
43 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
44 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
47 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
48 MYSQL_PORT The port of the mysql/mariadb database
49 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
50 MYSQL_PASSWORD The password of the mysql/mariadb database login
51 MYSQL_DATABASE The name of the mysql/mariadb database
52 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
53 FRIENDICA_PHP_PATH The path of the PHP binary
54 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
55 FRIENDICA_TZ The timezone of Friendica
56 FRIENDICA_LANG The langauge of Friendica
59 bin/console autoinstall -f 'input.config.php
60 Installs Friendica with the prepared 'input.config.php' file
62 bin/console autoinstall --savedb
63 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
65 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
66 Installs Friendica with a local mysql database with credentials
70 protected function doExecute()
73 $this->out("Initializing setup...\n");
75 $a = BaseObject::getApp();
77 $installer = new Installer();
79 $this->out(" Complete!\n\n");
82 $this->out("Checking environment...\n");
84 $installer->resetChecks();
86 if (!$this->runBasicChecks($installer)) {
87 $errorMessage = $this->extractErrors($installer->getChecks());
88 throw new RuntimeException($errorMessage);
91 $this->out(" Complete!\n\n");
93 // if a config file is set,
94 $config_file = $this->getOption(['f', 'file']);
96 if (!empty($config_file)) {
97 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
99 $this->out("Copying config file...\n");
100 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
101 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");
105 $db_host = $a->getConfigValue('database', 'hostname');
106 $db_user = $a->getConfigValue('database', 'username');
107 $db_pass = $a->getConfigValue('database', 'password');
108 $db_data = $a->getConfigValue('database', 'database');
110 // Creating config file
111 $this->out("Creating config file...\n");
113 $save_db = $this->getOption(['s', 'savedb'], false);
115 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
116 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
117 $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
118 $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
119 $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
120 $url_path = $this->getOption(['u', 'urlpath'], !empty('FRIENDICA_URL_PATH') ? getenv('FRIENDICA_URL_PATH') : null);
121 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
122 $admin_mail = $this->getOption(['A', 'admin'], !empty('FRIENDICA_ADMIN_MAIL') ? getenv('FRIENDICA_ADMIN_MAIL') : '');
123 $tz = $this->getOption(['T', 'tz'], !empty('FRIENDICA_TZ') ? getenv('FRIENDICA_TZ') : '');
124 $lang = $this->getOption(['L', 'lang'], !empty('FRIENDICA_LANG') ? getenv('FRIENDICA_LANG') : '');
126 if (empty($php_path)) {
127 $php_path = $installer->getPHPPath();
130 $installer->createConfig(
133 (!empty($db_port) ? $db_host . ':' . $db_port : $db_host),
144 $this->out(" Complete!\n\n");
146 // Check database connection
147 $this->out("Checking database...\n");
149 $installer->resetChecks();
151 if (!$installer->checkDB($db_host, $db_user, $db_pass, $db_data)) {
152 $errorMessage = $this->extractErrors($installer->getChecks());
153 throw new RuntimeException($errorMessage);
156 $this->out(" Complete!\n\n");
159 $this->out("Inserting data into database...\n");
161 $installer->resetChecks();
163 if (!$installer->installDatabase()) {
164 $errorMessage = $this->extractErrors($installer->getChecks());
165 throw new RuntimeException($errorMessage);
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.config.php'\n\n");
179 $this->out("\nInstallation is finished\n");
185 * @param Installer $installer the Installer instance
187 * @return bool true if checks were successfully, otherwise false
188 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
190 private function runBasicChecks(Installer $installer)
194 $installer->resetChecks();
195 if (!$installer->checkFunctions()) {
198 if (!$installer->checkImagick()) {
201 if (!$installer->checkLocalIni()) {
204 if (!$installer->checkSmarty3()) {
207 if (!$installer->checkKeys()) {
212 if (!empty(Config::get('config', 'php_path'))) {
213 $php_path = Config::get('config', 'php_path');
216 if (!$installer->checkPHP($php_path, true)) {
220 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
226 * @param array $results
229 private function extractErrors($results)
232 $allChecksRequired = $this->getOption('a') !== null;
234 foreach ($results as $result) {
235 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
236 $errorMessage .= "--------\n";
237 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
241 return $errorMessage;