How To Install CodeIgniter on AlmaLinux 9
CodeIgniter is a powerful, open-source PHP web application framework that has gained popularity among developers for its simplicity, flexibility, and robust performance. AlmaLinux 9, on the other hand, is a free, open-source Linux distribution that serves as a stable and reliable platform for hosting web applications. In this comprehensive guide, we’ll walk you through the process of installing CodeIgniter on AlmaLinux 9, providing you with a solid foundation for your web development projects.
Whether you’re a seasoned developer or just starting your journey in web development, this tutorial will equip you with the knowledge and skills needed to set up a fully functional CodeIgniter environment on AlmaLinux 9. Let’s dive in and explore the step-by-step process of bringing these two powerful technologies together.
Prerequisites
Before we begin the installation process, it’s essential to ensure that your system meets the necessary requirements and that you have the proper permissions to carry out the installation. Here’s what you’ll need:
- A server or virtual machine running AlmaLinux 9
- Root access or sudo privileges on your AlmaLinux 9 system
- A stable internet connection for downloading packages and dependencies
- Basic familiarity with the Linux command line interface
Additionally, you should have the following software packages installed on your AlmaLinux 9 system:
- PHP (version 7.4 or later)
- Apache web server
- MySQL or MariaDB database server
- Composer (PHP dependency manager)
Don’t worry if you don’t have these packages installed yet; we’ll cover the installation process for each of them in the following sections.
Updating AlmaLinux 9 System
Before proceeding with the installation of CodeIgniter and its dependencies, it’s crucial to ensure that your AlmaLinux 9 system is up to date. This step helps prevent potential conflicts and ensures that you have the latest security patches and bug fixes.
To update your AlmaLinux 9 system, open a terminal and run the following command:
sudo dnf update -y
This command will update all installed packages to their latest versions. The “-y” flag automatically answers “yes” to any prompts during the update process.
Once the update process is complete, it’s recommended to reboot your system to ensure all changes take effect:
sudo reboot
Installing PHP and Required Extensions
CodeIgniter is built on PHP, so we need to install PHP and its necessary extensions. AlmaLinux 9 comes with PHP 8.0 in its default repositories, which is suitable for CodeIgniter 4.
To install PHP and its extensions, run the following command:
sudo dnf install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json
This command installs PHP along with several commonly used extensions that CodeIgniter may require. After the installation is complete, verify the PHP version by running:
php -v
You should see output indicating the installed PHP version, which should be 8.0 or higher.
Setting Up Apache Web Server
Apache is one of the most popular web servers and works well with CodeIgniter. To install Apache on AlmaLinux 9, use the following command:
sudo dnf install httpd
After installation, start the Apache service and enable it to run at system startup:
sudo systemctl start httpd
sudo systemctl enable httpd
To verify that Apache is running correctly, open a web browser and navigate to your server’s IP address or domain name. You should see the default Apache test page.
Installing and Configuring MySQL
CodeIgniter requires a database to store and retrieve data. MySQL is a popular choice for this purpose. To install MySQL on AlmaLinux 9, run:
sudo dnf install mysql-server
Once installed, start the MySQL service and enable it to run at system startup:
sudo systemctl start mysqld
sudo systemctl enable mysqld
To secure your MySQL installation, run the security script:
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow root login remotely, and remove the test database.
Next, create a new database for your CodeIgniter project:
sudo mysql -u root -p
Enter your MySQL root password when prompted. Then, create a new database and user for CodeIgniter:
CREATE DATABASE codeigniter;
CREATE USER 'codeigniter_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON codeigniter.* TO 'codeigniter_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Remember to replace ‘your_password’ with a strong, unique password.
Downloading and Installing Composer
Composer is a dependency management tool for PHP that we’ll use to install CodeIgniter. To install Composer, run these commands:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Verify the installation by running:
composer --version
You should see the Composer version information displayed.
Installing CodeIgniter using Composer
Now that we have Composer installed, we can use it to create a new CodeIgniter project. Navigate to your desired project directory (e.g., /var/www/html/
) and run:
cd /var/www/html/
sudo composer create-project codeigniter4/appstarter codeigniter
This command creates a new CodeIgniter project in a directory named “codeigniter
“. Once the installation is complete, navigate into the project directory:
cd codeigniter
Next, we need to configure CodeIgniter. Copy the example environment file and edit it:
cp env .env
nano .env
In the .env
file, set the following variables:
CI_ENVIRONMENT = development
app.baseURL = 'http://your_domain_or_ip'
database.default.hostname = localhost
database.default.database = codeigniter
database.default.username = codeigniter_user
database.default.password = your_password
database.default.DBDriver = MySQLi
Save the file and exit the editor.
Configuring Apache Virtual Host for CodeIgniter
To serve your CodeIgniter application, we need to create an Apache virtual host. Create a new configuration file:
sudo nano /etc/httpd/conf.d/codeigniter.conf
Add the following content to the file:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/codeigniter/public
ServerName your_domain_or_ip
<Directory /var/www/html/codeigniter/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Replace “your_domain_or_ip
” with your actual domain name or server IP address. Save the file and exit the editor.
Enable the new virtual host and restart Apache:
sudo systemctl restart httpd
Setting Up File Permissions
Proper file permissions are crucial for the security and functionality of your CodeIgniter application. Set the correct permissions with these commands:
sudo chown -R apache:apache /var/www/html/codeigniter
sudo chmod -R 755 /var/www/html/codeigniter
sudo chmod -R 777 /var/www/html/codeigniter/writable
These commands ensure that Apache can read and execute files in the CodeIgniter directory, and has write access to the “writable” directory.
Testing the CodeIgniter Installation
To verify that CodeIgniter is installed and configured correctly, open a web browser and navigate to your server’s domain name or IP address. You should see the CodeIgniter welcome page.
If you encounter any issues, check the Apache error logs for more information:
sudo tail -f /var/log/httpd/error_log
Common issues include incorrect file permissions, misconfigured virtual hosts, or PHP errors. Ensure that all steps in this guide have been followed accurately.
Configuring CodeIgniter Database Connection
To test the database connection, edit the database configuration file:
nano app/Config/Database.php
Ensure that the ‘default’ array in the $default property matches the settings you specified in the .env file earlier. Save and close the file.
To test the database connection, you can create a simple controller that attempts to connect to the database. Create a new file:
nano app/Controllers/TestDb.php
Add the following content:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class TestDb extends Controller
{
public function index()
{
$db = \Config\Database::connect();
if ($db->connID) {
echo "Database connection successful!";
} else {
echo "Database connection failed.";
}
}
}
Save the file and access it through your web browser at http://your_domain_or_ip/testdb
. If you see “Database connection successful!”, your CodeIgniter installation is properly configured to connect to the database.
Best Practices for CodeIgniter Development on AlmaLinux 9
As you begin developing with CodeIgniter on AlmaLinux 9, keep these best practices in mind:
- Regularly update your AlmaLinux 9 system and PHP packages to ensure you have the latest security patches.
- Use version control (like Git) to manage your CodeIgniter project.
- Implement proper error handling and logging in your CodeIgniter application.
- Optimize your database queries and use caching when appropriate to improve performance.
- Follow CodeIgniter’s security guidelines to protect your application from common vulnerabilities.
- Regularly backup your database and application files.
Congratulations! You have successfully installed CodeIgniter. Thanks for using this tutorial for installing the CodeIgniter PHP framework on your AlmaLinux 9 system. For additional help or useful information, we recommend you check the official CodeIgniter website.