3 namespace Friendica\Console;
5 use Asika\SimpleConsole\Console;
7 use Friendica\App\BaseURL;
8 use Friendica\Core\Config;
9 use Friendica\Core\Installer;
10 use Friendica\Core\Theme;
11 use Friendica\Database\Database;
12 use Friendica\Util\BasePath;
13 use Friendica\Util\ConfigFileLoader;
16 class AutomaticInstallation extends Console
23 * @var Config\Cache\ConfigCache
28 * @var Config\Configuration
37 protected function getHelp()
40 Installation - Install Friendica automatically
42 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
45 Installs Friendica with data based on the local.config.php file or environment variables
48 Not checking .htaccess/URL-Rewrite during CLI installation.
51 -h|--help|-? Show help information
52 -v Show more debug information.
53 -a All setup checks are required (except .htaccess)
54 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
55 -s|--savedb Save the DB credentials to the file (if environment variables is used)
56 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
57 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
58 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
59 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
60 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
61 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
62 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
63 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
64 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
65 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
68 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
69 MYSQL_PORT The port of the mysql/mariadb database
70 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
71 MYSQL_PASSWORD The password of the mysql/mariadb database login
72 MYSQL_DATABASE The name of the mysql/mariadb database
73 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
74 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
75 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
76 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
77 FRIENDICA_TZ The timezone of Friendica
78 FRIENDICA_LANG The langauge of Friendica
81 bin/console autoinstall -f 'input.config.php
82 Installs Friendica with the prepared 'input.config.php' file
84 bin/console autoinstall --savedb
85 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
87 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
88 Installs Friendica with a local mysql database with credentials
92 public function __construct(App\Mode $appMode, Config\Cache\ConfigCache $configCache, Config\Configuration $config, Database $dba, array $argv = null)
94 parent::__construct($argv);
96 $this->appMode = $appMode;
97 $this->configCache =$configCache;
98 $this->config = $config;
102 protected function doExecute()
104 // Initialise the app
105 $this->out("Initializing setup...\n");
107 $installer = new Installer();
109 $configCache = $this->configCache;
110 $basePathConf = $configCache->get('system', 'basepath');
111 $basepath = new BasePath($basePathConf);
112 $installer->setUpCache($configCache, $basepath->getPath());
114 $this->out(" Complete!\n\n");
117 $this->out("Checking environment...\n");
119 $installer->resetChecks();
121 if (!$this->runBasicChecks($installer, $configCache)) {
122 $errorMessage = $this->extractErrors($installer->getChecks());
123 throw new RuntimeException($errorMessage);
126 $this->out(" Complete!\n\n");
128 // if a config file is set,
129 $config_file = $this->getOption(['f', 'file']);
131 if (!empty($config_file)) {
132 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
134 $this->out("Copying config file...\n");
135 if (!copy($basePathConf . DIRECTORY_SEPARATOR . $config_file, $basePathConf . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
136 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $basePathConf . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
140 //reload the config cache
141 $loader = new ConfigFileLoader($basePathConf);
142 $loader->setupCache($configCache);
145 // Creating config file
146 $this->out("Creating config file...\n");
148 $save_db = $this->getOption(['s', 'savedb'], false);
150 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
151 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
152 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
153 $configCache->set('database', 'database',
154 $this->getOption(['d', 'dbdata'],
155 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
156 $configCache->set('database', 'username',
157 $this->getOption(['U', 'dbuser'],
158 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
159 $configCache->set('database', 'password',
160 $this->getOption(['P', 'dbpass'],
161 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
163 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
164 if (!empty($php_path)) {
165 $configCache->set('config', 'php_path', $php_path);
167 $configCache->set('config', 'php_path', $installer->getPHPPath());
170 $configCache->set('config', 'admin_email',
171 $this->getOption(['A', 'admin'],
172 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
173 $configCache->set('system', 'default_timezone',
174 $this->getOption(['T', 'tz'],
175 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
176 $configCache->set('system', 'language',
177 $this->getOption(['L', 'lang'],
178 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
180 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
181 if (!empty($basepath)) {
182 $configCache->set('system', 'basepath', $basepath);
185 $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
188 $this->out('The Friendica URL has to be set during CLI installation.');
191 $baseUrl = new BaseURL($this->config, []);
192 $baseUrl->saveByURL($url);
195 $installer->createConfig($configCache);
198 $this->out(" Complete!\n\n");
200 // Check database connection
201 $this->out("Checking database...\n");
203 $installer->resetChecks();
205 if (!$installer->checkDB($this->dba)) {
206 $errorMessage = $this->extractErrors($installer->getChecks());
207 throw new RuntimeException($errorMessage);
210 $this->out(" Complete!\n\n");
213 $this->out("Inserting data into database...\n");
215 $installer->resetChecks();
217 if (!$installer->installDatabase($basePathConf)) {
218 $errorMessage = $this->extractErrors($installer->getChecks());
219 throw new RuntimeException($errorMessage);
222 $this->out(" Complete!\n\n");
225 $this->out("Installing theme\n");
226 if (!empty($this->config->get('system', 'theme'))) {
227 Theme::install($this->config->get('system', 'theme'));
228 $this->out(" Complete\n\n");
230 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
233 $this->out("\nInstallation is finished\n");
239 * @param Installer $installer The Installer instance
240 * @param Config\Cache\ConfigCache $configCache The config cache
242 * @return bool true if checks were successfully, otherwise false
243 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
245 private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
249 $installer->resetChecks();
250 if (!$installer->checkFunctions()) {
253 if (!$installer->checkImagick()) {
256 if (!$installer->checkLocalIni()) {
259 if (!$installer->checkSmarty3()) {
262 if (!$installer->checkKeys()) {
266 $php_path = $configCache->get('config', 'php_path');
268 if (!$installer->checkPHP($php_path, true)) {
272 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
278 * @param array $results
281 private function extractErrors($results)
284 $allChecksRequired = $this->getOption('a') !== null;
286 foreach ($results as $result) {
287 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
288 $errorMessage .= "--------\n";
289 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
293 return $errorMessage;