Create and Run a Laravel Project
What is Laravel?
Laravel is an open-source PHP web framework created by PHP developer Taylor Otwell. It aims to provide developers with a clean and elegant toolkit to build modern web applications. Laravel offers a wealth of features such as routing, sessions, caching, authentication, etc., which simplify common web development tasks.
Key Features and Advantages of Laravel
- Simplicity and Elegance: Laravel’s syntax is simple and elegant, making the code more readable and maintainable.
- Rich Feature Set: Includes routing, authentication, session management, and caching, greatly simplifying development work.
- Powerful ORM: Eloquent ORM offers a simple ActiveRecord implementation for database operations.
- Modular Design: Easily integrates third-party libraries and extensions through the Composer package manager.
- Strong Community Support: Has a large developer community and a rich ecosystem.
Laravel helps developers quickly build high-quality web applications and APIs, suitable for projects of all sizes, from small applications to large enterprise systems.
Create and Run a Laravel Project Using ServBay
In this article, we will use the PHP environment provided by ServBay to create and run a Laravel project. We will utilize ServBay’s ‘Host’ feature to set up a web server and configure it to access the project easily.
Note: For users who previously used NGINX or Apache
ServBay uses Caddy as the default web server. For users migrating from NGINX and Apache to ServBay, there are some key changes to be aware of:
Caddy Configuration
ServBay comes with Caddy pre-installed and pre-configured for optimization and debugging. Developers just need to manage their sites through ServBay’s ‘Host’ feature without manually modifying the Caddy configuration file.
Rewrite Rules and .htaccess
In NGINX and Apache, developers usually write their own Rewrite rules and .htaccess files for URL rewriting and other configurations. However, ServBay has already configured Caddy's rules out of the box. Unless you have special requirements, you don’t need to write these rules yourself.
Learn More
For more information, refer to Rewrite and .htaccess, How to Migrate an Apache Website to ServBay, and How to Migrate an NGINX Website to ServBay.
Create a Laravel Project
TIP
ServBay recommends developers place websites in the /Applications/ServBay/www
directory for easier management.
Install Composer
ServBay comes with Composer pre-installed, no separate installation is needed.
Create a Laravel Project
Use Composer to create a new Laravel project:
bashcd /Applications/ServBay/www mkdir servbay-laravel-app cd servbay-laravel-app composer create-project --prefer-dist laravel/laravel .
1
2
3
4Enter the Project Directory
Enter the newly created Laravel project directory:
bashcd /Applications/ServBay/www/servbay-laravel-app
1
Initial Configuration
Generate Application Key
Laravel requires an application key to ensure the security of user sessions and other encrypted data. Use the Artisan command to generate it:
bashphp artisan key:generate
1Configure Environment Variables
Configure database connection information and other environment variables in the
.env
file. Make sure the following configurations are set correctly:APP_NAME=Laravel APP_ENV=local APP_KEY=base64:... APP_DEBUG=true APP_URL=https://servbay-laravel-test.local LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Configure Web Server
Use ServBay’s ‘Host’ feature to access the Laravel project through a web server. In ServBay’s ‘Host’ settings, add a new host:
- Name:
My First Laravel Dev Site
- Domain:
servbay-laravel-test.local
- Website Type:
PHP
- PHP Version: Choose
8.3
- Web Root Directory:
/Applications/ServBay/www/servbay-laravel-app/public
For detailed setup steps, refer to Adding the First Website.
Add Sample Code
Add the following code to the routes/web.php
file to output "Hello ServBay!":
Route::get('/', function () {
return 'Hello ServBay!';
});
2
3
Access the Website
Open your browser and visit https://servbay-laravel-test.local
, you should see the page output Hello ServBay!
.
NoSQL Database Example
Memcached Example
Install Memcached Extension
Memcached extension comes pre-installed in ServBay, no additional installation is needed.
Configure Memcached
Configure Memcached connection information in the
.env
file:CACHE_STORE=memcached MEMCACHED_HOST=127.0.0.1
1
2Use Memcached
Use cache in the controller:
phpuse Illuminate\Support\Facades\Cache; Route::get('/memcached', function () { Cache::put('key', 'value', 600); return Cache::get('key'); });
1
2
3
4
5
6
Redis Example
Install Redis Extension
Redis extension comes pre-installed in ServBay, no additional installation is needed.
Configure Redis
Configure Redis connection information in the
.env
file:CACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
1
2
3
4Use Redis
Use cache in the controller:
phpuse Illuminate\Support\Facades\Redis; Route::get('/redis', function () { Redis::set('key', 'value'); return Redis::get('key'); });
1
2
3
4
5
6
Relational Database Example
Create Database Structure and Migration Files
Create Migration File
Use Artisan command to create a migration file:
bashphp artisan make:migration create_accounts_table
1Edit Migration File
Find the newly created migration file in the
database/migrations
directory and edit it to define the database table structure:phppublic function up() { Schema::create('accounts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }
1
2
3
4
5
6
7
8
9Run Migration
Use the Artisan command to run the migration and create the database table:
bashphp artisan migrate
1
MySQL Example
Configure MySQL
Configure MySQL connection information in the
.env
file:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6Insert User Data
Insert user data in the controller:
phpuse Illuminate\Support\Facades\DB; Route::get('/mysql-add', function () { DB::table('accounts')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'Account added'; });
1
2
3
4
5
6
7
8
9Use MySQL
Query the database in the controller:
phpuse Illuminate\Support\Facades\DB; Route::get('/mysql', function () { $users = DB::table('accounts')->get(); return $users; });
1
2
3
4
5
6
PostgreSQL Example
Configure PostgreSQL
Configure PostgreSQL connection information in the
.env
file:DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6Insert User Data
Insert user data in the controller:
phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql-add', function () { DB::table('accounts')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'Account added'; });
1
2
3
4
5
6
7
8
9Use PostgreSQL
Query the database in the controller:
phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql', function () { $users = DB::table('accounts')->get(); return $users; });
1
2
3
4
5
6
By following these steps, you have successfully created and run a Laravel project, utilized ServBay's features to manage and access your project, and connected various databases to retrieve data.