We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Install CodeIgniter on an Ubuntu 12.04 Cloud Server

Posted Jul 1, 2013 70.3k views PHP Frameworks PHP Ubuntu

About CodeIgniter

CodeIgniter is an open source web application framework for PHP that is small in size but very powerful in utility. Its goal is to enable people to write their applications much faster than normal by providing a set of libraries and helpers for the most common tasks. It is based on the Model-View-Controller approach for a great separation of logic from presentation.

This tutorial will show you how to setup CodeIgniter on your cloud server using the command line. It will also go through some of the initial configuration you are likely to want to make.

This tutorial assumes that you are already running your own VPS with root access and have LAMP (Linux, Apache, MySQL and PHP) installed on it. You can consult this tutorial that will get you going with the latter if you haven't already.

Installing CodeIgniter (CI)

First, you need to navigate to your cloud server's directory root folder, for instance:

cd /var/www

Download the latest stable release of CI. Go to the downloads page of the CI website and check for the latest release. Then, run the following command to download it:

wget http://ellislab.com/asset/ci_download_files/reactor/CodeIgniter_2.1.2.zip

Make sure you replace the name of the zip file at the end of the URL with the latest stable release (Current version) you find there.

If you run the ls command, you should see the zip file in your folder. Unzip it with the following command:

unzip CodeIgniter_2.1.2.zip

If you get the "unzip: command not found" error, it means you don't have Unzip installed. Just run the following command to install it:

sudo apt-get install unzip

Now try it again.

If you run the ls command, you'll notice the new CodeIgniter folder extracted there. You can rename that folder to codeigniter (or whatever you want) with the following command:

mv /var/www/CodeIgniter_2.1.2 /var/www/codeigniter

Now you can point your browser to that folder:


There, you will see the CodeIgniter welcome message.

This message is produced by an example Controller you can find in the application/controllers folder called welcome.php. This Controller just loads a view located in the application/views folder that contains a simple HTML message.


Now that CodeIgniter is set up, you should do some initial configuration for your application. If you plan to work with a database, you need to set one up and provide the information for CodeIgniter to be able to communicate with it. Please consult this tutorial in case you are unfamiliar with how to create a MySQL database. Once you have that, edit the following file (make sure you replace the "codeigniter" folder name with the one you have installed it in):

nano /var/www/codeigniter/application/config/database.php

Then find the following block and edit it to include your database information:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'your_username';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'your_database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Save the file, exit and you are done with the database configuration. Next, open the config.php file and make some changes there:

nano /var/www/codeigniter/application/config/config.php

Now, set your base url. Find this block and edit according to your situation:

$config['base_url'] = 'http://www.example.com';

This is what you would set if you had the example.com domain name pointing to the folder in which you have installed CodeIgniter. In other words, if you had created a virtual host for the example.com domain name, that would have its document root in, say /var/www/codeigniter. To learn how to create virtual hosts in Apache, you can consult this tutorial.

Save the file and exit.

Side Note: Virtual Hosts

In the URLs listed below, the document root for code igniter is pointing to /var/www/codeigniter. This can be done by changing the document root in the virtual host file:

sudo nano /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
        DocumentRoot /var/www/codeigniter
<VirtualHost *:80>

The next thing you will probably want to change is to remove the index.php segment you need to put in the URL right before your Controller name. You see, the CodeIgniter URL has the following structure:

base url / index.php / controller name / controller function / controller parameter 1 / controller parameter 2 / controller parameter etc 

To test this out, you can open the welcome.php controller file:

nano /var/www/codeigniter/application/controllers/welcome.php

And below the index function, add another function like so:

public function test($param) {
  echo $param;

If you point your browser to http://www.example.com/index.php/welcome/test/3, you should see displayed the number 3 on the page. Replace the last segment and it will display that instead. And that's pretty cool.

But you may want to remove the "index.php" part so your URL looks nice and clean. There are 2 steps to do this. First, reopen the config file you had previously edited:

nano /var/www/codeigniter/application/config/config.php

Find the following code:

$config['index_page'] = 'index.php';
And replace it with:
$config['index_page'] = '';

Save and exit. Now CodeIgniter will no longer automatically include " index.php" in the URL. However, that's not enough. It just means that you will get a bunch of "Page not found" errors if you simply omit it from the URL. You will also need to create an .htaccess file to handle some redirects for you.

Note: To use the .htaccess functionality, you'll need mod_rewrite enabled on your Apache server. To check for this, run the following command:

apache2ctl -M
If you see rewrite_module in the list, you are good to go. If not, just enable it with the following command:
a2enmod rewrite

And restart Apache for the changes to take effect:

sudo service apache2 restart

You can now continue. Create the .htaccess file in the CodeIgniter root folder (next to the index.php file):

nano /var/www/codeigniter/.htaccess

And paste in the following code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L] 

Ensure that your .htaccess file is enabled by setting AllowOverride to All in the virtual hosts file:

<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all

Save the file and exit. Now, if you visit the site in the browser, you don't need to include index.php in the url:


And your URL looks much better. You are ready to start developing your PHP application using the wonders of CodeIgniter.

CodeIgniter—Article #1CodeIgniter—Article #2CodeIgniter—Article #3Article Submitted by: Danny


Creative Commons License