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:
- Visit the official CodeIgniter website: https://codeigniter.com/download
- 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:
- Install Apache:
$ sudo apt update
$ sudo apt install apache2
- Start Apache:
$ sudo systemctl start apache2
- 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.
- Enable the Site:
$ sudo a2ensite codeigniter.conf
- 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.
- Install MySQL Server:
$ sudo apt update
$ sudo apt install mysql-server
- Access MySQL Shell:
$ sudo mysql
- Create a Database:
mysql> CREATE DATABASE ci_db;
- 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.
- 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
- Set the Base URL: Open the
app/Config/App.php
file and set thebaseURL
property:
public $baseURL = 'http://localhost/codeigniter/';
- 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',
];
- Set the System Directory: Adjust the
$systemDirectory
variable inapp/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.
- Set Permissions on the System Directory:
$ sudo chmod -R 755 /var/www/html/codeigniter/system
- Set Permissions on the Logs Directory:
$ sudo chmod -R 755 /var/www/html/codeigniter/app/Logs
- 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.
- 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;
}
}
- 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 thephp-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:
-
Install Composer: If you don’t have Composer installed, download and install it following the instructions on the official Composer website (https://getcomposer.org/).
-
Create a Project Directory: Create a directory for your CodeIgniter project.
mkdir my-codeigniter-app
cd my-codeigniter-app
- 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.
-
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 theDocumentRoot
would point to/var/www/html/my-codeigniter-app/public
. -
Configure the Environment: Copy
.env.example
to.env
and adjust settings likeapp.baseURL
and database credentials. -
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:
-
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/).
-
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"]
- Create a
docker-compose.yml
file: Create adocker-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"
- Build and Run the Containers:
docker-compose up -d
This command builds the Docker image and starts the containers in detached mode.
- 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.