If Only I Knew That!

Webby things that could save you time

Setting up Codeigniter

with 6 comments

After setting up several codeigniter based sites with dev/staging/production environments, I thought I’d write up the steps I undertake.

A overview of the steps are:

  1. Download CI and upload to hosting environment
  2. Rename ‘system’ directory
  3. Move ‘application’ directory to public accessible directory
  4. Upload and modify index.php
  5. Add .htaccess to remove index.php from URL’s
  6. Modify ‘config.php’ for domain and remove of index.php form URL’s
  7. Add ‘application.php’ specific config file
  8. Modify ‘database.php’ config for dev/staging/production
  9. Modify the autoload config for regularly used libraries, helpers and application config file

My hosting servers are cpanel based and I’ll use example.com as the setup domain and the system path of /home/exampleuser/

Step 1:

Download the latest CI from Codeigniter and upload the system directory to hosting environment in the /home/exampleuser/ directory. Note – This is a non public accessible directory

Step 2:

Rename ‘system’ to CI-version number at the time of writing the current version number is 1.7.1 so the directory is renamed ‘CI-1.7.1′ this allows me to see what version the site is running off at a glance and makes a update easier to manage.

Step 3:

Move the ‘application’ directory out of our newly renamed directory ‘CI-1.7.1′ to a publicly accessible directory in this case /home/exampleuser/public_html/

Step 4:

Upload ‘index.php’ to our publicly accessible directory as well. Then edit it with your preferred editor to match our new system directory name and path, like so

/*
|---------------------------------------------------------------
| SYSTEM FOLDER NAME
|---------------------------------------------------------------
|
| This variable must contain the name of your "system" folder.
| Include the path if the folder is not in the same  directory
| as this file.
|
| NO TRAILING SLASH!
|
*/
	$system_folder = "../CI-1.7.1";

The directory structure looks like this now
exampleuser_dir_structure

Step 5:

As my servers have mod_rewrite, I’ll remove index.php from the URL’s to give a cleaner URL and help make them more readable


http://example.com/index.php/controller/method

to


http://example.com/controller/method

I add a .htaccess file to the same directory as the index.php file, in it’s most basic form it looks like this

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]

to exclude my assets directories which I’ll add later during build I add the follow for css, images, js and robot.txt

RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

UPDATED: see post

Step 6:

As index.php has been removed from the URL’s I need to change the setting in the /application/config/config.php to reflect this and add the domain name to the config

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|	http://example.com/
|
*/
$config['base_url']	= "http://example.com/";

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";

Step 7:

To allow me to have config settings for the application being built, I create a new file in the /application/config/ directory called application.php. I’ll autoload this later so I can access the settings easily.

Here is a sample of some of the config elements I add – Application Name, Version. As the staging server is publicly visible I don’t want unauthorized users viewing it so I protect it with a simple plugin and the settings are stored here. I also have a central config for debugging so it can be switched on and off easily.

When I get chance I’ll post the details of the staging plugin for anyone interested and the code for the debug

/*
|--------------------------------------------------------------------------
| Application name
|--------------------------------------------------------------------------
*/
$config['app_name']	= "Example Application";

/*
|--------------------------------------------------------------------------
| Version
|--------------------------------------------------------------------------
*/
$config['version']	= "1.0.0";

/*
|--------------------------------------------------------------------------
| On staging server
|--------------------------------------------------------------------------
*/
$config['on_staging']	= TRUE;

/*
|--------------------------------------------------------------------------
| Staging server allowed IP's
|--------------------------------------------------------------------------
*/
$config['staging_allowed_ips']	= array('XXX.XXX.XXX.XXX');

/*
|--------------------------------------------------------------------------
| Staging redirect for non allowed IP's
|--------------------------------------------------------------------------
*/
$config['staging_redirect']	= 'http://example.com/';

/*
|--------------------------------------------------------------------------
| Debug
|--------------------------------------------------------------------------
*/
$config['debug']	= TRUE;

/* End of file application.php */
/* Location: ./application/config/application.php */

Step 8:

As each server (dev/staging/production) has it’s own database and different settings, a modification to the /application/config/database.php allows me to easily switch between them

/*
| -------------------------------------------------------------------
| DATABASE CONNECTIVITY SETTINGS
| -------------------------------------------------------------------
| This file will contain the settings needed to access your database.
|
| For complete instructions please consult the "Database Connection"
| page of the User Guide.
|
| -------------------------------------------------------------------
| EXPLANATION OF VARIABLES
| -------------------------------------------------------------------
|
|	['hostname'] The hostname of your database server.
|	['username'] The username used to connect to the database
|	['password'] The password used to connect to the database
|	['database'] The name of the database you want to connect to
|	['dbdriver'] The database type. ie: mysql.  Currently supported:
				 mysql, mysqli, postgre, odbc, mssql, sqlite, oci8
|	['dbprefix'] You can add an optional prefix, which will be added
|				 to the table name when using the  Active Record class
|	['pconnect'] TRUE/FALSE - Whether to use a persistent connection
|	['db_debug'] TRUE/FALSE - Whether database errors should be displayed.
|	['cache_on'] TRUE/FALSE - Enables/disables query caching
|	['cachedir'] The path to the folder where cache files should be stored
|	['char_set'] The character set used in communicating with the database
|	['dbcollat'] The character collation used in communicating with the database
|
| The $active_group variable lets you choose which connection group to
| make active.  By default there is only one group (the "default" group).
|
| The $active_record variables lets you determine whether or not to load
| the active record class
*/

$active_group = "staging";
$active_record = TRUE;

$db['dev']['hostname'] = "localhost";
$db['dev']['username'] = "";
$db['dev']['password'] = "";
$db['dev']['database'] = "";
$db['dev']['dbdriver'] = "mysql";
$db['dev']['dbprefix'] = "";
$db['dev']['pconnect'] = TRUE;
$db['dev']['db_debug'] = TRUE;
$db['dev']['cache_on'] = FALSE;
$db['dev']['cachedir'] = "";
$db['dev']['char_set'] = "utf8";
$db['dev']['dbcollat'] = "utf8_general_ci";

$db['staging']['hostname'] = "localhost";
$db['staging']['username'] = "";
$db['staging']['password'] = "";
$db['staging']['database'] = "";
$db['staging']['dbdriver'] = "mysql";
$db['staging']['dbprefix'] = "";
$db['staging']['pconnect'] = TRUE;
$db['staging']['db_debug'] = TRUE;
$db['staging']['cache_on'] = FALSE;
$db['staging']['cachedir'] = "";
$db['staging']['char_set'] = "utf8";
$db['staging']['dbcollat'] = "utf8_general_ci";

$db['production']['hostname'] = "localhost";
$db['production']['username'] = "";
$db['production']['password'] = "";
$db['production']['database'] = "";
$db['production']['dbdriver'] = "mysql";
$db['production']['dbprefix'] = "";
$db['production']['pconnect'] = TRUE;
$db['production']['db_debug'] = FALSE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = "";
$db['production']['char_set'] = "utf8";
$db['production']['dbcollat'] = "utf8_general_ci";

/* End of file database.php */
/* Location: ./application/config/database.php */

As you can see, this one is using staging as the active group

$active_group = "staging";

Before the application gets finally handed over to the client I remove the extra groups for security

Step 9:

Finally the autoload config – I find nearly all my projects use the database, session libraries and url helper and I also autoload the application config I created earlier.

To autoload these edit the /application/config/autoload.php

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your system/application/libraries folder.
|
| Prototype:
|
|	$autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('database', 'session');

/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|	$autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array('url');

/*
| -------------------------------------------------------------------
|  Auto-load Config files
| -------------------------------------------------------------------
| Prototype:
|
|	$autoload['config'] = array('config1', 'config2');
|
| NOTE: This item is intended for use ONLY if you have created custom
| config files.  Otherwise, leave it blank.
|
*/

$autoload['config'] = array('application');

Now I’m ready to start developing the project.

Written by daveganley

April 13th, 2009 at 1:00 pm

Posted in Codeigniter

Tagged with , , , ,

6 Responses to 'Setting up Codeigniter'

Subscribe to comments with RSS or TrackBack to 'Setting up Codeigniter'.

  1. [...] Setting up Codeigniter, as seen in If Only I Knew That! blog. Step by step guide that shows you how to setup a CodeIgniter installation and [...]

  2. Just started using CI and I already see the benefit. I had some questions about setup and you answered most of them. Two questions still remain:
    1. How do I integrate a modules folder into the system?
    2. Will you have other posts about CI development?

    Again, great article.

    Kris

    16 Jul 09 at 9:24 am

  3. Thanks

    Check out HMVC

    And yes I will be adding more posts about CI dev, just busy at the moment with a big site build on the CI framework

    admin

    16 Jul 09 at 9:31 am

  4. I would totally be interested in see that staging plugin!! Have you published it anywhere else?

    Jason

    13 Sep 09 at 10:45 am

  5. Yes, I am insteresed in the details of the staging plugin for anyone interested and the code for the debug.
    It will be nice to have a tutorial about how to use them. Thanks.

    shin

    27 Sep 09 at 4:51 pm

  6. Nice tutorial. Thanks.

Leave a Reply

Powered by CDN Rewrites