3 namespace Friendica\Console;
5 use Asika\SimpleConsole\Console;
7 use Friendica\Core\Config;
8 use Friendica\Core\Installer;
9 use Friendica\Core\Theme;
10 use Friendica\Database\Database;
11 use Friendica\Util\BasePath;
12 use Friendica\Util\BaseURL;
13 use Friendica\Util\ConfigFileLoader;
16 class AutomaticInstallation extends Console
23 * @var Config\Cache\ConfigCache
31 protected function getHelp()
34 Installation - Install Friendica automatically
36 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
39 Installs Friendica with data based on the local.config.php file or environment variables
42 Not checking .htaccess/URL-Rewrite during CLI installation.
45 -h|--help|-? Show help information
46 -v Show more debug information.
47 -a All setup checks are required (except .htaccess)
48 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
49 -s|--savedb Save the DB credentials to the file (if environment variables is used)
50 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
51 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
52 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
53 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
54 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
55 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
56 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
57 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
58 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
59 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
62 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
63 MYSQL_PORT The port of the mysql/mariadb database
64 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
65 MYSQL_PASSWORD The password of the mysql/mariadb database login
66 MYSQL_DATABASE The name of the mysql/mariadb database
67 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
68 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
69 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
70 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
71 FRIENDICA_TZ The timezone of Friendica
72 FRIENDICA_LANG The langauge of Friendica
75 bin/console autoinstall -f 'input.config.php
76 Installs Friendica with the prepared 'input.config.php' file
78 bin/console autoinstall --savedb
79 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
81 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
82 Installs Friendica with a local mysql database with credentials
86 public function __construct(App\Mode $appMode, Config\Cache\ConfigCache $configCache, Database $dba, array $argv = null)
88 parent::__construct($argv);
90 $this->appMode = $appMode;
91 $this->configCache =$configCache;
95 protected function doExecute()
98 $this->out("Initializing setup...\n");
100 $installer = new Installer();
102 $configCache = $this->configCache;
103 $basePathConf = $configCache->get('system', 'basepath');
104 $basepath = new BasePath($basePathConf);
105 $installer->setUpCache($configCache, $basepath->getPath());
107 $this->out(" Complete!\n\n");
110 $this->out("Checking environment...\n");
112 $installer->resetChecks();
114 if (!$this->runBasicChecks($installer, $configCache)) {
115 $errorMessage = $this->extractErrors($installer->getChecks());
116 throw new RuntimeException($errorMessage);
119 $this->out(" Complete!\n\n");
121 // if a config file is set,
122 $config_file = $this->getOption(['f', 'file']);
124 if (!empty($config_file)) {
125 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
127 $this->out("Copying config file...\n");
128 if (!copy($basePathConf . DIRECTORY_SEPARATOR . $config_file, $basePathConf . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
129 throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $basePathConf . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
133 //reload the config cache
134 $loader = new ConfigFileLoader($basePathConf);
135 $loader->setupCache($configCache);
138 // Creating config file
139 $this->out("Creating config file...\n");
141 $save_db = $this->getOption(['s', 'savedb'], false);
143 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
144 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
145 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
146 $configCache->set('database', 'database',
147 $this->getOption(['d', 'dbdata'],
148 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
149 $configCache->set('database', 'username',
150 $this->getOption(['U', 'dbuser'],
151 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
152 $configCache->set('database', 'password',
153 $this->getOption(['P', 'dbpass'],
154 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
156 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
157 if (!empty($php_path)) {
158 $configCache->set('config', 'php_path', $php_path);
160 $configCache->set('config', 'php_path', $installer->getPHPPath());
163 $configCache->set('config', 'admin_email',
164 $this->getOption(['A', 'admin'],
165 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
166 $configCache->set('system', 'default_timezone',
167 $this->getOption(['T', 'tz'],
168 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
169 $configCache->set('system', 'language',
170 $this->getOption(['L', 'lang'],
171 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
173 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
174 if (!empty($basepath)) {
175 $configCache->set('system', 'basepath', $basepath);
178 $url = $this->getOption(['U', 'url'], !empty(getenv('FRIENDICA_URL')) ? getenv('FRIENDICA_URL') : null);
181 $this->out('The Friendica URL has to be set during CLI installation.');
184 $baseUrl = new BaseURL($basePathConf, []);
185 $baseUrl->saveByURL($url);
188 $installer->createConfig($configCache);
191 $this->out(" Complete!\n\n");
193 // Check database connection
194 $this->out("Checking database...\n");
196 $installer->resetChecks();
198 if (!$installer->checkDB($this->dba)) {
199 $errorMessage = $this->extractErrors($installer->getChecks());
200 throw new RuntimeException($errorMessage);
203 $this->out(" Complete!\n\n");
206 $this->out("Inserting data into database...\n");
208 $installer->resetChecks();
210 if (!$installer->installDatabase($basePathConf)) {
211 $errorMessage = $this->extractErrors($installer->getChecks());
212 throw new RuntimeException($errorMessage);
215 $this->out(" Complete!\n\n");
218 $this->out("Installing theme\n");
219 if (!empty($configCache->get('system', 'theme'))) {
220 Theme::install($configCache->get('system', 'theme'));
221 $this->out(" Complete\n\n");
223 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
226 $this->out("\nInstallation is finished\n");
232 * @param Installer $installer The Installer instance
233 * @param Config\Cache\ConfigCache $configCache The config cache
235 * @return bool true if checks were successfully, otherwise false
236 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
238 private function runBasicChecks(Installer $installer, Config\Cache\ConfigCache $configCache)
242 $installer->resetChecks();
243 if (!$installer->checkFunctions()) {
246 if (!$installer->checkImagick()) {
249 if (!$installer->checkLocalIni()) {
252 if (!$installer->checkSmarty3()) {
255 if (!$installer->checkKeys()) {
259 $php_path = $configCache->get('config', 'php_path');
261 if (!$installer->checkPHP($php_path, true)) {
265 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
271 * @param array $results
274 private function extractErrors($results)
277 $allChecksRequired = $this->getOption('a') !== null;
279 foreach ($results as $result) {
280 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
281 $errorMessage .= "--------\n";
282 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
286 return $errorMessage;