Added create_priv and drop_priv to koha's mysql user (needed for updating the
[koha.git] / installer.pl
1 #!/usr/bin/perl -w # please develop with -w
2
3 use diagnostics;
4 use strict; # please develop with the strict pragma
5
6 unless ($< == 0) {
7     print "You must be root to run this script.\n";
8     exit 1;
9 }
10
11 system('clear');
12 print qq|
13 **********************************
14 * Welcome to the Koha Installer  *
15 **********************************
16 Welcome to the Koha install script!  This script will prompt you for some
17 basic information about your desired setup, then install Koha according to
18 your specifications.  To accept the default value for any question, simply hit
19 Enter at the prompt.
20
21 Please be sure to read the documentation, or visit the Koha website at 
22 http://www.koha.org for more information.
23
24 Are you ready to begin the installation? (Y/[N]):
25 |;
26
27 my $answer = <STDIN>;
28 chomp $answer;
29
30 if ($answer eq "Y" || $answer eq "y") {
31         print "Great! continuing setup... \n";
32     } else {
33     print qq|
34 This installer currently does not support an completely automated 
35 setup.
36
37 Please be sure to read the documentation, or visit the Koha website 
38 at http://www.koha.org for more information.
39 |;
40     exit;
41 };
42
43 print "\n";
44
45 #
46 # Test for Perl and Modules
47 #
48 print qq|
49
50 PERL & MODULES
51 ==============
52
53 |;
54
55 print "\nChecking perl modules ...\n";
56     unless (eval "require 5.004") {
57     die "Sorry, you need at least Perl 5.004\n";
58 }
59
60 my @missing = ();
61 unless (eval {require DBI})               { push @missing,"DBI" };
62 unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
63 unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
64 unless (eval {require Set::Scalar})       { push @missing,"Set::Scalar" };
65
66 #
67 # Print out a list of any missing modules
68 #
69 if (@missing > 0) {
70     print "\n\n";
71     print "You are missing some Perl modules which are required by Koha.\n";
72     print "Once these modules have been installed, rerun this installer.\n";
73     print "They can be installed by running (as root) the following:\n";
74     foreach my $module (@missing) {
75         print "   perl -MCPAN -e 'install \"$module\"'\n";
76         exit(1);
77     }} else{
78     print "All modules appear to be installed, continuing...\n";
79 };
80
81
82 print "\n";
83 my $input;
84 my $domainname = `hostname -d`;
85 chomp $domainname;
86 my $opacdir = '/usr/local/www/opac';
87 my $kohadir = '/usr/local/www/koha';
88 print qq|
89
90 OPAC DIRECTORY
91 ==============
92 Please supply the directory you want Koha to store its OPAC files in.  Leave off
93 the trailing slash.  This directory will be auto-created for you if it doesn't
94 exist.
95
96 Usually $opacdir
97 |;
98
99 print "Enter directory [$opacdir]: ";
100 chomp($input = <STDIN>);
101
102 if ($input) {
103   $opacdir = $input;
104 }
105
106
107 print qq|
108
109 INTRANET/LIBRARIANS DIRECTORY
110 =============================
111 Please supply the directory you want Koha to store its Intranet/Librarians files 
112 in.  Leave off the trailing slash.  This directory will be auto-created for you if 
113 it doesn't exist.
114
115 Usually $kohadir
116 |;
117
118 print "Enter directory [$kohadir]: ";
119 chomp($input = <STDIN>);
120
121 if ($input) {
122   $kohadir = $input;
123 }
124
125 #
126 #KOHA conf
127 #
128 my $etcdir = '/etc';
129 my $dbname = 'Koha';
130 my $hostname = 'localhost';
131 my $user = 'kohaadmin';
132 my $pass = '';
133
134 print qq|
135
136 KOHA.CONF
137 =========
138 Koha uses a small configuration file that is usually placed in your /etc/ files 
139 directory. The configuration file, will be created in this directory
140
141 |;
142
143 #Get the path to the koha.conf directory
144 print "Enter the path to your configuration directory [$etcdir]: ";
145 chomp($input = <STDIN>);
146
147 if ($input) {
148   $etcdir = $input;
149 }
150
151
152 #Get the database name
153 print qq|
154
155 Please provide the name of the mysql database for your koha installation.
156 This is normally "$dbname".
157
158 |;
159
160 print "Enter database name [$dbname]: ";
161 chomp($input = <STDIN>);
162
163 if ($input) {
164   $dbname = $input;
165 }
166
167
168 #Get the hostname for the database
169 print qq|
170
171 Please provide the hostname for mysql.  Unless the database is located on another 
172 machine this will be "localhost".
173 |;
174
175 print "Enter hostname [$hostname]: ";
176 chomp($input = <STDIN>);
177
178 if ($input) {
179   $hostname = $input;
180 }
181
182 #Get the username for the database
183 print qq|
184
185 Please provide the name of the user, who will have full administrative rights
186 to the $dbname database, when authenticating from $hostname.
187
188 If no user is entered it will default to $user.
189 |;
190
191 print "Enter username [$user]:";
192 chomp($input = <STDIN>);
193
194 if ($input) {
195   $user = $input;
196 }
197
198 #Get the password for the database user
199 print qq|
200
201 Please provide a good password for the user $user.
202 |;
203
204 print "Enter password:";
205 chomp($input = <STDIN>);
206
207 if ($input) {
208   $pass = $input;
209 }
210
211 print "\n";
212
213
214 #Create the configuration file
215 open(SITES,">$etcdir/koha.conf") or warn "Couldn't create file
216 at $etcdir.  Must have write capability.\n";
217 print SITES <<EOP
218 database=$dbname
219 hostname=$hostname
220 user=$user
221 pass=$pass
222 includes=$kohadir/htdocs/includes
223 EOP
224 ;
225 close(SITES);
226
227 print "Successfully created the Koha configuration file.\n";
228
229 my $httpduser;
230 my $realhttpdconf;
231
232 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
233                       /usr/local/etc/apache/httpd.conf
234                       /usr/local/etc/apache/apache.conf
235                       /var/www/conf/httpd.conf
236                       /etc/apache/conf/httpd.conf
237                       /etc/apache/conf/apache.conf
238                       /etc/httpd/conf/httpd.conf
239                       /etc/httpd/httpd.conf)) {
240    if ( -f $httpdconf ) {
241             $realhttpdconf=$httpdconf;
242             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
243       while (<HTTPDCONF>) {
244          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
245             $httpduser = $1;
246          }
247       }
248       close(HTTPDCONF);
249    }
250 }
251 $httpduser ||= 'Undetermined';
252
253 #
254 # Set ownership of the koha.conf file for security
255 #
256 chown((getpwnam($httpduser)) [2,3], "$etcdir/koha.conf") or warn "can't chown koha.conf: $!";
257 chmod 0440, "$etcdir/koha.conf";
258
259 #
260 #SETUP opac
261 #
262 my $svr_admin = "webmaster\@$domainname";
263 my $opac_svr_name = "opac.$domainname";
264 my $koha_svr_name = "koha.$domainname";
265
266 print qq|
267
268 OPAC and KOHA/LIBRARIAN CONFIGURATION
269 =====================================
270 Koha needs to setup your Apache configuration file for the
271 OPAC virtual host.
272
273 Please enter the e-mail address for your webserver admin.
274 Usually $svr_admin
275 |;
276
277 print "Enter e-mail address [$svr_admin]:";
278 chomp($input = <STDIN>);
279
280 if ($input) {
281   $svr_admin = $input;
282 }
283
284
285 print qq|
286
287 Please enter the servername for your OPAC interface.
288 Usually $opac_svr_name
289 |;
290 print "Enter servername address [$opac_svr_name]:";
291 chomp($input = <STDIN>);
292
293 if ($input) {
294   $opac_svr_name = $input;
295 }
296
297 print qq|
298
299 Please enter the servername for your Intranet/Librarian interface.
300 Usually $koha_svr_name
301 |;
302 print "Enter servername address [$koha_svr_name]:";
303 chomp($input = <STDIN>);
304
305 if ($input) {
306   $koha_svr_name = $input;
307 }
308
309
310 #
311 # Update Apache Conf File.
312 #
313 print qq|
314
315 UPDATING APACHE.CONF
316 ====================
317
318 |;
319 if (`grep 'VirtualHost $opac_svr_name' $realhttpdconf`) {
320     warn "$realhttpdconf appears to already have an entry for Koha\nVirtual Hosts.\n";
321     print "Press <ENTER> to continue...";
322     <STDIN>;
323     print "\n";
324 } else {
325     open(SITE,">>$realhttpdconf") or warn "Insufficient priveleges to open $realhttpdconf for writing.\n";
326     print SITE <<EOP
327
328 <VirtualHost $opac_svr_name>
329    ServerAdmin $svr_admin
330    DocumentRoot $opacdir/htdocs
331    ServerName $opac_svr_name
332    ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
333    ErrorLog logs/opac-error_log
334    TransferLog logs/opac-access_log common
335    SetEnv PERL5LIB "$kohadir/modules"
336 </VirtualHost>
337
338 <VirtualHost $koha_svr_name>
339    ServerAdmin $svr_admin
340    DocumentRoot $kohadir/htdocs
341    ServerName $koha_svr_name
342    ScriptAlias /cgi-bin/koha/ "$kohadir/cgi-bin/"
343    ErrorLog logs/koha-error_log
344    TransferLog logs/koha-access_log common
345    SetEnv PERL5LIB "$kohadir/modules"
346 </VirtualHost>
347
348 EOP
349 ;
350     close(SITE);
351     print "Successfully updated Apache Configuration file.\n";
352
353 }
354
355 #
356 # Setup the modules directory
357 #
358 print qq|
359
360 CREATING REQUIRED DIRECTORIES
361 =============================
362
363 |;
364
365
366 unless ( -d $kohadir ) {
367    print "Creating $kohadir...\n";
368    my $result=mkdir ($kohadir, oct(770));
369    if ($result==0) {
370        my @dirs = split(m#/#, $kohadir);
371         my $checkdir='';
372         foreach (@dirs) {
373             $checkdir.="$_/";
374             unless (-e "$checkdir") {
375                 mkdir($checkdir, 0775);
376             }
377         }
378    }
379    chown (oct(0), (getgrnam($httpduser))[2], "$kohadir");
380    chmod (oct(770), "$kohadir");
381 }
382 unless ( -d "$kohadir/htdocs" ) {
383    print "Creating $kohadir/htdocs...\n";
384    mkdir ("$kohadir/htdocs", oct(750));
385 }
386 unless ( -d "$kohadir/cgi-bin" ) {
387    print "Creating $kohadir/cgi-bin...\n";
388    mkdir ("$kohadir/cgi-bin", oct(750));
389 }
390 unless ( -d "$kohadir/modules" ) {
391    print "Creating $kohadir/modules...\n";
392    mkdir ("$kohadir/modules", oct(750));
393 }
394 unless ( -d $opacdir ) {
395    print "Creating $opacdir...\n";
396    my $result=mkdir ($opacdir, oct(770));
397    if ($result==0) {
398        my @dirs = split(m#/#, $opacdir);
399         my $checkdir='';
400         foreach (@dirs) {
401             $checkdir.="$_/";
402             unless (-e "$checkdir") {
403                 mkdir($checkdir, 0775);
404             }
405         }
406    }
407    chown (oct(0), (getgrnam($httpduser))[2], "$opacdir");
408    chmod (oct(770), "$opacdir");
409 }
410 unless ( -d "$opacdir/htdocs" ) {
411    print "Creating $opacdir/htdocs...\n";
412    mkdir ("$opacdir/htdocs", oct(750));
413 }
414 unless ( -d "$opacdir/cgi-bin" ) {
415    print "Creating $opacdir/cgi-bin...\n";
416    mkdir ("$opacdir/cgi-bin", oct(750));
417 }
418
419
420
421 print "\n\nINSTALLING KOHA...\n";
422 print "\n\n==================\n";
423 print "Copying internet-html files to $kohadir/htdocs...\n";
424 system("cp -R intranet-html/* $kohadir/htdocs/");
425 print "Copying intranet-cgi files to $kohadir/cgi-bin...\n";
426 system("cp -R intranet-cgi/* $kohadir/cgi-bin/");
427 print "Copying script files to $kohadir/modules...\n";
428 system("cp -R modules/* $kohadir/modules/");
429 print "Copying opac-html files to $opacdir/htdocs...\n";
430 system("cp -R opac-html/* $opacdir/htdocs/");
431 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
432 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
433
434 system("chown -R root.$httpduser $opacdir");
435 system("chown -R root.$httpduser $kohadir");
436
437 print qq|
438
439 MYSQL CONFIGURATION
440 ===================
441 |;
442 my $mysql;
443 my $mysqldir;
444 my $mysqluser = 'root';
445 my $mysqlpass = '';
446
447 foreach my $mysql (qw(/usr/local/mysql
448                       /opt/mysql
449                       )) {
450    if ( -d $mysql ) {
451             $mysqldir=$mysql;
452    }
453 }
454 if (!$mysqldir){
455     $mysqldir='/usr';
456 }
457 print qq|
458 To allow us to create the koha database please supply the 
459 mysql\'s root users password
460 |;
461
462 print "Enter mysql\'s root users password: ";
463 chomp($input = <STDIN>);
464
465 if ($input) {
466   $mysqlpass = $input;
467 }
468
469
470 print qq|
471
472 CREATING DATABASE
473 =================
474 |;
475 my $result=system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass create $dbname");
476 if ($result) {
477     print "\nCouldn't connect to the MySQL server for the reason given above.";
478     print "Press <ENTER> to continue...";
479     <STDIN>;
480     print "\n";
481 } else {
482     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname < koha.mysql");
483     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass mysql -e \"insert into user (Host,User,Password) values ('$hostname','$user',password('$pass'))\"\;");
484     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass mysql -e \"insert into db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) values ('%','$dbname','$user','Y','Y','Y','Y','Y','Y');");
485     system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass reload");
486
487     system ("perl -I $kohadir/modules scripts/updater/updatedatabase");
488 }
489
490
491 #RESTART APACHE
492 #system('clear');
493 print "\n\n";
494 print qq|
495 COMPLETED
496 =========
497 Congratulations ... your Koha installation is almost complete!
498 The final step is to restart your webserver.
499
500 Be sure to read the INSTALL, and Hints files. 
501
502 For more information visit http://www.koha.org
503
504 Would you like to restart your webserver now? (Y/[N]):
505 |;
506
507 my $restart = <STDIN>;
508 chomp $restart;
509
510 if ($answer eq "Y" || $answer eq "y") {
511         # Need to support other init structures here?
512         if (-e "/etc/rc.d/init.d/httpd") {
513             system('/etc/rc.d/init.d/httpd restart');
514         } elsif (-e "/etc/init.d/apache") {
515             system('/etc//init.d/apache restart');
516         } elsif (-e "/etc/init.d/apache-ssl") {
517             system('/etc/init.d/apache-ssl restart');
518         }
519     } else {
520     print qq|
521 print "\nCongratulations ... your Koha installation is complete!\n";
522 print "\nYou will need to restart your webserver before using Koha!\n";
523 |;
524     exit;
525 };