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 $installer->setUpCache($configCache, BasePath::create($a->getBasePath(), $_SERVER));
84 $this->out(" Complete!\n\n");
87 $this->out("Checking environment...\n");
89 $installer->resetChecks();
91 if (!$this->runBasicChecks($installer, $configCache)) {
92 $errorMessage = $this->extractErrors($installer->getChecks());
93 throw new RuntimeException($errorMessage);
96 $this->out(" Complete!\n\n");
98 // if a config file is set,
99 $config_file = $this->getOption(['f', 'file']);
101 if (!empty($config_file)) {
102 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
104 $this->out("Copying config file...\n");
105 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
106 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");
110 //reload the config cache
111 $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
112 $loader->setupCache($configCache);
115 // Creating config file
116 $this->out("Creating config file...\n");
118 $save_db = $this->getOption(['s', 'savedb'], false);
120 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
121 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
122 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
123 $configCache->set('database', 'database',
124 $this->getOption(['d', 'dbdata'],
125 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
126 $configCache->set('database', 'username',
127 $this->getOption(['U', 'dbuser'],
128 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
129 $configCache->set('database', 'password',
130 $this->getOption(['P', 'dbpass'],
131 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
133 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
134 if (!empty($php_path)) {
135 $configCache->set('config', 'php_path', $php_path);
137 $configCache->set('config', 'php_path', $installer->getPHPPath());
140 $configCache->set('config', 'admin_email',
141 $this->getOption(['A', 'admin'],
142 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
143 $configCache->set('system', 'default_timezone',
144 $this->getOption(['T', 'tz'],
145 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
146 $configCache->set('system', 'language',
147 $this->getOption(['L', 'lang'],
148 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
150 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
151 if (!empty($basepath)) {
152 $configCache->set('system', 'basepath', $basepath);
155 $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
158 $this->out('The Friendica URL has to be set during CLI installation.');
161 $baseUrl = new BaseURL($a->getConfig(), []);
162 $baseUrl->saveByURL($url);
165 $installer->createConfig($configCache);
168 $this->out(" Complete!\n\n");
170 // Check database connection
171 $this->out("Checking database...\n");
173 $installer->resetChecks();
175 if (!$installer->checkDB($configCache, $a->getProfiler())) {
176 $errorMessage = $this->extractErrors($installer->getChecks());
177 throw new RuntimeException($errorMessage);
180 $this->out(" Complete!\n\n");
183 $this->out("Inserting data into database...\n");
185 $installer->resetChecks();
187 if (!$installer->installDatabase($a->getBasePath())) {
188 $errorMessage = $this->extractErrors($installer->getChecks());
189 throw new RuntimeException($errorMessage);
192 $this->out(" Complete!\n\n");
195 $this->out("Installing theme\n");
196 if (!empty(Config::get('system', 'theme'))) {
197 Theme::install(Config::get('system', 'theme'));
198 $this->out(" Complete\n\n");
200 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
203 $this->out("\nInstallation is finished\n");
209 * @param Installer $installer The Installer instance
210 * @param Config\Cache\ConfigCache $configCache The config cache
212 * @return bool true if checks were successfully, otherwise false
213 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
215 private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
219 $installer->resetChecks();
220 if (!$installer->checkFunctions()) {
223 if (!$installer->checkImagick()) {
226 if (!$installer->checkLocalIni()) {
229 if (!$installer->checkSmarty3()) {
232 if (!$installer->checkKeys()) {
236 $php_path = $configCache->get('config', 'php_path');
238 if (!$installer->checkPHP($php_path, true)) {
242 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
248 * @param array $results
251 private function extractErrors($results)
254 $allChecksRequired = $this->getOption('a') !== null;
256 foreach ($results as $result) {
257 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
258 $errorMessage .= "--------\n";
259 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
263 return $errorMessage;