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:
- PHP version 7.2 or greater
- Web server software like Apache or Nginx
- MySQL or any other database software
- Text editor software like Visual Studio Code, Sublime Text, Atom etc.
- Basic PHP and web server knowledge
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:
- Go to https://codeigniter.com/download
- On this page, you will find the current stable version of CodeIgniter (which is 4.x.x at the time of writing).
- Click the Download button to get a zip archive 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 if you don’t already have it. For Ubuntu:
$ sudo apt update
$ sudo apt install apache2
- Start Apache server:
$ sudo systemctl start apache2
- Create a virtual host configuration file like
codeigniter.conf
under/etc/apache2/sites-available/
with the following contents:
<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 virtual host:
$ sudo a2ensite codeigniter.conf
- Restart Apache for changes to take effect:
$ 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 if you don’t have it already:
$ sudo apt update
$ sudo apt install mysql-server
- Log in to MySQL console as root:
$ sudo mysql
- Create a database, say
ci_db
:
mysql> CREATE DATABASE ci_db;
- Create a new user and grant permissions:
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.
- Copy the unzipped CodeIgniter folder to Apache’s document root which is
/var/www/html/
. You can rename the folder here.
$ sudo cp -r codeigniter-4.x.x /var/www/html/codeigniter
- Open
/var/www/html/codeigniter/app/Config/App.php
and set the base URL:
public $baseURL = 'http://localhost/codeigniter/';
- Open
/var/www/html/codeigniter/app/Config/Database.php
and enter the database credentials:
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'ci_user',
'password' => 'password',
'database' => 'ci_db',
];
- Open
/var/www/html/codeigniter/app/Config/Paths.php
and set the system folder path:
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.
- Give write permission to the
system
folder:
$ sudo chmod -R 755 /var/www/html/codeigniter/system
- Give write permission to the
app/Logs
folder to create log files:
$ sudo chmod -R 755 /var/www/html/codeigniter/app/Logs
- Change ownership of the CodeIgniter root directory to the web server:
$ 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.
- Visit
http://localhost/codeigniter/public/
in your browser. This is the default starting point. - You should see the CodeIgniter welcome page with basic php info.
- To test database connectivity, create a new file
info.php
underapp/Controllers
with the following code:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class Info extends Controller {
public function index() {
$db = \Config\Database::connect();
$result = $db->query("SELECT * FROM ci_db.tables LIMIT 1");
echo 'Database connected successfully. Number of tables: ' . $result->getResultArray()[0]->TABLE_NAME;
}
}
- Visit
http://localhost/codeigniter/public/info
to see database connectivity test output.
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:
- Page not found error – Check the baseURL set in App.php and try navigating to /codeigniter/public instead of just /codeigniter. Also verify if the .htaccess file is present in the public folder.
- Database connection error – Double check database credentials set in Database.php. Try hardcoding the credentials instead of using variables.
- Permission denied errors – Run the chmod and chown commands properly for CodeIgniter folders and files.
- Apache config errors – Verify syntax of virtual host configuration. Enable it using a2ensite command. Restart Apache after any config changes.
- CodeIgniter welcome page not showing – Check for PHP errors logged in app/Logs folder. Fix syntax errors. Also clear browser cache.
- Contact your web hosting provider if Apache, MySQL or PHP is not installed/configured properly.
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. 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.