Setting up the Lumen framework on Ubuntu 22.04 is a straightforward process. Lumen is a PHP micro-framework designed to build microservices and high-performance APIs. Developed by Taylor Otwell as a leaner version of Laravel, it offers the perfect starting point for developers looking to create smaller, faster services.
This guide will walk you through the steps to install and set up the Lumen framework on Ubuntu 22.04 LTS.
Prerequisites
Before you start, ensure you have the following prerequisites installed on your system:
- Ubuntu 22.04 LTS
- PHP (version 8.0 or higher)
- Composer, the PHP package manager
- MySQL or another database system (Optional, but commonly used with Lumen)
Step 1: Install PHP and Extensions
Ubuntu 22.04 ships with PHP, but you might not have it installed by default. Install PHP along with the necessary extensions using the following commands:
sudo apt update
sudo apt install php php-cli php-mbstring php-xml php-zip curl -y
Step 2: Install Composer
Composer is a dependency manager for PHP, allowing you to install and manage your PHP dependencies. Install Composer globally by running:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
This will make Composer available globally as composer. Now make the file executable.
chmod +x /usr/local/bin/composer
Step 3: Install Lumen
Now that you have PHP and Composer installed, you can install the Lumen framework. To create a new Lumen project, run:
composer create-project --prefer-dist laravel/lumen your-project-name
Replace “your-project-name” with the desired directory name for your new project.
Step 4: Serve Your Application
Navigate to your project directory:
cd your-project-name
Use the PHP built-in server to serve your application:
php -S localhost:8000 -t public
This command will start a development server at http://localhost:8000.
Step 5: Database Configuration (Optional)
If your application will use a database, you need to configure it. Open the .env file in your project directory:
nano .env
Look for the database configuration section and update the settings to match your database connection details:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password
After saving the changes, you might need to install the PHP extension for your database. For MySQL, you can install it using:
sudo apt install php-mysql
Step 6: Test Your Installation
Finally, test your Lumen installation by accessing the default route in your browser or using curl:
curl http://localhost:8000
You should receive a response from Lumen indicating it’s working correctly.
Conclusion
You have successfully set up the Lumen framework on Ubuntu 22.04 LTS. Lumen provides a great platform for developing high-performance microservices and APIs. With its ease of setup and use, you can quickly start developing your next project. Remember, this guide covers a basic setup; for production environments, consider additional configurations like securing your application, setting up a virtual host, and configuring a web server like Nginx or Apache.