How to Install CodeIgniter on Linux

Posted on

How to Install CodeIgniter on Linux

How to Install CodeIgniter on Linux

CodeIgniter is an open-source, lightweight PHP framework that provides a simple and elegant toolkit for developing web applications. Installing CodeIgniter on your system is quick and easy. In this comprehensive guide, we will walk through the CodeIgniter installation process step-by-step. We will cover downloading and unzipping CodeIgniter, configuring a web server, setting up a database, connecting CodeIgniter to the database, and testing that everything is working properly. Whether you are new to CodeIgniter or looking to set up a development environment, this tutorial has you covered. Let’s get started!

Prerequisites

Before installing CodeIgniter, you need to have the following set up on your system:

  • A Linux operating system (e.g., Ubuntu, Debian, CentOS).
  • PHP 7.2 or newer installed.
  • A web server (e.g., Apache, Nginx).
  • A database server (e.g., MySQL, MariaDB, PostgreSQL).
  • Basic knowledge of the command line.

PHP especially needs to be installed and configured properly to work with CodeIgniter. Also, you need access to create a new database through MySQL or any other DB software. Familiarity with basic PHP syntax and experience setting up a web server will help with understanding the CodeIgniter configuration.

Step 1 – Download CodeIgniter

First, we need to download the latest version of CodeIgniter from its official website. Here are the steps:

  1. Visit the official CodeIgniter website: https://codeigniter.com/download
  2. Download the latest version of CodeIgniter.

Alternatively, you can also download CodeIgniter by clicking on the GitHub link given on the website. This will take you to the CodeIgniter4 repository page. Here you can download the zip file for the latest commit.

After downloading, unzip the archive and you will find the CodeIgniter files in a folder.

Step 2 – Set Up a Web Server

Now we need to set up a web server that will serve our CodeIgniter application. For this guide, we will use Apache, but you can use any web server like Nginx instead. Here are the steps to set up an Apache web server:

  1. Install Apache:
$ sudo apt update
$ sudo apt install apache2
  1. Start Apache:
$ sudo systemctl start apache2
  1. Create a Virtual Host Configuration File: Create a new configuration file for your CodeIgniter application. For example, codeigniter.conf in /etc/apache2/sites-available/:
<VirtualHost *:80>
    ServerName localhost
    ServerAlias localhost
    DocumentRoot /var/www/html/codeigniter
    <Directory /var/www/html/codeigniter>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

This sets up a virtual host named localhost pointing to the CodeIgniter installation directory.

  1. Enable the Site:
$ sudo a2ensite codeigniter.conf
  1. Restart Apache:
$ sudo systemctl restart apache2

Apache web server is now ready to serve the CodeIgniter application.

Step 3 – Set Up a Database

CodeIgniter requires a database to store application data like users, posts, products etc. Let’s create a MySQL database for our app.

  1. Install MySQL Server:
$ sudo apt update
$ sudo apt install mysql-server
  1. Access MySQL Shell:
$ sudo mysql
  1. Create a Database:
mysql> CREATE DATABASE ci_db;
  1. Create a User and Grant Privileges:
mysql>  CREATE USER 'ci_user'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL ON ci_db.* TO 'ci_user'@'localhost';

This creates a database named ci_db and a user ci_user with full privileges. Remember the username, password, and database name as we need to configure them with CodeIgniter later.

Step 4 – Configure CodeIgniter

Now we have to configure CodeIgniter to connect to the database and point it to the right directories.

  1. Move CodeIgniter Files: Move the extracted CodeIgniter files to your web server’s document root.
$ sudo cp -r codeigniter-4.x.x /var/www/html/codeigniter
  1. Set the Base URL: Open the app/Config/App.php file and set the baseURL property:
public $baseURL = 'http://localhost/codeigniter/';
  1. Configure the Database Connection: Open the app/Config/Database.php file and configure the database settings:
public $default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'ci_user',
    'password' => 'password',
    'database' => 'ci_db',
];
  1. Set the System Directory: Adjust the $systemDirectory variable in app/Config/Paths.php if needed.
public $systemDirectory = __DIR__ . '/../system';

That completes the basic CodeIgniter configuration.

Step 5 – Set Up Permissions

For security, we need to set the right permissions on the CodeIgniter files and folders.

  1. Set Permissions on the System Directory:
$ sudo chmod -R 755 /var/www/html/codeigniter/system
  1. Set Permissions on the Logs Directory:
$ sudo chmod -R 755 /var/www/html/codeigniter/app/Logs
  1. Set Ownership:
$ sudo chown -R www-data:www-data /var/www/html/codeigniter

In Apache, www-data is the default user and group. Change it accordingly for other web servers.

Step 6 – Test CodeIgniter

Time to test out our CodeIgniter installation.

  1. Create a Controller: Create a simple controller to test the database connection. For example, app/Controllers/Info.php:
<?php
namespace AppControllers;
use CodeIgniterController;
class Info extends Controller {
    public function index() {

        $db = ConfigDatabase::connect();
        $result = $db->query("SELECT * FROM information_schema.tables LIMIT 1");
        $row = $result->getRow();
        echo 'Database connected successfully. First table name: ' . $row->TABLE_NAME;
    }
}
  1. Access the Controller: Open your browser and navigate to http://localhost/codeigniter/info.

If you see the table name printed, CodeIgniter is configured properly and connected to the database.

Troubleshooting

Here are some common issues faced during installation and how to fix them:

  • "Unable to connect to the database": Double-check the database credentials in app/Config/Database.php. Ensure the database server is running and accessible.
  • "404 Not Found": Verify the Apache virtual host configuration and ensure AllowOverride All is set in the <Directory> block. Also, make sure the .htaccess file is present in the CodeIgniter root directory.
  • "Call to undefined function mysqli_connect()": Ensure the php-mysqli extension is installed and enabled.

Alternative Solutions

While the above method details a standard approach, there are alternative ways to install and configure CodeIgniter on a Linux system. Here are two different approaches:

1. Using Composer

Composer is a dependency management tool for PHP. It simplifies the process of installing and managing libraries and frameworks like CodeIgniter.

Explanation: Using Composer automates the download and setup process. It also allows for easy updates and management of dependencies. This method is generally preferred for larger projects.

Steps:

  1. Install Composer: If you don’t have Composer installed, download and install it following the instructions on the official Composer website (https://getcomposer.org/).

  2. Create a Project Directory: Create a directory for your CodeIgniter project.

mkdir my-codeigniter-app
cd my-codeigniter-app
  1. Install CodeIgniter using Composer:
composer create-project codeigniter4/appstarter .

This command downloads CodeIgniter 4 and sets up a basic application structure in your current directory.

  1. Configure the Web Server: Set up your web server (Apache or Nginx) to point to the public directory within your project directory. The virtual host configuration would be similar to the one described in the original article, but the DocumentRoot would point to /var/www/html/my-codeigniter-app/public.

  2. Configure the Environment: Copy .env.example to .env and adjust settings like app.baseURL and database credentials.

  3. Test the Installation: Access your application through the browser (e.g., http://localhost/my-codeigniter-app/public).

Code Example (for .env file):

CI_ENVIRONMENT = development

app.baseURL = 'http://localhost/my-codeigniter-app/public'

database.default.hostname = localhost
database.default.username = ci_user
database.default.password = password
database.default.database = ci_db

2. Using Docker

Docker provides a containerization platform, allowing you to run applications in isolated environments.

Explanation: Using Docker creates a consistent and reproducible environment for your CodeIgniter application. This eliminates inconsistencies between development, testing, and production environments.

Steps:

  1. Install Docker: Install Docker and Docker Compose on your Linux system. Instructions can be found on the official Docker website (https://docs.docker.com/get-docker/).

  2. Create a Dockerfile: Create a Dockerfile in your project directory.

FROM php:7.4-apache

# Install extensions
RUN apt-get update && apt-get install -y 
    libfreetype6-dev 
    libjpeg62-turbo-dev 
    libpng-dev 
    mysql-client 
    && docker-php-ext-configure gd --with-freetype --with-jpeg 
    && docker-php-ext-install -j$(nproc) gd mysqli pdo pdo_mysql

# Enable Apache modules
RUN a2enmod rewrite

# Set working directory
WORKDIR /var/www/html

# Copy CodeIgniter files
COPY . .

# Set permissions
RUN chown -R www-data:www-data /var/www/html

# Expose port
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]
  1. Create a docker-compose.yml file: Create a docker-compose.yml file to define the services for your application (e.g., web server and database).
version: "3.7"
services:
  web:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: ci_db
      MYSQL_USER: ci_user
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"
  1. Build and Run the Containers:
docker-compose up -d

This command builds the Docker image and starts the containers in detached mode.

  1. Access the Application: Open your browser and navigate to http://localhost:8000.

Code Example (Dockerfile): The Dockerfile example provides a complete setup for a CodeIgniter environment, including PHP extensions and Apache configuration. The docker-compose.yml sets up both the web server and the database.

Conclusion

That concludes the CodeIgniter installation tutorial! We downloaded the latest CodeIgniter, set up Apache web server, created a MySQL database, configured system paths and database credentials, set file permissions and tested the output. We also looked at alternative installation methods using Composer and Docker. CodeIgniter is now ready for app development. With its simple syntax, rich set of libraries and clear documentation, you can build powerful PHP apps quickly using CodeIgniter.

Leave a Reply

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