How to Run CodeIgniter Project in Localhost

How to Run CodeIgniter Project in Localhost

Running a CodeIgniter project on your local machine is a crucial step for web developers who wish to develop, test, and debug their applications before deploying them to a live server. CodeIgniter, a powerful PHP framework, simplifies the development process, making it easier to create dynamic websites quickly. Here’s a step-by-step guide on how to set up and run a CodeIgniter project on your localhost.

Prerequisites

Before diving into the setup, ensure you have the following installed on your local machine:

  1. Web Server: Apache or Nginx.
  2. PHP: Version 7.2 or newer.
  3. Database Server: MySQL or MariaDB.
  4. CodeIgniter Framework: Download from the official website.
  5. Composer: Dependency manager for PHP.

Step 1: Install a Local Server Environment

To run PHP scripts, you need a local server environment. XAMPP and WAMP are popular choices that come bundled with Apache, MySQL, and PHP.

Using XAMPP:

  • Download XAMPP from the official website.
  • Install and start Apache and MySQL from the XAMPP control panel.

Step 2: Download CodeIgniter

Download the latest version of CodeIgniter from the official CodeIgniter website. Extract the downloaded file to your preferred location, typically in the htdocs folder (XAMPP) or www folder (WAMP).

Step 3: Configure the Database

Set up your database in MySQL:

  1. Open phpMyAdmin (http://localhost/phpmyadmin).
  2. Create a new database for your CodeIgniter project.

Configure CodeIgniter to use this database:

  1. Navigate to the application/config folder.
  2. Open the database.php file.
  3. Enter your database details:

Step 4: Configure Base URL

Configure the base URL for your project:

  1. Navigate to application/config.
  2. Open the config.php file.
  3. Set the base URL:

$config[‘base_url’] = ‘http://localhost/your_project_folder/’;

Step 5: Set Up Apache Virtual Host (Optional)

Setting up a virtual host can make it easier to access your project. Edit the Apache configuration file:

  1. Open the httpd-vhosts.conf file (found in the apache/conf/extra directory for XAMPP).
  2. Add the following configuration:

Edit your hosts file (C:\Windows\System32\drivers\etc\hosts) and add:

127.0.0.1 your_project.localhost

Step 6: Start the Development Server

Open your browser and navigate to http://localhost/your_project_folder/ or http://your_project.localhost/ if you set up a virtual host. You should see the default CodeIgniter welcome page.

Step 7: Test Your Setup

To verify that everything is working correctly, create a simple controller and view:

  1. Create a file named Welcome.php in the application/controllers folder:

<?php

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class Welcome extends CI_Controller {

    public function index()

    {

        $this->load->view(‘welcome_message’);

    }

}

  1. Create a view named welcome_message.php in the application/views folder with some sample HTML content.

Navigate to http://localhost/your_project_folder/index.php/welcome to see your custom welcome message.

Conclusion

By following these steps, you can successfully set up and run a CodeIgniter project on your local machine. This setup allows you to develop and test your applications efficiently before deploying them to a live server. Happy coding!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *