3 namespace Friendica\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\Util\BasePath;
11 use Friendica\Util\BaseURL;
12 use Friendica\Util\ConfigFileLoader;
15 class AutomaticInstallation extends Console
17 protected function getHelp()
20 Installation - Install Friendica automatically
22 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
25 Installs Friendica with data based on the local.config.php file or environment variables
28 Not checking .htaccess/URL-Rewrite during CLI installation.
31 -h|--help|-? Show help information
32 -v Show more debug information.
33 -a All setup checks are required (except .htaccess)
34 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
35 -s|--savedb Save the DB credentials to the file (if environment variables is used)
36 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
37 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
38 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
39 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
40 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
41 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
42 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
43 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
44 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
45 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
48 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
49 MYSQL_PORT The port of the mysql/mariadb database
50 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
51 MYSQL_PASSWORD The password of the mysql/mariadb database login
52 MYSQL_DATABASE The name of the mysql/mariadb database
53 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
54 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
55 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
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 $configCache = $a->getConfigCache();
82 $basepath = new BasePath($a->getBasePath());
83 $installer->setUpCache($configCache, $basepath->getPath());
85 $this->out(" Complete!\n\n");
88 $this->out("Checking environment...\n");
90 $installer->resetChecks();
92 if (!$this->runBasicChecks($installer, $configCache)) {
93 $errorMessage = $this->extractErrors($installer->getChecks());
94 throw new RuntimeException($errorMessage);
97 $this->out(" Complete!\n\n");
99 // if a config file is set,
100 $config_file = $this->getOption(['f', 'file']);
102 if (!empty($config_file)) {
103 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
105 $this->out("Copying config file...\n");
106 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
107 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");
111 //reload the config cache
112 $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
113 $loader->setupCache($configCache);
116 // Creating config file
117 $this->out("Creating config file...\n");
119 $save_db = $this->getOption(['s', 'savedb'], false);
121 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
122 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
123 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
124 $configCache->set('database', 'database',
125 $this->getOption(['d', 'dbdata'],
126 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
127 $configCache->set('database', 'username',
128 $this->getOption(['U', 'dbuser'],
129 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
130 $configCache->set('database', 'password',
131 $this->getOption(['P', 'dbpass'],
132 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
134 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
135 if (!empty($php_path)) {
136 $configCache->set('config', 'php_path', $php_path);
138 $configCache->set('config', 'php_path', $installer->getPHPPath());
141 $configCache->set('config', 'admin_email',
142 $this->getOption(['A', 'admin'],
143 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
144 $configCache->set('system', 'default_timezone',
145 $this->getOption(['T', 'tz'],
146 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
147 $configCache->set('system', 'language',
148 $this->getOption(['L', 'lang'],
149 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
151 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
152 if (!empty($basepath)) {
153 $configCache->set('system', 'basepath', $basepath);
156 $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
159 $this->out('The Friendica URL has to be set during CLI installation.');
162 $baseUrl = new BaseURL($a->getConfig(), []);
163 $baseUrl->saveByURL($url);
166 $installer->createConfig($configCache);
169 $this->out(" Complete!\n\n");
171 // Check database connection
172 $this->out("Checking database...\n");
174 $installer->resetChecks();
176 if (!$installer->checkDB($configCache, $a->getProfiler())) {
177 $errorMessage = $this->extractErrors($installer->getChecks());
178 throw new RuntimeException($errorMessage);
181 $this->out(" Complete!\n\n");
184 $this->out("Inserting data into database...\n");
186 $installer->resetChecks();
188 if (!$installer->installDatabase($a->getBasePath())) {
189 $errorMessage = $this->extractErrors($installer->getChecks());
190 throw new RuntimeException($errorMessage);
193 $this->out(" Complete!\n\n");
196 $this->out("Installing theme\n");
197 if (!empty(Config::get('system', 'theme'))) {
198 Theme::install(Config::get('system', 'theme'));
199 $this->out(" Complete\n\n");
201 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
204 $this->out("\nInstallation is finished\n");
210 * @param Installer $installer The Installer instance
211 * @param Config\Cache\ConfigCache $configCache The config cache
213 * @return bool true if checks were successfully, otherwise false
214 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
216 private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
220 $installer->resetChecks();
221 if (!$installer->checkFunctions()) {
224 if (!$installer->checkImagick()) {
227 if (!$installer->checkLocalIni()) {
230 if (!$installer->checkSmarty3()) {
233 if (!$installer->checkKeys()) {
237 $php_path = $configCache->get('config', 'php_path');
239 if (!$installer->checkPHP($php_path, true)) {
243 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
249 * @param array $results
252 private function extractErrors($results)
255 $allChecksRequired = $this->getOption('a') !== null;
257 foreach ($results as $result) {
258 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
259 $errorMessage .= "--------\n";
260 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
264 return $errorMessage;