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:

  1. Raspberry Pi running Linux Debian (Raspbian OS).
  2. Internet connectivity on the Raspberry Pi.
  3. 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:

  1. Open a terminal on your Raspberry Pi.
  2. Update the package list and install Apache web server:
bash
sudo apt update
sudo apt install apache2
  1. Next, install PHP along with some essential PHP extensions:
bash
sudo apt install php libapache2-mod-php php-mysql
  1. Install MySQL server and set a root password when prompted:
bash
sudo apt install mysql-server
  1. Secure your MySQL installation by running the following command:
bash
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.

  1. Install PHPMyAdmin, a graphical web-based database administration tool for MySQL:
bash
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.

  1. Once the installation is complete, enable the necessary Apache modules:
bash
sudo a2enmod rewrite
  1. Finally, restart Apache to apply the changes:
bash
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.

  1. Navigate to your CodeIgniter project’s config.php file. This file is typically located in the application/config/ directory.
  2. Set the base_url to your Raspberry Pi’s IP address or domain name. For example:
php
$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.

  1. Open the database.php file located in the application/config/ directory.
  2. Provide the necessary database credentials (host, username, password, database name) to establish a connection. For example:
php
$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:

apache
<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:

bash
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.

  1. Open the virtual host configuration file:
bash
sudo nano /etc/apache2/sites-available/myapp.conf
  1. Add the following configuration to the file:
apache
<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.

  1. Enable the virtual host:
bash
sudo a2ensite myapp.conf
  1. Restart Apache:
bash
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!

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *






Hubungi Kami

Hubungi Kami

Kami menyediakan jasa proyek Arduino, Electrical, dan IoT (Internet of Things):
- Penyediaan komponen.
- Perancangan hardware dan software.
- Pemrograman.
- Hingga deploy server.


Cocok untuk hobi, edukasi (skripsi atau tugas akhir), dan industri.
Serahkan kepada kami, Anda tinggal terima hasil jadi!



Hubungi via WhatsApp



Ikuti Kami di TikTok (Inspirasi Project IoT)



Kunjungi YouTube Kami (Tutorial Project IoT)



Temukan Kami di Facebook



Beli Komponen Di GoLite Store