Thursday, February 28, 2008

Remote Installation of Joomla

Working mainly with the Joomla CMS as our base for building websites, I have to install at least one Joomla site a week. Sometimes, 2-3 a day. This can become quite tedious. Here is a few tricks to help you out.

What this applies to

This article does not apply to Joomla alone. It can be used to install any web application on a remote server.

What to avoid when doing a remote install

If you have a T3 connection, then you probably don't mind downloading the full Joomla installation package. However, in if you have a relatively slow connection like I do, you want to avoid having to download anything to your local computer. What you want is to have all downloads go between the remote computer, and the remote site you are downloading Joomla from.

Uploading uncompressed folders, apart from being slow, is more error prone. Some FTP clients are known to lose a few files here and there. Your files will inherit the privileges set by your local system, or the ftp client. You don't want this. What you want is to upload a compressed archive, then uncompress it on your server, letting it create the privileges.

Installing Joomla with remote Shell Access

If you have SSH privileges on the remote host, then installing Joomla should be a breeze.

First fire up your SSH client, such as Putty. Then make your way to the folder you will be installing Joomla.

eg: cd /var/www/html/joomla1/
or
eg: cd /user/root/public_html/joomla1/
The filesystem structure will depend on your system.

Go to Joomla website and find the download page for the latest Joomla version you want to install. As the time of this writing, there are two forks of the Joomla CMS, the 1.0.x and 1.5 versions.

Regardless of which version you choose, make sure its the latest update, and the full package.

Now, instead of downloading Joomla to your computer, right click the download link and copy the URL.

In your SSH client, use wget to retrieve the remote file to the folder you will be installing Joomla. eg:

wget 'http://joomlacode.org/gf/download/frsrelease/6828/22537/Joomla_1.0.15-Stable-Full_Package.tar.gz'
I do this by typing in wget and then right clicking in the putty screen to paste the Joomla download URL.

Now you should have the file:

/var/www/html/joomla1/Joomla_1.0.15-Stable-Full_Package.tar.gz

Now you need to uncompress the file.

gunzip 'Joomla_1.0.15-Stable-Full_Package.tar.gz'
tar -xvf 'Joomla_1.0.15-Stable-Full_Package.tar'

gunzip will uncompress the Gzip archive. This will leave you with the Tar achive: Joomla_1.0.15-Stable-Full_Package.tar. You want to uncompress this too so so you use tar -xvf. Regarding -xvf: x if for extract, v is for verbose, and f is for file.

Now you should have the uncompressed Joomla Installation in your folder. So you will just have to go to your browser, type in the URL of the installation folder, and you can start with the web based installation.

Before you do that, you will need to set up your database.

This is quick and easy from the shell.

mysql -u username -p 
Assuming you have the user root with full privileges, you can log into mysql with that user and create your database:
mysql -u root -p 
You will be prompted for the password, which you fill in and hit enter.

Now you should be logged into mysql, so you can create your database.

create database `joomla_joomla1`;
I usually prefix all Joomla databases with joomla_. You can name it anything you want.

Now that you have your database table, you can associate a user with that table. Its good to have a user that only has access to that table.

GRANT ALL PRIVILEGES ON joomla_joomla1.* TO joomla@localhost IDENTIFIED BY 'abc123xyz';
where 'abc123xyz' is of course the password. This will create the user joomla, with full privileges to the joomla_joomla1 database. In joomla_joomla1.*, the asterisk matches any table in the joomla_joomla1 database. joomla@localhost means the user has to be on the host, localhost and not a different domain.

Now you can continue with the online installer, fill in the database installation, and complete the installation of Joomla.

Installing Joomla with PHP shell execution

If you don't have shell access, then it may not be so simple. However, you can still achieve something similar to shell access by using PHP to execute your commands.

If you at least have access to a remote server which as a faster internet connection then your local internet connection, you may want to use that speed to your benefit. You can log into a different server with SSH, then open an FTP session between your SSH server and the server you will be installing Joomla on. Then you can transfer the Joomla installation package from Joomla.org to your SSH server with wget, then FTP it to the Joomla Install server.

Another method is to open an FTP session to the server you will install Joomla on, and create a PHP file on the directory you will be installing Joomla on. Then you place commands in the PHP file and execute them. For example, to download Joomla.

<?php
echo exec("wget 'http://joomlacode.org/gf/download/frsrelease/6828/22537/Joomla_1.0.15-Stable-Full_Package.tar.gz'");
?>
exec() executes system commands from within PHP. You can also use system() or an equivalent function if system commands execution is allowed in your PHP installation.

If you're able to execute shell commands from within PHP, you can follow the steps in "installing Joomla with Remote Shell Access" but each time you will have to write the shell command into PHP and then execute it from visiting the page in the browser.

Installing Joomla with just PHP

Now on some servers, you're just stuck with no shell access, and shell calls disabled in PHP. This give you no choice but to install Joomla with just PHP, or FTP. Still, I prefer using PHP to uploading each file via FTP.

Here's how to quickly download the Joomla Installation package:

<?php

/**
 * Copy a remote URL to local file
 * @return Bool Success or Failure
 * @param $url String URL
 * @param $file String Local File
 */
function copy_url($url, $file, $len = 2028) {
 $len = intval($len ? $len : 2028);
 $ur = fopen($url, 'rb');
 $fw = fopen($file, 'wb');
 if ($ur && $fw) {
  while($buf = fread($ur, $len)) {
   if (!fwrite($fw, $buf, $len)) {
    return false;
   }
  }
  fclose($ur);
  fclose($fw);
  return true;
 }
 return false;
 
}

// download the Joomla install archive
$url = 'http://joomlacode.org/gf/download/frsrelease/6828/22537/Joomla_1.0.15-Stable-Full_Package.tar.gz';
$file = 'joomla.tar.gz';
copy_url($url, $file);

?>

Now you have to uncompress this archive. If you have access to Cpanel, you can easily do this using the Cpanel File Manager. Otherwise, you'll have to PHP functions. (I will update a function for this here). Update: See comments for the unzip function.

To create the MySQL database using PHP, you still need a user with database creation privileges. You can also use PHPMyAdmin if you have that installed on the server.

<?php

error_reporting(E_ALL);

echo 'connect to db
'; $link = mysql_connect('localhost', 'root', 'abc123xyz'); if (!$link) die('Could not connect to db'); echo 'create table
'; $query = 'create database joomla_joomla1'; $result = mysql_query($query); if (!$result) { echo('Invalid query: ' . mysql_error()); } echo 'close link
'; mysql_close($link); ?>
Save the PHP code to a PHP file and execute it by visiting the URL in the browser.

If all went well, you should have a joomla installation and a database ready. So now you can visit the Joomla folder via the browser and start with your Installation of Joomla.

Security Precautions while installing Joomla

A good idea, is to always secure the directory you are going to install Joomla in, with a .htaccess file. You can easily set up HTTP Basic Authentication just for the duration of your install. Otherwise, the installation is open to the public. So if you went out for tea and came back to finish the install, you could have ABC Hacker from XYZ has defaced your Server printed on your screen.

After installing Joomla, always delete the installation directory, as well as the INSTALL.php file.

Conclusion

I hope this bit has helped you make your installations of Joomla, or any other PHP application a bit more efficient.

7 comments:

John Doe said...

Great article - I do a lot of joomla installs, and this will save me some time- thanks :-)

It would be awesome with a php unzip function of the downloaded package though :-)

3biano said...

Useful article for a very good practice. Regarding the Unzip PHP Script i've tried with this:

http://www.phpconcept.net/pclzip/index.php

and it worked fine.

e.g. to unzip a file with this lib

...
require_once('pclzip.lib.php');
$archive = new PclZip('myzip.zip');
if ($archive->extract() == 0) {
die("Error : ".$archive->errorInfo(true));
}
...

(Tnx to blueday54555
on http://www.codeguru.com/forum/archive/index.php/t-335868.html)

bucabay said...

Hi 3biano,

Thanks for posting the php version of deflating zip files. That should come in handy.

Ambassadors For Christ's Church said...

Really i thank you very much for your help here...


Thank you friend
Venu.K
venukommu@gmail.com

Ambassadors For Christ's Church said...

Hi,
Once again thanks for your article (Remote Installation Of Joomla).

I stuck up here is, I downloaded the Joomla latest version using wget command and extracted itself as you told.

Next step is, I need to do web installation, but i am not getting web installation. I am getting here one error is "The requested URL /Joomla/ was not found on this server. Apache/2.2.3 (Red Hat) Server at server1.sample.com Port 80" ...

Can you tell me that how can i do the web installation here please ?

Really i thankful for your help regarding this article....

This is my ID: venukommu@gmail.com

Thanks&Regards
Venu.K

bucabay said...

You have to extract the Joomla installation archive to a web directory. For example, if your document root (web directory root) is /var/www/html

Then extract it to /var/www/html/joomla

or similar. Then you can visit it at yoursite.com/joomla

David said...

Great time saver thanks