Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Administration via command line.

Administration via command line: Difference between revisions

From MoodleDocs
No edit summary
(copying recent changes from 23 wiki)
 
(21 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Moodle 2.0}}
{{Installing Moodle}}
Some large scale deployments or special distributions may prefer command line environment when installing or upgrading Moodle. All command line tools are located in <code>admin/cli/*</code>
If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. Core admin CLI tools are located in the <code>admin/cli/*</code> folder. Other plugins provide their CLI functionality via scripts in their own cli folder. For example, the enrol_db sync script is located in <code>enrol/db/cli/</code>.


  Note: This page is a work in progress, please use comments page for improvement suggestions.
To avoid problems with access control, you should run them as the owner of the web server process. It is especially important for CLI installation and upgrade as they create new files in moodledata directory and the web server has to have write access to them. In Linux distributions, the user that runs the web server is usually apache or wwrun or httpd or something similar. As a root, you will probably want to execute Moodle CLI scripts like this:


==Installation==
    $ cd /path/to/your/moodle/dir
Advanced cli installation script.
    $ sudo -u apache /usr/bin/php admin/cli/somescript.php --params


Command line Moodle installer, creates config.php and initializes database.
Most of the scripts accept common --help (or -h) parameter to display the full usage information, for example:
Please note you must execute this script with the same uid as apache
or use chmod/chown after installation.
Site defaults may be changed via local/defaults.php.
Options:
--lang=CODE          Installation and default site language.
--wwwroot=URL        Web address for the Moodle site, required in non-interactive mode.
--dataroot=DIR        Location of the moodle data folder, must not be web accessible. Default is moodleroot in parent directory.
--dbtype=TYPE        Database type. Default is mysqli
--dbhost=HOST        Database host. Default is localhost
--dbname=NAME        Database name. Default is moodle
--dbuser=USERNAME    Database user. Default is root
--dbpass=PASSWORD    Database password. Default is blank
--dbsocket            Use database sockets. Available for some databases only.
--prefix=STRING      Table prefix for above database tables. Default is mdl_
--admin-password=PASS Password for the moodle admin account, required in non-interactive mode.
--non-interactive    No interactive questions, installation fails if any problem encountered.
--agreelicense        Indicates agreement with software license, required in non-interactive mode.
-h, --help            Print out this help
Example: $sudo -u wwwrun /usr/bin/php admin/cli/install.php --lang=cs


==Upgrades==
    $ sudo -u apache /usr/bin/php admin/cli/install.php --help


Command line Moodle upgrade.
== Upgrading via command line ==
Please note you must execute this script with the same uid as apache!
Site defaults may be changed via local/defaults.php.
Options:
--non-interactive    No interactive questions or confirmations
-h, --help            Print out this help
Example: $sudo -u wwwrun /usr/bin/php admin/cli/upgrade.php


==Reset passwords==
Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data (if you read this page, you probably have some own scripts to backup your moodledata and the database, right?)


Reset local user passwords, useful especially for admin acounts.
    $ sudo -u apache /usr/bin/php admin/cli/upgrade.php --non-interactive
There are no security checks here because anybody who is able to
 
execute this file may execute any PHP too.
Upgrading via command line is a very comfortable way of Moodle upgrade if you use [[CVS]] or [[Git|git]] checkout of the Moodle source code. See the following procedure how to upgrade your site within several seconds to the most recent version while preserving your eventual local customizations tracked in git repository:
Options:
 
-h, --help            Print out this help
    $ cd /var/www/sites/moodle/htdocs/
Example: $/usr/bin/php admin/cli/reset_password.php
    $ git fetch
    $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
    $ git merge origin/cvshead
    $ sudo -u apache /usr/bin/php admin/cli/upgrade.php
    $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable
 
'''Issues with upgrading via command line'''
 
if your config.php contains information about several moodle instances (distinct moodle websites and databases sharing the same codebase), then this script will silently fail
 
The solution is to temporarily eliminate from config.php all but the one instance you want to upgrade
 
If this is a problem for the other instances (production sites), then modify the cli script to point to a copy of config.php  (which will be the one edited to contain only one moodle instance at a time)
 
== Installation via command line ==
 
Since version 2.0, Moodle can be installed from the command line. There are two modes of installation. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.
 
    $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs
 
== Maintenance mode ==
 
To switch your site into the maintenance mode via CLI, you can use
 
    $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
 
To turn the maintenance mode off, just execute the same script with --disable parameter.
 
== Offline mode ==
 
In some situations, you may want to switch your Moodle site into offline mode so that it is not accessible via the web but you can not stop the web server completely (typically because there are other web pages and applications running there). If a file called <code>climaintenance.html</code> exists in the root folder of moodledata directory, Moodle will automatically display the contents of that file instead of any other page.
 
    $ cd /var/www/sites/moodle/moodledata/
    $ echo '&lt;h1&gt;Sorry, maintenance in progress&lt;/h1&gt;' &gt; climaintenance.html
 
You can prepare a nice formatted HTML page to inform your users about the server being down and keep in the moodledata directory under a name like <code>climaintenance.off</code> and rename it to the <code>climaintenance.html</code> if needed.
 
== Custom site defaults ==
 
During the install and upgrade via CLI, Moodle sets the administration variables to the default values. You can use different defaults. See MDL-17850 for details. Shortly, all you need to do is to add a file <code>local/defaults.php</code> into your Moodle installation. The format of the file is like
 
<code php>
<?php
$defaults['pluginname']['settingname'] = 'settingvalue'; // for plugins
$defaults['moodle']['settingname'] = 'settingvalue';    // for core settings
</code>
 
These defaults are used during install, upgrade and are also displayed as defaults at the Site administration pages.
 
== Reset user password ==
 
If you happen to forget your admin password (or you want to set a password for any other user of your Moodle system), you can use reset_password.php script. The script sets the correctly salted password for the given user.
 
    $ sudo -u apache /usr/bin/php admin/cli/reset_password.php
 
== MySQL storage engine conversion ==
 
If you run your Moodle site with MySQL database backend and use the default MyISAM as the storage engine for your tables, you may want to convert them to use some more reliable engine like InnoDB (actually, you should want to switch to PostgreSQL ;-) anyway).
 
    $ sudo -u apache /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB
 
== Running cron via command line ==
 
In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.
 
==Database transfer==
 
A command line script for [[Database transfer]] may be found in ''admin/tool/dbtransfer/cli/migrate.php''.
 
[[fr:Administration en ligne de commande]]
[[de:Administration über Kommandozeile]]
[[ja:コマンドライン経由の管理]]

Latest revision as of 10:30, 31 July 2012

If you have shell access to your web server, you may find various CLI (command line interface) scripts useful during Moodle administration. Core admin CLI tools are located in the admin/cli/* folder. Other plugins provide their CLI functionality via scripts in their own cli folder. For example, the enrol_db sync script is located in enrol/db/cli/.

To avoid problems with access control, you should run them as the owner of the web server process. It is especially important for CLI installation and upgrade as they create new files in moodledata directory and the web server has to have write access to them. In Linux distributions, the user that runs the web server is usually apache or wwrun or httpd or something similar. As a root, you will probably want to execute Moodle CLI scripts like this:

   $ cd /path/to/your/moodle/dir
   $ sudo -u apache /usr/bin/php admin/cli/somescript.php --params

Most of the scripts accept common --help (or -h) parameter to display the full usage information, for example:

   $ sudo -u apache /usr/bin/php admin/cli/install.php --help

Upgrading via command line

Moodle can be upgraded from the command line. As with the installation script, there is either interactive or non-interactive mode of the upgrade. The script itself does not put the site into the maintenance mode, you have to do it on your own. Also, the script does not backup any data (if you read this page, you probably have some own scripts to backup your moodledata and the database, right?)

   $ sudo -u apache /usr/bin/php admin/cli/upgrade.php --non-interactive

Upgrading via command line is a very comfortable way of Moodle upgrade if you use CVS or git checkout of the Moodle source code. See the following procedure how to upgrade your site within several seconds to the most recent version while preserving your eventual local customizations tracked in git repository:

   $ cd /var/www/sites/moodle/htdocs/
   $ git fetch
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable
   $ git merge origin/cvshead
   $ sudo -u apache /usr/bin/php admin/cli/upgrade.php
   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --disable

Issues with upgrading via command line

if your config.php contains information about several moodle instances (distinct moodle websites and databases sharing the same codebase), then this script will silently fail

The solution is to temporarily eliminate from config.php all but the one instance you want to upgrade

If this is a problem for the other instances (production sites), then modify the cli script to point to a copy of config.php (which will be the one edited to contain only one moodle instance at a time)

Installation via command line

Since version 2.0, Moodle can be installed from the command line. There are two modes of installation. In interactive mode, the install script asks you for all data needed to properly set up new Moodle site. In non-interactive mode, you must provide all required data as the script parameters and then the new site is installed silently. The parameters can be passed in the interactive mode, too. The provided values are then used as the default values during the interactive session.

   $ sudo -u apache /usr/bin/php admin/cli/install.php --lang=cs

Maintenance mode

To switch your site into the maintenance mode via CLI, you can use

   $ sudo -u apache /usr/bin/php admin/cli/maintenance.php --enable

To turn the maintenance mode off, just execute the same script with --disable parameter.

Offline mode

In some situations, you may want to switch your Moodle site into offline mode so that it is not accessible via the web but you can not stop the web server completely (typically because there are other web pages and applications running there). If a file called climaintenance.html exists in the root folder of moodledata directory, Moodle will automatically display the contents of that file instead of any other page.

   $ cd /var/www/sites/moodle/moodledata/
   $ echo '<h1>Sorry, maintenance in progress</h1>' > climaintenance.html

You can prepare a nice formatted HTML page to inform your users about the server being down and keep in the moodledata directory under a name like climaintenance.off and rename it to the climaintenance.html if needed.

Custom site defaults

During the install and upgrade via CLI, Moodle sets the administration variables to the default values. You can use different defaults. See MDL-17850 for details. Shortly, all you need to do is to add a file local/defaults.php into your Moodle installation. The format of the file is like

<?php $defaults['pluginname']['settingname'] = 'settingvalue'; // for plugins $defaults['moodle']['settingname'] = 'settingvalue'; // for core settings

These defaults are used during install, upgrade and are also displayed as defaults at the Site administration pages.

Reset user password

If you happen to forget your admin password (or you want to set a password for any other user of your Moodle system), you can use reset_password.php script. The script sets the correctly salted password for the given user.

   $ sudo -u apache /usr/bin/php admin/cli/reset_password.php

MySQL storage engine conversion

If you run your Moodle site with MySQL database backend and use the default MyISAM as the storage engine for your tables, you may want to convert them to use some more reliable engine like InnoDB (actually, you should want to switch to PostgreSQL ;-) anyway).

   $ sudo -u apache /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB

Running cron via command line

In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line.

Database transfer

A command line script for Database transfer may be found in admin/tool/dbtransfer/cli/migrate.php.