CentOSRHEL Based

How To Install CodeIgniter on CentOS Stream 10

Install CodeIgniter on CentOS Stream 10

In this tutorial, we will show you how to install CodeIgniter on CentOS Stream 10. CodeIgniter, a powerful PHP framework with a small footprint, offers an excellent toolkit for developing web applications swiftly. Its simplicity and speed make it a favorite among developers who value efficiency. CentOS Stream 10, a stable and reliable Linux distribution, provides an ideal server environment for deploying PHP-based projects. In this guide, we will walk you through the process of installing CodeIgniter on CentOS Stream 10, ensuring you have a solid foundation for your web development endeavors.

This comprehensive guide covers every essential step, from setting up the LAMP stack to configuring CodeIgniter for optimal performance. Whether you’re a seasoned developer or just starting, this tutorial provides clear, concise instructions to get your CodeIgniter application up and running on CentOS Stream 10. Let’s dive in!

Prerequisites

Before installing CodeIgniter, you need to ensure that your CentOS Stream 10 server meets the necessary requirements. This involves setting up the LAMP stack (Linux, Apache, MySQL, PHP) and configuring essential settings.

LAMP Stack Installation (Linux, Apache, MySQL, PHP)

The LAMP stack is the backbone of most PHP-based web applications. Each component plays a crucial role in serving dynamic content.

  • Update the System: Start by updating your CentOS Stream 10 system to ensure you have the latest packages:
sudo dnf update -y
  • Install Apache: Apache is the web server that will handle HTTP requests. Install it using the following command:
sudo dnf install httpd -y
  • Start and Enable Apache: Enable Apache to start on boot and start it immediately:
sudo systemctl start httpd && sudo systemctl enable httpd
  • Install MySQL: MySQL (or MariaDB) is the database management system used to store your application’s data:
sudo dnf install mariadb-server -y
  • Start and Enable MySQL: Ensure MySQL starts on boot and start the service:
sudo systemctl start mariadb && sudo systemctl enable mariadb
  • Secure MySQL: Secure your MySQL installation by running:
sudo mysql_secure_installation
  • Install PHP and Extensions: PHP is the scripting language that CodeIgniter uses. Install PHP along with essential extensions:
sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-curl php-mbstring php-xml php-json
  • Verify PHP Version: Check the installed PHP version to ensure it meets CodeIgniter’s requirements:
php -v

Setting Up a Firewall

Configuring a firewall is crucial to protect your server from unauthorized access. firewalld is the default firewall management tool on CentOS Stream 10.

  • Check Firewall Status: Verify that firewalld is running:
sudo systemctl status firewalld
  • Allow HTTP and HTTPS Traffic: Add rules to allow HTTP (port 80) and HTTPS (port 443) traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Install and Configure Composer

Composer is a dependency manager for PHP, making it easy to manage project dependencies.

  • Download Composer: Download the Composer installer:
curl -sS https://getcomposer.org/installer | php
  • Move Composer to `/usr/local/bin`: Make Composer globally accessible:
sudo mv composer.phar /usr/local/bin/composer
  • Make Composer Executable: Ensure Composer is executable:
sudo chmod +x /usr/local/bin/composer
  • Verify Installation: Check the Composer version:
composer --version

Installing CodeIgniter

With the prerequisites in place, you can now proceed to install CodeIgniter on your CentOS Stream 10 server. You can install CodeIgniter either manually or using Composer. The latter is recommended for easier updates and dependency management.

Downloading CodeIgniter

  • Using Composer:

Navigate to the directory where you want to install your CodeIgniter project (e.g., /var/www/html/):

cd /var/www/html/

Create a new CodeIgniter project using Composer:

composer create-project codeigniter4/appstarter <project-name>

Replace <project-name> with your desired project name (e.g., my-ci-app).This command downloads CodeIgniter and sets up a basic application structure.

  • Manual Download:

Alternatively, you can download the latest version of CodeIgniter from the official website. After downloading, extract the files to your desired directory (e.g., /var/www/html/<project-name>).

Directory Structure

Understanding the CodeIgniter directory structure is crucial for organizing your application. Here are the key directories:

  • application/: Contains the core application logic, including models, views, and controllers.
  • system/: Holds the core CodeIgniter files; avoid modifying these.
  • public/: The document root containing index.php and other publicly accessible files like CSS, JavaScript, and images.
  • writable/: Stores cache, logs, and uploads. Ensure this directory is writable by the web server.

Setting Up File Permissions

Proper file permissions are essential for security. Set the appropriate permissions to ensure the web server can read and write necessary files.

Change the ownership of the project directory to the Apache user:

sudo chown -R apache:apache /var/www/html/<project-name>

Set write permissions for the writable/ directory:

sudo chmod -R 755 /var/www/html/<project-name>/writable

Configuring the Base URL

The base URL tells CodeIgniter where your application is located. Configure this in the app/Config/App.php file.

Open the file with a text editor:

sudo nano /var/www/html/<project-name>/app/Config/App.php

Locate the $baseURL variable and set it to your server’s IP address or domain, followed by the project name:

public $baseURL = 'http://your_server_ip/<project-name>/';

Replace your_server_ip with your server’s IP address or domain name.

Database Configuration

Most web applications require a database to store and retrieve data. Configuring the database connection in CodeIgniter involves creating a database, a user, and setting the appropriate credentials in the configuration file.

Creating a Database

Log in to MySQL as the root user:

sudo mysql -u root -p

Enter the root password when prompted.

Create a new database for CodeIgniter:

CREATE DATABASE <database-name>;

Create a new user for the database:

CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';

Grant privileges to the user:

GRANT ALL PRIVILEGES ON <database-name>.* TO '<username>'@'localhost';

Flush privileges and exit:

FLUSH PRIVILEGES;
  EXIT;

Replace <database-name>, <username>, and <password> with your desired values.

Configuring Database Settings

Edit the app/Config/Database.php file to configure your database connection settings.

Open the file:

sudo nano /var/www/html/<project-name>/app/Config/Database.php

Update the following settings with your database credentials:

public $default = [
  'DSN' => '',
  'hostname' => 'localhost',
  'username' => '<username>',
  'password' => '<password>',
  'database' => '<database-name>',
  'dbdriver' => 'MySQLi',
  'dbprefix' => '',
  'pconnect' => false,
  'db_debug' => (ENVIRONMENT !== 'production'),
  'cache_on' => false,
  'cache_dir' => '',
  'charset' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => false,
  'compress' => false,
  'strictOn' => false,
  'failover' => [],
  'save_queries' => true,
  ];

Replace <username>, <password>, and <database-name> with the credentials you created earlier.

Running Migrations (If Applicable)

Database migrations allow you to evolve your database schema in a structured way. If your application requires initial database migrations, run them using CodeIgniter’s CLI command:

php spark migrate

This command applies any pending migrations to your database.

Configuring CodeIgniter

Configuring CodeIgniter involves setting the environment, enabling development mode, and adjusting other configuration options to suit your application’s needs.

Setting the Environment

Copy the env file to .env:

cp env .env

Edit the .env file to set the environment:

nano .env

Set the CI_ENVIRONMENT variable:

CI_ENVIRONMENT = development

This setting tells CodeIgniter to use the development configuration settings.

Enabling Development Mode

Running CodeIgniter in development mode provides helpful debugging information. Ensure CI_ENVIRONMENT is set to development in the .env file.

Other Configuration Options

CodeIgniter provides numerous configuration options in the app/Config/ directory. Here are a few key files:

  • App.php: Contains general application settings, such as the base URL, encryption key, and CSRF protection settings.
  • Routes.php: Defines application routes, mapping URLs to controllers and methods.
  • Email.php: Configures email settings for sending emails from your application.

Explore these files to customize CodeIgniter to your specific requirements.

Testing the Installation

After installing and configuring CodeIgniter, it’s essential to test the installation to ensure everything is working correctly.

Accessing CodeIgniter in a Web Browser

Open a web browser and navigate to http://your_server_ip/<project-name>/. If CodeIgniter is installed correctly, you should see the CodeIgniter welcome page.

Troubleshooting Common Issues

If you encounter issues, consider the following troubleshooting steps:

  • Check Apache Configuration: Ensure Apache is configured correctly and that the virtual host is properly set up.
  • Check PHP Installation: Verify that PHP is installed correctly and that all required extensions are enabled.
  • Check the `.env` File: Ensure that the .env file is correctly configured and that all necessary variables are set.
  • Check File Permissions: Verify that the web server has the necessary permissions to read and write files in the project directory.
  • Check CodeIgniter Logs: Examine the CodeIgniter logs in writable/logs/ for any error messages.

Security Considerations

Security is paramount when deploying web applications. Here are some security considerations to keep in mind when using CodeIgniter.

Keeping CodeIgniter Up-to-Date

Regularly update CodeIgniter to patch security vulnerabilities. Use Composer to update CodeIgniter:

composer update

Protecting Against XSS Attacks

Cross-Site Scripting (XSS) attacks involve injecting malicious scripts into web pages. Use CodeIgniter’s built-in XSS filtering to mitigate these attacks:

$this->security->xss_clean($data);

Preventing CSRF Attacks

Cross-Site Request Forgery (CSRF) attacks trick users into performing actions they didn’t intend to. Enable CSRF protection in app/Config/App.php:

public $csrfProtection = 'true';

Database Security

Protect your database from SQL injection attacks by using parameterized queries or prepared statements.

Never store plain text passwords in the database. Use password_hash() to hash passwords before storing them.

Congratulations! You have successfully installed CodeIgniter. Thanks for using this tutorial for installing CodeIgniter on your CentOS Stream 10 system. For additional help or useful information, we recommend you check the official CodeIgniter website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button