Install CodeIgniter 4 PHP Framework on Ubuntu 22.04|20.04|18.04

Posted on 25 views

CodeIgniter is an open source Application Development Framework for building websites using PHP. The goal of CodeIgniter is to enable you to develop projects much faster without writing code from scratch. It gives you a rich set of libraries for commonly needed tasks, simple interface and logical structure to access these libraries.

Install CodeIgniter PHP Framework on Ubuntu 22.04|20.04|18.04

The steps for installing the CodeIgniter PHP Framework on Ubuntu 122.04|20.04|18.04 are a bit straightforward. Only that there are below few dependencies/Prerequisites:

  • MySQL / MariaDB database server
  • Apache / Nginx web server
  • PHP

Step 1: Install PHP on Ubuntu

Start with the installation of PHP on Ubuntu 22.04|20.04|18.04 from apt repository:

sudo apt update
sudo apt install php php-cli php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-intl

If planning to use Nginx web server, you need to install php-fpm

sudo apt install php-fpm

Confirm PHP version:

$ php -v
PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Step 2: Install and Configure Database

In this section, we’ll install and configure Database server for CodeIgniter. You have an option to use MySQL or MariaDB database server.

sudo apt install mariadb-server mariadb-client

For MySQL Database server, check:

sudo apt install mysql-server mysql-client

After installing the database server, create a database and user for CodeIgniter.

Login as root user to MySQL console

$ sudo mysql -u root -p

CREATE USER 'codeigniter'@'localhost' IDENTIFIED BY 'StrongPassword';
CREATE DATABASE codeigniter;
GRANT ALL ON codeigniter.* to 'codeigniter'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 3: Install and Configure CodeIgniter

Once you have the database server installed and database created. You can proceed to install and configure CodeIgniter.

Download the latest release of CodeIgniter from Github page.

LATEST_VER=$(curl -s https://api.github.com/repos/codeigniter4/CodeIgniter4/releases/latest|grep tag_name | cut -d '"' -f 4)
wget https://github.com/codeigniter4/CodeIgniter4/archive/refs/tags/v4.1.8.tar.gz -O CodeIgniter-$LATEST_VER.tar.gz
tar xvf CodeIgniter-$LATEST_VER.tar.gz && rm -f CodeIgniter-$LATEST_VER.tar.gz
mv CodeIgniter4-*/ CodeIgniter

Edit CodeIgniter database configuration file to set database credentials:

cp CodeIgniter/env CodeIgniter/.env
vim CodeIgniter/.env

Set environemnt:

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------
CI_ENVIRONMENT = development

Set database connection:

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = codeigniter
database.default.username = codeigniter
database.default.password = StrongPassword
database.default.DBDriver = MySQLi

Next is to configure CodeIgniter base URL for accessing via your web browser.

vim CodeIgniter/.env

Set like below:

app.baseURL = 'http://code.example.com'

When done with the configurations, move the CodeIgniter folder to /srv

sudo mv CodeIgniter /srv

Step 4: Install and Configure Apache Web server

Install the apache2package by running:

sudo apt -y install apache2 libapache2-mod-php

Set proper permissions for /srv/CodeIgniter

sudo chown -R www-data:www-data /srv/CodeIgniter

Create an apache configuration file for CodeIgniter

sudo vim /etc/apache2/sites-enabled/codeigniter.conf

Add:


    ServerName code.example.com
    ServerAlias www.code.example.com
    ServerAdmin [email protected]
    DocumentRoot /srv/CodeIgniter/public
    ErrorLog /var/log/apache2/codeigniter-error_log
    CustomLog /var/log/apache2/codeigniter-access_log combined
    /srv/CodeIgniter/public>
	Require all granted
        AllowOverride All
        Options +Indexes
    

Enable rewrite module:

sudo a2enmod rewrite
sudo systemctl restart apache2.service

Restart apache2 service

sudo systemctl restart apache2

If you access http://example.com from your browser, you should get a page like below:

install-codeigniter-ubuntu-01-1024x396

This page signifies successful installation of CodeIgniter on Ubuntu 22.04|20.04|18.04

You can start your development with CodeIgniter and refer to the User guide manual for more learning.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.