Introduction:
CodeIgniter is a powerful PHP framework for building web applications with a focus on simplicity and performance. In this step-by-step guide, we’ll show you how to set up and run a CodeIgniter application on a Raspberry Pi server running Linux Debian. By the end of this tutorial, you’ll have a fully functional web application running on your Raspberry Pi, ready to serve web requests.
Requirements:
Before we get started, make sure you have the following requirements:
- Raspberry Pi running Linux Debian (Raspbian OS).
- Internet connectivity on the Raspberry Pi.
- Basic familiarity with the Linux command line.
Step 1: Set Up the LAMP Stack
First, we need to set up the LAMP (Linux, Apache, MySQL, PHP) stack on your Raspberry Pi. The LAMP stack provides all the essential components required to run a PHP-based web application.
Follow these steps to install Apache, PHP, and PHPMyAdmin on your Raspberry Pi:
- Open a terminal on your Raspberry Pi.
- Update the package list and install Apache web server:
sudo apt update
sudo apt install apache2
- Next, install PHP along with some essential PHP extensions:
sudo apt install php libapache2-mod-php php-mysql
- Install MySQL server and set a root password when prompted:
sudo apt install mysql-server
- Secure your MySQL installation by running the following command:
sudo mysql_secure_installation
During the process, you’ll be prompted to configure some security settings for MySQL. Follow the on-screen instructions to set a root password, remove anonymous users, disallow root login remotely, and remove the test database.
- Install PHPMyAdmin, a graphical web-based database administration tool for MySQL:
sudo apt install phpmyadmin
During the PHPMyAdmin installation, you’ll be prompted to configure some settings. Select “Apache” as the web server to configure PHPMyAdmin to work with Apache.
- Once the installation is complete, enable the necessary Apache modules:
sudo a2enmod rewrite
- Finally, restart Apache to apply the changes:
sudo service apache2 restart
Step 2: Configure CodeIgniter’s Base URL
With the LAMP stack installed and running, let’s configure your CodeIgniter application to work with your Raspberry Pi.
- Navigate to your CodeIgniter project’s
config.php
file. This file is typically located in theapplication/config/
directory. - Set the
base_url
to your Raspberry Pi’s IP address or domain name. For example:
$config['base_url'] = 'http://192.168.1.52/';
Make sure to replace 192.168.1.52
with your Raspberry Pi’s actual IP address or domain name.
Step 3: Configure the Database
Your CodeIgniter application may require a database to store and retrieve data. Let’s configure your application to connect to a MySQL database.
- Open the
database.php
file located in theapplication/config/
directory. - Provide the necessary database credentials (host, username, password, database name) to establish a connection. For example:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'your_username',
'password' => 'your_password',
'database' => 'your_database',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Replace 'your_username'
, 'your_password'
, and 'your_database'
with your actual MySQL database credentials.
Step 4: Create a .htaccess File
CodeIgniter uses .htaccess
files to enable clean URLs and remove the need for index.php
in the URLs. Let’s create a .htaccess
file in the root directory of your CodeIgniter project with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase / # Remove index.php from the URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Ensure that the Apache mod_rewrite
module is enabled by running the following commands:
sudo a2enmod rewrite
sudo service apache2 restart
Step 5: Create a Virtual Host
To access your CodeIgniter application using a specific domain name, we’ll create a virtual host.
- Open the virtual host configuration file:
sudo nano /etc/apache2/sites-available/myapp.conf
- Add the following configuration to the file:
<VirtualHost *:80>
ServerAdmin webmaster@192.168.1.52
ServerName 192.168.1.52
DocumentRoot /var/www/html/monitoring ErrorLog ${APACHE_LOG_DIR}/monitoring_error.log
CustomLog ${APACHE_LOG_DIR}/monitoring_access.log combined
<Directory /var/www/html/monitoring>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Make sure to replace 192.168.1.52
with your Raspberry Pi’s actual IP address or domain name. Also, ensure that /var/www/html/monitoring
points to the directory where your CodeIgniter project is located.
- Enable the virtual host:
sudo a2ensite myapp.conf
- Restart Apache:
sudo service apache2 restart
Conclusion:
You have now successfully set up and configured a CodeIgniter web application on your Raspberry Pi server running Linux Debian. Your application is accessible either through the IP address or the domain name specified in the virtual host configuration. This opens up a world of possibilities for you to build and deploy web applications on your Raspberry Pi, taking advantage of its capabilities as a low-cost and energy-efficient server. Happy coding!