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\Util\Config\ConfigFileLoader;
13 class AutomaticInstallation extends Console
15 protected function getHelp()
18 Installation - Install Friendica automatically
20 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
23 Installs Friendica with data based on the local.config.php file or environment variables
26 Not checking .htaccess/URL-Rewrite during CLI installation.
29 -h|--help|-? Show help information
30 -v Show more debug information.
31 -a All setup checks are required (except .htaccess)
32 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
33 -s|--savedb Save the DB credentials to the file (if environment variables is used)
34 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
35 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
36 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
37 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
38 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
39 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
40 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
41 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
42 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
43 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
46 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
47 MYSQL_PORT The port of the mysql/mariadb database
48 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
49 MYSQL_PASSWORD The password of the mysql/mariadb database login
50 MYSQL_DATABASE The name of the mysql/mariadb database
51 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
52 FRIENDICA_PHP_PATH The path of the PHP binary
53 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
54 FRIENDICA_TZ The timezone of Friendica
55 FRIENDICA_LANG The langauge of Friendica
58 bin/console autoinstall -f 'input.config.php
59 Installs Friendica with the prepared 'input.config.php' file
61 bin/console autoinstall --savedb
62 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
64 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
65 Installs Friendica with a local mysql database with credentials
69 protected function doExecute()
72 $this->out("Initializing setup...\n");
74 $a = BaseObject::getApp();
76 $installer = new Installer();
78 $configCache = $a->getConfigCache();
80 $this->out(" Complete!\n\n");
83 $this->out("Checking environment...\n");
85 $installer->resetChecks();
87 if (!$this->runBasicChecks($installer, $configCache)) {
88 $errorMessage = $this->extractErrors($installer->getChecks());
89 throw new RuntimeException($errorMessage);
92 $this->out(" Complete!\n\n");
94 // if a config file is set,
95 $config_file = $this->getOption(['f', 'file']);
97 if (!empty($config_file)) {
98 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
100 $this->out("Copying config file...\n");
101 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
102 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");
106 //reload the config cache
107 $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
108 $loader->setupCache($configCache);
111 // Creating config file
112 $this->out("Creating config file...\n");
114 $save_db = $this->getOption(['s', 'savedb'], false);
116 //$db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST') ? getenv('MYSQL_HOST') : Installer::DEFAULT_HOST) : '');
117 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
118 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
119 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
120 $configCache->set('database', 'database',
121 $this->getOption(['d', 'dbdata'],
122 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
123 $configCache->set('database', 'username',
124 $this->getOption(['U', 'dbuser'],
125 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
126 $configCache->set('database', 'password',
127 $this->getOption(['P', 'dbpass'],
128 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
129 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
130 if (!empty($php_path)) {
131 $configCache->set('config', 'php_path', $php_path);
133 $configCache->set('config', 'admin_email',
134 $this->getOption(['A', 'admin'],
135 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
136 $configCache->set('system', 'default_timezone',
137 $this->getOption(['T', 'tz'],
138 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
139 $configCache->set('system', 'language',
140 $this->getOption(['L', 'lang'],
141 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
144 if (empty($php_path)) {
145 $configCache->set('config', 'php_path', $installer->getPHPPath());
148 $installer->createConfig(
155 $this->out(" Complete!\n\n");
157 // Check database connection
158 $this->out("Checking database...\n");
160 $installer->resetChecks();
162 if (!$installer->checkDB($a->getBasePath(), $configCache, $a->getProfiler())) {
163 $errorMessage = $this->extractErrors($installer->getChecks());
164 throw new RuntimeException($errorMessage);
167 $this->out(" Complete!\n\n");
170 $this->out("Inserting data into database...\n");
172 $installer->resetChecks();
174 if (!$installer->installDatabase($a->getBasePath())) {
175 $errorMessage = $this->extractErrors($installer->getChecks());
176 throw new RuntimeException($errorMessage);
179 $this->out(" Complete!\n\n");
182 $this->out("Installing theme\n");
183 if (!empty(Config::get('system', 'theme'))) {
184 Theme::install(Config::get('system', 'theme'));
185 $this->out(" Complete\n\n");
187 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
190 $this->out("\nInstallation is finished\n");
196 * @param Installer $installer The Installer instance
197 * @param Config\Cache\IConfigCache $configCache The config cache
199 * @return bool true if checks were successfully, otherwise false
200 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
202 private function runBasicChecks(Installer $installer, Config\Cache\IConfigCache $configCache)
206 $installer->resetChecks();
207 if (!$installer->checkFunctions()) {
210 if (!$installer->checkImagick()) {
213 if (!$installer->checkLocalIni()) {
216 if (!$installer->checkSmarty3()) {
219 if (!$installer->checkKeys()) {
224 if ($configCache->has('config', 'php_path')) {
225 $php_path = $configCache->get('config', 'php_path');
228 if (!$installer->checkPHP($php_path, true)) {
232 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
238 * @param array $results
241 private function extractErrors($results)
244 $allChecksRequired = $this->getOption('a') !== null;
246 foreach ($results as $result) {
247 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
248 $errorMessage .= "--------\n";
249 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
253 return $errorMessage;