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)) {
133 if (!file_exists($config_file)) {
134 throw new RuntimeException("ERROR: Config file does not exist.\n");
137 //reload the config cache
138 $loader = new ConfigFileLoader($config_file);
139 $loader->setupCache($configCache);
142 // Creating config file
143 $this->out("Creating config file...\n");
145 $save_db = $this->getOption(['s', 'savedb'], false);
147 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
148 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
149 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
150 $configCache->set('database', 'database',
151 $this->getOption(['d', 'dbdata'],
152 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
153 $configCache->set('database', 'username',
154 $this->getOption(['U', 'dbuser'],
155 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
156 $configCache->set('database', 'password',
157 $this->getOption(['P', 'dbpass'],
158 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
160 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
161 if (!empty($php_path)) {
162 $configCache->set('config', 'php_path', $php_path);
164 $configCache->set('config', 'php_path', $installer->getPHPPath());
167 $configCache->set('config', 'admin_email',
168 $this->getOption(['A', 'admin'],
169 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
170 $configCache->set('system', 'default_timezone',
171 $this->getOption(['T', 'tz'],
172 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
173 $configCache->set('system', 'language',
174 $this->getOption(['L', 'lang'],
175 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
177 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
178 if (!empty($basepath)) {
179 $configCache->set('system', 'basepath', $basepath);
182 $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
185 $this->out('The Friendica URL has to be set during CLI installation.');
188 $baseUrl = new BaseURL($this->config, []);
189 $baseUrl->saveByURL($url);
192 $installer->createConfig($configCache);
195 $this->out("Complete!\n\n");
197 // Check database connection
198 $this->out("Checking database...\n");
200 $installer->resetChecks();
202 if (!$installer->checkDB($this->dba)) {
203 $errorMessage = $this->extractErrors($installer->getChecks());
204 throw new RuntimeException($errorMessage);
207 $this->out(" Complete!\n\n");
210 $this->out("Inserting data into database...\n");
212 $installer->resetChecks();
214 if (!$installer->installDatabase($basePathConf)) {
215 $errorMessage = $this->extractErrors($installer->getChecks());
216 throw new RuntimeException($errorMessage);
219 if (!empty($config_file) && $config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
221 $this->out("Copying config file...\n");
222 if (!copy($basePathConf . DIRECTORY_SEPARATOR . $config_file, $basePathConf . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
223 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $basePathConf . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
227 $this->out(" Complete!\n\n");
230 $this->out("Installing theme\n");
231 if (!empty($this->config->get('system', 'theme'))) {
232 Theme::install($this->config->get('system', 'theme'));
233 $this->out(" Complete\n\n");
235 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
238 $this->out("\nInstallation is finished\n");
244 * @param Installer $installer The Installer instance
245 * @param Config\Cache\ConfigCache $configCache The config cache
247 * @return bool true if checks were successfully, otherwise false
248 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
250 private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
254 $installer->resetChecks();
255 if (!$installer->checkFunctions()) {
258 if (!$installer->checkImagick()) {
261 if (!$installer->checkLocalIni()) {
264 if (!$installer->checkSmarty3()) {
267 if (!$installer->checkKeys()) {
271 $php_path = $configCache->get('config', 'php_path');
273 if (!$installer->checkPHP($php_path, true)) {
277 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
283 * @param array $results
286 private function extractErrors($results)
289 $allChecksRequired = $this->getOption('a') !== null;
291 foreach ($results as $result) {
292 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
293 $errorMessage .= "--------\n";
294 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
298 return $errorMessage;