3 namespace Friendica\Console;
5 use Asika\SimpleConsole\Console;
7 use Friendica\App\BaseURL;
8 use Friendica\Core\Config\IConfig;
9 use Friendica\Core\Config\Cache;
10 use Friendica\Core\Installer;
11 use Friendica\Core\Theme;
12 use Friendica\Database\Database;
13 use Friendica\Util\BasePath;
14 use Friendica\Util\ConfigFileLoader;
17 class AutomaticInstallation extends Console
28 protected function getHelp()
31 Installation - Install Friendica automatically
33 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
36 Installs Friendica with data based on the local.config.php file or environment variables
39 Not checking .htaccess/URL-Rewrite during CLI installation.
42 -h|--help|-? Show help information
43 -v Show more debug information.
44 -a All setup checks are required (except .htaccess)
45 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
46 -s|--savedb Save the DB credentials to the file (if environment variables is used)
47 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
48 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
49 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
50 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
51 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
52 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
53 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
54 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
55 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
56 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
59 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
60 MYSQL_PORT The port of the mysql/mariadb database
61 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
62 MYSQL_PASSWORD The password of the mysql/mariadb database login
63 MYSQL_DATABASE The name of the mysql/mariadb database
64 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
65 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
66 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
67 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
68 FRIENDICA_TZ The timezone of Friendica
69 FRIENDICA_LANG The langauge of Friendica
72 bin/console autoinstall -f 'input.config.php
73 Installs Friendica with the prepared 'input.config.php' file
75 bin/console autoinstall --savedb
76 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
78 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
79 Installs Friendica with a local mysql database with credentials
83 public function __construct(App\Mode $appMode, Cache $configCache, IConfig $config, Database $dba, array $argv = null)
85 parent::__construct($argv);
87 $this->appMode = $appMode;
88 $this->configCache = $configCache;
89 $this->config = $config;
93 protected function doExecute()
96 $this->out("Initializing setup...\n");
98 $installer = new Installer();
100 $configCache = $this->configCache;
101 $basePathConf = $configCache->get('system', 'basepath');
102 $basepath = new BasePath($basePathConf);
103 $installer->setUpCache($configCache, $basepath->getPath());
105 $this->out(" Complete!\n\n");
108 $this->out("Checking environment...\n");
110 $installer->resetChecks();
112 if (!$this->runBasicChecks($installer, $configCache)) {
113 $errorMessage = $this->extractErrors($installer->getChecks());
114 throw new RuntimeException($errorMessage);
117 $this->out(" Complete!\n\n");
119 // if a config file is set,
120 $config_file = $this->getOption(['f', 'file']);
122 if (!empty($config_file)) {
124 if (!file_exists($config_file)) {
125 throw new RuntimeException("ERROR: Config file does not exist.\n");
128 //reload the config cache
129 $loader = new ConfigFileLoader($config_file);
130 $loader->setupCache($configCache);
133 // Creating config file
134 $this->out("Creating config file...\n");
136 $save_db = $this->getOption(['s', 'savedb'], false);
138 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
139 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
140 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
141 $configCache->set('database', 'database',
142 $this->getOption(['d', 'dbdata'],
143 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
144 $configCache->set('database', 'username',
145 $this->getOption(['U', 'dbuser'],
146 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
147 $configCache->set('database', 'password',
148 $this->getOption(['P', 'dbpass'],
149 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
151 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
152 if (!empty($php_path)) {
153 $configCache->set('config', 'php_path', $php_path);
155 $configCache->set('config', 'php_path', $installer->getPHPPath());
158 $configCache->set('config', 'admin_email',
159 $this->getOption(['A', 'admin'],
160 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
161 $configCache->set('system', 'default_timezone',
162 $this->getOption(['T', 'tz'],
163 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
164 $configCache->set('system', 'language',
165 $this->getOption(['L', 'lang'],
166 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
168 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
169 if (!empty($basepath)) {
170 $configCache->set('system', 'basepath', $basepath);
173 $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
176 $this->out('The Friendica URL has to be set during CLI installation.');
179 $baseUrl = new BaseURL($this->config, []);
180 $baseUrl->saveByURL($url);
183 $installer->createConfig($configCache);
186 $this->out("Complete!\n\n");
188 // Check database connection
189 $this->out("Checking database...\n");
191 $installer->resetChecks();
193 if (!$installer->checkDB($this->dba)) {
194 $errorMessage = $this->extractErrors($installer->getChecks());
195 throw new RuntimeException($errorMessage);
198 $this->out(" Complete!\n\n");
201 $this->out("Inserting data into database...\n");
203 $installer->resetChecks();
205 if (!$installer->installDatabase($basePathConf)) {
206 $errorMessage = $this->extractErrors($installer->getChecks());
207 throw new RuntimeException($errorMessage);
210 if (!empty($config_file) && $config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
212 $this->out("Copying config file...\n");
213 if (!copy($basePathConf . DIRECTORY_SEPARATOR . $config_file, $basePathConf . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
214 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $basePathConf . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
218 $this->out(" Complete!\n\n");
221 $this->out("Installing theme\n");
222 if (!empty($this->config->get('system', 'theme'))) {
223 Theme::install($this->config->get('system', 'theme'));
224 $this->out(" Complete\n\n");
226 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
229 $this->out("\nInstallation is finished\n");
235 * @param Installer $installer The Installer instance
236 * @param Cache $configCache The config cache
238 * @return bool true if checks were successfully, otherwise false
239 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
241 private function runBasicChecks(Installer $installer, Cache $configCache)
245 $installer->resetChecks();
246 if (!$installer->checkFunctions()) {
249 if (!$installer->checkImagick()) {
252 if (!$installer->checkLocalIni()) {
255 if (!$installer->checkSmarty3()) {
258 if (!$installer->checkKeys()) {
262 $php_path = $configCache->get('config', 'php_path');
264 if (!$installer->checkPHP($php_path, true)) {
268 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
274 * @param array $results
278 private function extractErrors($results)
281 $allChecksRequired = $this->getOption('a') !== null;
283 foreach ($results as $result) {
284 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
285 $errorMessage .= "--------\n";
286 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
290 return $errorMessage;