Skip to main content

How To Install CodeIgniter on an Ubuntu 12.04 Cloud Server

CodeIgniter: Getting Started With a Simple Example

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:
<your_domain>/codeigniter

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.

Configuration


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
</Directory>

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:
http://www.example.com/welcome/test/3 

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

Comments

Popular posts from this blog

Basic naming idea in codeigniter

File Naming Class files must be named in a Ucfirst-like manner, while any other file name (configurations, views, generic scripts, etc.) should be in all lowercase. INCORRECT: somelibrary.php someLibrary.php SOMELIBRARY.php Some_Library.php Application_config.php Application_Config.php applicationConfig.php CORRECT : Somelibrary.php Some_library.php applicationconfig.php application_config.php Furthermore, class file names should match the name of the class itself. For example, if you have a class named  Myclass , then its filename must be Myclass.php. Class and Method Naming Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. INCORRECT : class superclass class SuperClass CORRECT : class Super_class class Super_class {           public function __construct ()        ...

CodeIgniter: Getting Started With a Simple Example

CodeIgniter: Getting Started With a Simple Example Introduction CodeIgniter is a powerful PHP framework that can help you greatly speed up the development of your web applications. It is has a small performance footprint due to the modular approach to loading its libraries and does a great job separating logic from presentation by using a Model-View-Controller (MVC) dynamic. In this tutorial, you will learn how to create a very simple application that displays content from the database. It is not meant to provide the solution to building your own CMS, but rather to illustrate how CodeIgniter can be used. Additionally, it seeks to put in practice some of the lessons learned in the previous article and assumes you already have CodeIgniter installed on your development environment and have configured it as described in the first tutorial. The Plan In this article, you will see how to use some of CodeIgniter’s classes and functions to query a database table c...

How to setup CodeIgniter in your localhost with WAMP server

I've been developing a couple of website using codeIgniter framework. I only learned about this framework early this year 2011. Basically, I've tried installing codeIgniter from localhost and on the host server itself. How to make codeigniter work is very simple and easy. Below are the steps to kick start your first website using codeigniter. 1. Installing WAMP First and foremost, before you can have your website running on your localhost you should get a copy of WAMP(Windows Apache MySQL and PHP). It is where you can manage your server settings and Apache/MySQL services. So start downloading Wampserver now! As you can see there are 2 buttons to download between 32bits and 64bits that would be basing on your system type. To check what OS type is running click Windows logo on your keyboard + Pause/Break and a window will appear. After download, double click the file and follow the instructions given. Everything will be installed automatically  with the ...