We know that the majority of developers are leaving PHP and choosing Python or JS as their language to develop applications. But we can not neglect the popularity of PHP websites. WordPress CMS, Laravel frameworks etc. made php an irresistible language in the development field. CodeIgniter on the beautiful PHP framework also have a big role in holding PHP. CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. It is much simpler and easier than Laravel.
Now we are going to install and set up a CodeIgniter application on the Ubuntu platform.
1. INSTALL APACHE2
CodeIgniter requires a web server to run. The commonly used web servers today are, Apache2 and NGINX. In this guide, we are using Apache2. You can install Apache2 with the below command.
sudo apt-get install apache2
2. INSTALL PHP AND IT’S EXTENSIONS
Since CodeIgniter is a PHP framework, PHP and some of its extensions need to be installed. The latest version of PHP is php7.2 and it is also supported by CodeIgniter 3.1. So need to install php7.2 and it’s extensions.
sudo apt-get install libapache2-mod-php7.2 php7.2-common php7.2-xml php7.2-zip php7.2-mysql unzip wget -y
3. INSTALL MYSQL
CodeIgniter also need a database server. Most using database with PHP is Mysql.
sudo apt-get install mysql-server mysql-client sudo mysql_secure_installation
When prompted, answer the questions below by following the guide.
Enter current password for root (enter for none): (Press Enter) Set root password? [Y/n]: Y New password: (Enter password for mysql DB) Re-enter new password: (Repeat password) Remove anonymous users? [Y/n]: Y Disallow root login remotely? [Y/n]: Y Remove test database and access to it? [Y/n]: Y Reload privilege tables now? [Y/n]: Y
In the latest version of MySQL, it rollbacks the support for the root user. So, you can not migrate the database with root user. So that we need to create a new user and grant all privileges to the user.
sudo mysql --user=root mysql CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION; EXIT;
4. INSTALL PHPMYADMIN(OPTIONAL)
phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySql over the Web. It can be installed using the command below. Using this tool, we can access our database graphically.
sudo apt-get install phpmyadmin
It will ask for your MySQL credentials and you have to give it properly.
We can access the phpmyadmin panel using the link below.
if it is giving an error “The requested URL /phpmyadmin was not found on this server”, do the below steps.
- Open apache2.conf file using nano editor.
sudo -H nano /etc/apache2/apache2.conf
2. Paste the following line to the end of the file.
3. Restart apache server
5. DOWNLOAD CODEIGNITER
After setting up XAMPP and running Apache & MySql in it, we have to download the CodeIgniter zip file from the official website.
We will get a zip file. Just move this file to /var/www/html and extract the zip file from there using the commands below.
cd ~ cd /Downloads sudo mv CodeIgniter-3.1.10.zip /var/www/html sudo unzip CodeIgniter-3.1.10.zip
Now we can rename the directory CodeIgniter-3.1.10 to our project name using the command
sudo mv CodeIgniter-3.1.10 my_code_igniter_app
6. SETTING PERMISSIONS FOR THE PROJECT DIRECTORY
We have to set permissions for working with this project directory since as by default the system may only allow us to change the contents in our project directory as root.
sudo chmod 777 -R /var/www/html/my_code_igniter_app
7.SETTING UP DATABASE
You have to create a database on your Mysql server which is the DB of our CodeIgniter application.
mysql -u username -p CREATE DATABASE dbname; USE dbname; EXIT;
Or If you installed phpmyadmin already in step 4, the below steps will help you in making a DB graphically
- Direct to the URL below.
Enter username and password(As a default, the username and password will be root).
- Click on the New tab
- Enter a database name
8.ADDING DATABASE DETAILS IN OUR PROJECT
Now from your project directory, direct to application/config/database.php
Here we have to add our database details created earlier inside this file.
9.RUNNING OUR APPLICATION
Now we can run our application on our browser using the link below
This will return a view as below.
Have a nice code !