Wednesday, December 25, 2024
Google search engine
HomeUncategorisedInstall and Use CodeIgniter 4 on Debian 11 / Debian 10

Install and Use CodeIgniter 4 on Debian 11 / Debian 10

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

CodeIgniter is a free and open-source used as a development framework for PHP websites. This framework is so powerful that you can build a full-featured web application in the quickest way possible. This is made possible through a rich set of libraries available for common functions such as uploading files, managing sessions, email sending e.t.c.

The latest release version, CodeIgniter 4 offers the following features:

  • Simple, Clear and easy to understand documentation.
  • CI is an MVC framework with a small footprint.
  • No need to learn about advanced concepts like PEAR.
  • CI is not restricted to any naming convention or coding rules.
  • Support for MySQL (5.1+) via the MySQLi driver, PostgreSQL via the Postgre driver and SqLite3 via the SQLite3 drive
  • Can be used with command-line programs
  • PHP 7.2 functionality and compatibility.
  • It provides for a public folder, intended as the document root for your app

Install and use CodeIgniter 4 PHP Framework on Debian 11|10 using the steps below:

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

Getting Started

There are several dependencies required by CodeIgniter 4 namely;

  • PHP
  • Database – MySQL / MariaDB database server
  • Web server – Apache / Nginx web server

We will begin by installing them as below:

Step 1 – Install PHP on Debian 11|10

Begin by installing the PHP dependency for CodeIgniter4 from the APT repository as below.

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 vim 

Verify the PHP installation.

$ php -v
PHP 7.4.25 (cli) (built: Oct 23 2021 21:53:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.25, Copyright (c), by Zend Technologies

Step 2 – Install and Configure Database

We will now create a database for CodeIgniter. In this guide, we will use the MariaDB/MySQL database server that can be installed as below.

  • MariaDB
sudo apt install mariadb-server mariadb-client
  • MySQL

Use the dedicated guide below

Once installed, login to the shell using the root user, with the default password root.

sudo mysql -u root -p

Create a database for CodeIgniter as below.

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

Step 3 – Install CodeIgniter 4 PHP Framework on Debian 11|10

There are two methods one can use to install CodeIgniter 4 PHP Framework on Debian 11|10, namely;

  • Using Composer
  • Manually

Option 1 – Install CodeIgniter 4 PHP Framework on Debian 11|10 using Composer

Composer can be used to install CodeIgniter 4 PHP in several ways. First, composer on your system.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Move the composer.phar file to your path.

sudo mv composer.phar /usr/local/bin/composer

Run composer.

composer

Sample output:

Install and Use CodeIgniter 4 PHP Framework on Debian

For App Starters, who wish to create a new CodeIgniter4 based project with the name CodeIgniter.

composer create-project codeigniter4/appstarter CodeIgniter

Sample Output:

Install and Use CodeIgniter 4 PHP Framework on Debian 1

With this method, update Codeigniter to the latest release.

composer update

Now you can proceed and configure CodeIgniter4 by editing files in the CodeIgniter directory.

Option 2 – Install CodeIgniter 4 PHP Framework on Debian 11|10 Manually.

This is an alternative method to install CodeIgniter 4 PHP Framework on Debian 11|10. Download CodeIgniter4 from the official CodeIgniter release page. You can also use the below command to pull the CodeIgniter4 archive.

Install the required packages.

sudo apt install curl wget

Once installed, proceed as below

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/${LATEST_VER}.tar.gz -O CodeIgniter-${LATEST_VER}.tar.gz

Once downloaded, extract and move the file to the CodeIgniter directory as below.

tar xvf CodeIgniter-${LATEST_VER}.tar.gz && rm -f CodeIgniter-${LATEST_VER}.tar.gz
mv CodeIgniter4-*/ CodeIgniter

Step 4 – Configure CodeIgniter4 on Debian 11|10

Configure CodeIgniter4 by editing the .env file.

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

Begin by setting the environment:

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

Now add the database details.

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

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

Also, configure the URL to access the CodeIgniter4 web UI.

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

Replace codeigniter.example.com with your actual domain name.

Once adjustments have been made, move the file to /srv as below.

sudo mv CodeIgniter /srv

Step 5 – Install and Configure Apache Web server

We will use Apache as the webserver for CodeIgniter4 on our system. Install Apache using the command:

sudo apt -y install apache2 libapache2-mod-php

Set the correct permissions for the /srv/CodeIgniter directory.

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

Create the virtual host file for CodeIgniter4.

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

in the file, add the lines below, while replacing codeigniter.example.com with your real domain name.

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

Save the file and enable the rewrite module.

sudo a2enmod rewrite

Move the default apache file to a backup file.

sudo mv /etc/apache2/sites-enabled/000-default.conf /etc/apache2/sites-enabled/000-default.conf.bak

Now restart the Apache service.

sudo systemctl restart apache2

Step 6 – Access the CodeIgniter4 Web UI

With Apache configured, we can access the CodeIgniter4 web interface using the set URL http://example.com. You should be able to see the below page.

Install and Use CodeIgniter 4 PHP Framework on Debian 2

Step 7 – Run an Application with CodeIgniter 4

CodeIgniter 4 has an inbuilt local development server that can be launched using the php spark servecommand in the main directory. But since we have the server already running, and displaying the welcome message page, we will create a simple app as below.

First, create the app in the Views folder as below

sudo vim /srv/CodeIgniter/app/Views/hello_world.php

In the file, add your code as below.

<html>
 <head>  
        <title>Hello World</title>  
    </head>  
<body>

    <h1>Hello World form Nairobi, Kenya!</h1>

</body>
</html>

Next, create the controller. We will edit the existing controller for our Welcome message to display our application

sudo vim /srv/CodeIgniter/app/Controllers/Home.php

Replace welcome_message with the created application name hello_world

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return view('hello_world');
    }
}

Save the file and reload the page.

Install and Use CodeIgniter 4 PHP Framework on Debian 3

Conclusion.

That is it!

We have successfully installed and configured CodeIgniter 4 PHP Framework on Debian 11|10. Furthermore, we have demonstrated how to create a simple web page using the CodeIgniter 4 PHP Framework. I hope this guide was valuable. In case you have any feedback, let us know in the comments.

See more:

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments