CodeIgniter

CodeIgniter

CodeIgniter is a lightweight and fast PHP framework, designed to help developers build web applications using the MVC (Model-View-Controller) architecture. CodeIgniter is renowned for its ease of use, comprehensive documentation, and high performance. This framework is ideal for developers who need a quick solution to build web applications without requiring complex configurations.

1. System Requirements

System requirements for CodeIgniter v4:

  • PHP 8.1+ with extensions: intl, mbstring, json
  • Database: MySQL/MariaDB, PostgreSQL, SQLite3, MS SQL, or Oracle Database
  • Apache or Nginx web server

Before starting deployment, prepare:

  • VPS or server with Ubuntu 24.04 OS
  • SSH access to the server
  • A domain already pointed to the server

2. Server Preparation

Update the system

sudo apt update
sudo apt upgrade -y

Install Apache

sudo apt install apache2 -y

Install MariaDB

sudo apt install mariadb-server -y

Add the PPA ondrej/php repository

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update 
sudo apt upgrade -y

Install PHP 8.3 and required extensions

sudo apt install libapache2-mod-php8.3 php8.3 php8.3-cli php8.3-common \
  php8.3-apcu php8.3-mbstring php8.3-gd php8.3-intl \
  php8.3-xml php8.3-soap php8.3-bcmath php8.3-mysql php8.3-zip \
  php8.3-curl php8.3-tidy php8.3-imagick -y

Install Composer

sudo wget https://getcomposer.org/download/latest-stable/composer.phar -O /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

3. Create Database

Log in to MariaDB

sudo mysql

Create database and user

CREATE DATABASE codeigniter;
GRANT ALL PRIVILEGES ON codeigniter.* TO 'codeigniter'@'localhost' IDENTIFIED BY 'rahasia';
FLUSH PRIVILEGES;
exit

4. Install CodeIgniter

Install CodeIgniter as a sample application to deploy.

If you already have a CodeIgniter application ready for deployment, skip to configuring the .env file.

sudo apt install unzip -y
composer create-project codeigniter4/appstarter contoh.com

Enter the directory and copy the env file

cd contoh.com
cp env .env

Open the .env file

nano .env

Set the URL and database configuration

CI_ENVIRONMENT = production
database.default.hostname = localhost
database.default.database = codeigniter
database.default.username = codeigniter
database.default.password = rahasia
database.default.DBDriver = MySQLi
database.default.DBPrefix =
database.default.port = 3306

Run migrate to create the database structure

php spark migrate

Run seeding if preparing seed data

php spark db:seed NamaSeeder

Or import from a .sql dump file

mysql -u codeigniter -p codeigniter < backup-db.sql

For production, remove development packages

composer install --no-dev

5. Apache Configuration

Move the CodeIgniter directory to /var/www

sudo mv contoh.com /var/www

Change the directory’s user-group

sudo chown -R www-data:www-data /var/www/contoh.com

Create a virtual host configuration for contoh.com

sudo nano /etc/apache2/sites-available/contoh.com.conf	

Enter the configuration

<VirtualHost *:80>
    ServerName contoh.com
    DocumentRoot /var/www/contoh.com/public
    <Directory /var/www/contoh.com/public>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/contoh.com_error.log
    CustomLog /var/log/apache2/contoh.com_access.log combined
</VirtualHost>

Enable the rewrite module, virtual host, and restart apache2

sudo a2enmod rewrite
sudo a2ensite contoh.com
sudo systemctl restart apache2	

6. Install SSL

Install Certbot

sudo apt install certbot python3-certbot-apache -y	

Request SSL certificate

sudo certbot --non-interactive \
    -m [email protected] \
    --agree-tos \
    --no-eff-email \
    --apache -d contoh.com -d www.contoh.com \
    --redirect	

CodeIgniter deployment is complete. Test access and application functionality.