Setup a Codeigniter Application on Ubuntu 16.04

Last Updated: Thu, Nov 30, 2017
Linux Guides PHP Programming Ubuntu
Archived content

This article is outdated and may not work correctly for current operating systems or software.

Introduction

Codeigniter is a very powerful PHP framework with a small footprint.

This guide assumes you have at least a working LAMP stack and root access to your VPS.

Installing Codeigniter

First, go to your Apache root folder.

cd /var/www/html

Download the latest stable release of Codeigniter.

wget https://github.com/bcit-ci/CodeIgniter/archive/3.1.5.zip

Extract the downloaded file.

unzip 3.1.5.zip

If you get an error: The program 'unzip' is currently not installed., you have to install unzip.

sudo apt install unzip

Now try to extract again.

Note a new directory called CodeIgniter-3.1.5. Rename this folder to codeigniter.

mv CodeIgniter-3.1.5 codeigniter

Navigate to http://[vultr-instance-ip]/codeigniter and you will be greeted with the codeigniter welcome page. This welcome page is produced by an example controller located at application/controllers called Welcome.php. The loaded page is located at application/views and is called welcome.php.

Configuration and Pretty URLs

Codeigniter is now up and running and you should do some initial configuration. If you plan to use a database you will have to configure the database access in the database.php configuration file located at application/config/database.php. Find the following block and update it with your connection information.

$db['default'] = array(

        'dsn'   => '',

        'hostname' => 'localhost',

        'username' => '',

        'password' => '',

        'database' => '',

        'dbdriver' => 'mysqli',

        'dbprefix' => '',

        'pconnect' => FALSE,

        'db_debug' => (ENVIRONMENT !== 'production'),

        'cache_on' => FALSE,

        'cachedir' => '',

        'char_set' => 'utf8',

        'dbcollat' => 'utf8_general_ci',

        'swap_pre' => '',

        'encrypt' => FALSE,

        'compress' => FALSE,

        'stricton' => FALSE,

        'failover' => array(),

        'save_queries' => TRUE

);

Save the file and exit. You have finished the database configuration, now let's review the Codeigniter URL scheme.

Pretty URL's

By default, URLs in Codeigniter are designed to be human friendly with a segment-based aproach:

http://[vultr-instance-ip]/codeigniter/index.php/[controller]/[method]/[param]

Following the MVC approach, the [controller] segment represents a Controller Class located at application/controllers, the [method] segment a method in this class and the [param] segment is a param passed to the method.

Let's now test this configuration by adding the following method to the index method in application/controllers/Welcome.php:

public function hello($user = 'John Doe')

{

    echo "Hello {$user}!";

}

Now navigate to http://[vultr-instance-ip]/codeigniter/index.php/welcome/hello/Jack. You should see Hello Jack! in your browser.

Removing index.php

By default index.php will be included in your url, to remove it we have to enable the Apache mod_rewrite module, add an .htaccess file to your application's root folder, and make some configurations in application/config/config.php.

First, let's check if mod_rewrite is already enabled by running the following command.

apache2ctl -M

If you see rewrite_module in the list you can move on, otherwise enable it.

a2enmod rewrite

Restart the Apache server.

sudo service apache2 restart

Now create the .htaccess file at /var/www/html/codeigniter/ and put the following content in it.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule .* index.php?/$0 [PT,L] 

Make sure that your .htaccess file is enabled by setting AllowOverride to All and adding Order allow,deny and allow from all in the virtual hosts file, or in your global apache configuration.

<Directory /var/www/>

        Options Indexes FollowSymLinks

        AllowOverride All

        Require all granted

        Order allow,deny

        allow from all

</Directory>

Now remove the index.php file from the URLs in Codeigniter configuration file application/config/config.php.

Find the following code,

$config['index_page'] = 'index.php';

And replace it with this.

$config['index_page'] = '';

You can now access your URLs without the index.php file. Navigate to http://[vultr-instance-ip]/codeigniter/welcome/hello/Jack and check this out. You should see the same message as before.

Conclusion

We have installed the Codeigniter framework and removed index.php from our URLs. You can learn more about this framework through the official user guide.

Want to contribute?

You could earn up to $600 by adding new articles.