Deploying a CodeIgniter App
CodeIgniter is a lightweight and powerful PHP framework for building web applications. It is known for its speed, simplicity, and small footprint, making it ideal for developers who want to quickly create robust PHP apps with minimal configuration.
This guide explains how to deploy a CodeIgniter application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.
Prerequisites
- PHP 8.1+
- Composer installed (for CodeIgniter 4)
- Git and GitHub account
- Klutch.sh account
Getting Started: Install CodeIgniter
- Create a new CodeIgniter 4 app using Composer:
Terminal window composer create-project codeigniter4/appstarter my-codeigniter-appcd my-codeigniter-app - Start the development server:
Your app should be running at http://localhost:8080.
Terminal window php spark serve
Sample Code (app/Controllers/Home.php)
Add a simple controller to your CodeIgniter project:
<?phpnamespace App\Controllers;
use CodeIgniter\Controller;
class Home extends Controller{ public function index() { return "Hello from CodeIgniter on Klutch.sh!"; }}
Deploying Without a Dockerfile
- Push your CodeIgniter app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- Create a new app:
- Select your CodeIgniter GitHub repository and branch
- Set the port to route traffic (usually 8080 for CodeIgniter)
- Choose region, compute, number of instances, and add any environment variables
- Add a start command in your app settings:
(Klutch.sh will provide the
Terminal window php spark serve --host 0.0.0.0 --port $PORTPORT
environment variable.) - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official PHP imageFROM php:8.1-cli# Install ComposerCOPY --from=composer:2 /usr/bin/composer /usr/bin/composer# Set working directoryWORKDIR /app# Copy app sourceCOPY . .# Install dependenciesRUN composer install --no-interaction --optimize-autoloader# Expose port (match your CodeIgniter app)EXPOSE 8080# Start the appCMD ["php", "spark", "serve", "--host", "0.0.0.0", "--port", "$PORT"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.
Note: Your CodeIgniter app should always listen on the PORT
environment variable as shown above.
Resources
Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.