3 namespace Friendica\Core\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\Config\ConfigFileLoader;
13 class AutomaticInstallation extends Console
15 protected function getHelp()
18 Installation - Install Friendica automatically
20 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
23 Installs Friendica with data based on the local.config.php file or environment variables
26 Not checking .htaccess/URL-Rewrite during CLI installation.
29 -h|--help|-? Show help information
30 -v Show more debug information.
31 -a All setup checks are required (except .htaccess)
32 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
33 -s|--savedb Save the DB credentials to the file (if environment variables is used)
34 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
35 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
36 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
37 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
38 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
39 -U|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
40 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
41 -b|--basepath <base_path> The basepath of Friendica(env FRIENDICA_BASE_PATH)
42 -S|--sslpolicy <ssl_policy> The SSL policy of Friendica (env FRIENDICA_SSL_POLICY)
43 -n|--hostname <hostname> The hostname of Friendica (env FRIENDICA_PHP_HOSTNAME)
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_PATH The URL path of Friendica (f.e. '/friendica') - leave empty for auto detection
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_SSL_POLICY The SSL policy of Friendica (default is NO SSL)
58 FRIENDICA_HOSTNAME The hostname of Friendica - leave empty for auto detection
59 FRIENDICA_TZ The timezone of Friendica
60 FRIENDICA_LANG The langauge of Friendica
63 bin/console autoinstall -f 'input.config.php
64 Installs Friendica with the prepared 'input.config.php' file
66 bin/console autoinstall --savedb
67 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
69 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
70 Installs Friendica with a local mysql database with credentials
74 protected function doExecute()
77 $this->out("Initializing setup...\n");
79 $a = BaseObject::getApp();
81 $installer = new Installer();
83 $configCache = $a->getConfigCache();
84 $installer->setUpCache($configCache, dirname(__DIR__, 3), $_SERVER);
86 $this->out(" Complete!\n\n");
89 $this->out("Checking environment...\n");
91 $installer->resetChecks();
93 if (!$this->runBasicChecks($installer, $configCache)) {
94 $errorMessage = $this->extractErrors($installer->getChecks());
95 throw new RuntimeException($errorMessage);
98 $this->out(" Complete!\n\n");
100 // if a config file is set,
101 $config_file = $this->getOption(['f', 'file']);
103 if (!empty($config_file)) {
104 if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
106 $this->out("Copying config file...\n");
107 if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
108 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");
112 //reload the config cache
113 $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode());
114 $loader->setupCache($configCache);
117 // Creating config file
118 $this->out("Creating config file...\n");
120 $save_db = $this->getOption(['s', 'savedb'], false);
122 //$db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST') ? getenv('MYSQL_HOST') : Installer::DEFAULT_HOST) : '');
123 $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? (getenv('MYSQL_HOST')) : Installer::DEFAULT_HOST);
124 $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
125 $configCache->set('database', 'hostname', $db_host . (!empty($db_port) ? ':' . $db_port : ''));
126 $configCache->set('database', 'database',
127 $this->getOption(['d', 'dbdata'],
128 ($save_db) ? getenv('MYSQL_DATABASE') : ''));
129 $configCache->set('database', 'username',
130 $this->getOption(['U', 'dbuser'],
131 ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : ''));
132 $configCache->set('database', 'password',
133 $this->getOption(['P', 'dbpass'],
134 ($save_db) ? getenv('MYSQL_PASSWORD') : ''));
135 $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
136 if (!empty($php_path)) {
137 $configCache->set('config', 'php_path', $php_path);
139 $configCache->set('config', 'php_path', $installer->getPHPPath());
142 $configCache->set('config', 'admin_email',
143 $this->getOption(['A', 'admin'],
144 !empty(getenv('FRIENDICA_ADMIN_MAIL')) ? getenv('FRIENDICA_ADMIN_MAIL') : ''));
145 $configCache->set('system', 'default_timezone',
146 $this->getOption(['T', 'tz'],
147 !empty(getenv('FRIENDICA_TZ')) ? getenv('FRIENDICA_TZ') : Installer::DEFAULT_TZ));
148 $configCache->set('system', 'language',
149 $this->getOption(['L', 'lang'],
150 !empty(getenv('FRIENDICA_LANG')) ? getenv('FRIENDICA_LANG') : Installer::DEFAULT_LANG));
152 $configCache->set('system', 'urlpath', $this->getOption(['u', 'urlpath'], !empty(getenv('FRIENDICA_URL_PATH')) ? getenv('FRIENDICA_URL_PATH') : ''));
153 $basepath = $this->getOption(['b', 'basepath'], !empty(getenv('FRIENDICA_BASE_PATH')) ? getenv('FRIENDICA_BASE_PATH') : null);
154 if (!empty($basepath)) {
155 $configCache->set('system', 'basepath', $basepath);
157 $php_path = $this->getOption(['B', 'phppath'], !empty(getenv('FRIENDICA_PHP_PATH')) ? getenv('FRIENDICA_PHP_PATH') : null);
158 if (!empty($php_path)) {
159 $configCache->set('config', 'php_path', $php_path);
161 $ssl_policy = $this->getOption(['S', 'sslpolicy'], !empty(getenv('FRIENDICA_SSL_POLICY')) ? getenv('FRIENDICA_SSL_POLICY') : null);
162 if (!empty($ssl_policy)) {
163 $configCache->set('system', 'ssl_policy', $ssl_policy);
165 $configCache->set('config', 'hostname', $this->getOption(['n', 'hostname'], !empty(getenv('FRIENDICA_HOSTNAME')) ? getenv('FRIENDICA_HOSTNAME') : ''));
167 $configCache->set('system', 'url', $installer->determineBaseUrl($configCache));
169 if (empty($configCache->get('config', 'hostname'))) {
170 $this->out('The Friendica hostname has to be set during CLI installation.');
174 $installer->createConfig($configCache);
177 $this->out(" Complete!\n\n");
179 // Check database connection
180 $this->out("Checking database...\n");
182 $installer->resetChecks();
184 if (!$installer->checkDB($configCache, $a->getProfiler())) {
185 $errorMessage = $this->extractErrors($installer->getChecks());
186 throw new RuntimeException($errorMessage);
189 $this->out(" Complete!\n\n");
192 $this->out("Inserting data into database...\n");
194 $installer->resetChecks();
196 if (!$installer->installDatabase($a->getBasePath())) {
197 $errorMessage = $this->extractErrors($installer->getChecks());
198 throw new RuntimeException($errorMessage);
201 $this->out(" Complete!\n\n");
204 $this->out("Installing theme\n");
205 if (!empty(Config::get('system', 'theme'))) {
206 Theme::install(Config::get('system', 'theme'));
207 $this->out(" Complete\n\n");
209 $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
212 $this->out("\nInstallation is finished\n");
218 * @param Installer $installer The Installer instance
219 * @param Config\Cache\IConfigCache $configCache The config cache
221 * @return bool true if checks were successfully, otherwise false
222 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
224 private function runBasicChecks(Installer $installer, Config\Cache\IConfigCache $configCache)
228 $installer->resetChecks();
229 if (!$installer->checkFunctions()) {
232 if (!$installer->checkImagick()) {
235 if (!$installer->checkLocalIni()) {
238 if (!$installer->checkSmarty3()) {
241 if (!$installer->checkKeys()) {
245 $php_path = $configCache->get('config', 'php_path');
247 if (!$installer->checkPHP($php_path, true)) {
251 $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
257 * @param array $results
260 private function extractErrors($results)
263 $allChecksRequired = $this->getOption('a') !== null;
265 foreach ($results as $result) {
266 if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
267 $errorMessage .= "--------\n";
268 $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
272 return $errorMessage;