Creating and Running a Laravel Project in ServBay
Overview
ServBay is a powerful local web development environment that supports both macOS and Windows. It comes bundled with a variety of popular software packages including PHP, Node.js, Python, Go, Java, databases (MySQL, PostgreSQL, MongoDB), caching services (Redis, Memcached), and web servers (Caddy, Nginx, Apache).
This document will guide you through quickly creating, configuring, and running a Laravel project in the ServBay environment. We’ll leverage ServBay’s built-in PHP environment, Composer package manager, and robust site (formerly “host”) management features to make the process simple and efficient.
What Is Laravel?
Laravel is a widely-used open-source PHP web application framework created by Taylor Otwell. It follows the MVC (Model-View-Controller) pattern and offers a variety of out-of-the-box features designed to simplify common web development tasks such as authentication, routing, session management, caching, and database operations. Known for its elegant syntax, extensive feature set, and strong community support, Laravel is an excellent choice for building modern and maintainable web applications.
Key Features and Advantages of Laravel
- Elegant Syntax: Clean, expressive code enhances productivity and improves readability.
- Eloquent ORM: Provides a robust ActiveRecord implementation for simple and intuitive database interactions.
- Artisan Console: Includes helpful CLI tools to run migrations, generate code, execute tests, and more.
- Blade Templating Engine: Concise templating syntax makes building dynamic views easy.
- Rich Ecosystem: Large number of official and third-party packages easily integrated via Composer.
- Strong Community Support: Vibrant community offers plenty of resources, tutorials, and solutions.
Advantages of Developing Laravel Projects with ServBay
ServBay offers significant benefits for Laravel developers:
- Integrated Environment: ServBay comes pre-installed with multiple PHP versions, Composer, and popular database/caching services—no need for separate installation or configuration.
- Easy Management: Switch PHP versions, manage site configurations, and start/stop services directly through the ServBay GUI.
- Pre-configured Web Server: ServBay uses Caddy by default and provides optimized configuration for common PHP frameworks (including Laravel), simplifying web server setup.
- HTTPS Support: ServBay automatically issues SSL certificates via ServBay User CA for
.local
domains and enables HTTPS by default, providing secure and production-like local environments.
Creating a Laravel Project
ServBay recommends storing your website projects in the following directories for tidy organization and unified management:
- macOS:
/Applications/ServBay/www
- Windows:
C:\ServBay\www
Verify Composer Installation
ServBay includes Composer by default—no separate installation needed. Verify Composer availability in your terminal:
bashcomposer --version
1If the command executes and displays the version, Composer is ready to use.
Create a New Laravel Project
Open your terminal, navigate to ServBay’s recommended site directory, and use Composer to create a new Laravel project named
servbay-laravel-app
:macOS:
bashcd /Applications/ServBay/www # Create project directory mkdir servbay-laravel-app # Enter project directory cd servbay-laravel-app # Use Composer to create the Laravel project in the current directory composer create-project --prefer-dist laravel/laravel .
1
2
3
4
5
6
7Windows:
cmdcd C:\ServBay\www # Create project directory mkdir servbay-laravel-app # Enter project directory cd servbay-laravel-app # Use Composer to create the Laravel project in the current directory composer create-project --prefer-dist laravel/laravel .
1
2
3
4
5
6
7Composer will download and install Laravel and its dependencies.
Enter the Project Directory
Make sure your terminal is in the root directory of your newly created Laravel project:
macOS:
bashcd /Applications/ServBay/www/servbay-laravel-app
1Windows:
cmdcd C:\ServBay\www\servbay-laravel-app
1
Initial Configuration
Generate Application Key
Laravel uses an application key to secure sessions and encrypted data. In the project root, run this Artisan command to generate the key:
Project root path:
- macOS:
/Applications/ServBay/www/servbay-laravel-app
- Windows:
C:\ServBay\www\servbay-laravel-app
bashphp artisan key:generate
1This command creates and sets the
APP_KEY
value in your.env
file.- macOS:
Configure Environment Variables (
.env
)Laravel uses the
.env
file for managing environment variables such as database connections and application URLs. Open the.env
file in your project root and configure it as needed. Ensure the following basic settings are correct:dotenvAPP_NAME=ServBay Laravel Demo APP_ENV=local APP_KEY=base64:... # This value is generated by php artisan key:generate APP_DEBUG=true APP_URL=https://servbay-laravel-test.local LOG_CHANNEL=stack # Example Database Configuration (MySQL) DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app # Desired database name DB_USERNAME=root # ServBay default DB user DB_PASSWORD=password # ServBay default DB password # Example Caching/Queue Configuration (Redis) CACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null # ServBay default Redis has no password REDIS_PORT=6379 # Example Caching/Queue Configuration (Memcached) # CACHE_STORE=memcached # MEMCACHED_HOST=127.0.0.1 # MEMCACHED_PORT=11211
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26Note: You may need to adjust
DB_*
,REDIS_*
, orMEMCACHED_*
settings according to your actual services and databases. Default usernames and passwords are visible in the ServBay control panel.
Configuring the Web Server (Adding a Site in ServBay)
Use ServBay’s site management feature to configure the web server and point your domain to the public
directory of your Laravel project.
Open ServBay Control Panel
Launch ServBay and open the control panel.
Add a New Site
Navigate to the “Site” section of the ServBay control panel. Click the add button (commonly a
+
icon or similar) to add a new site.Fill in Site Information
Fill out your site configuration as follows:
Name:
My First Laravel Dev Site
(a name for easy identification)Domain:
servbay-laravel-test.local
(the domain for browser access)Site Type:
PHP
PHP Version: Select a PHP version compatible with your Laravel version, e.g.,
8.3
.Site Root:
- macOS:
/Applications/ServBay/www/servbay-laravel-app/public
- Windows:
C:\ServBay\www\servbay-laravel-app\public
(Important: Must point to the
public
subdirectory inside your Laravel project root)- macOS:
For more detailed steps and options, refer to ServBay’s official documentation, especially the “Adding Your First Website” guide (look for the English version of the link).
Save and Apply Changes
Save your site configuration. ServBay will automatically update the web server settings (Caddy by default). If prompted, restart the web server as instructed.
Verifying Access
Your Laravel project should now be accessible via your configured domain.
Open Your Browser
Enter your configured domain, e.g.,
https://servbay-laravel-test.local
.Check the Result
If configured correctly, you should see Laravel’s welcome page.
Add a Simple Example Route
To quickly verify the setup, you can add a simple route to output "Hello ServBay!" in the routes/web.php
file.
Find the routes/web.php
file in your project directory and update or add the following code:
php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return 'Hello ServBay!'; // Edit or add this line
});
// ... other routes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
After saving the file, visit https://servbay-laravel-test.local
. You should now see Hello ServBay!
in your browser.
Database Integration Examples
Laravel offers a robust database abstraction layer and ORM (Eloquent) for easy interaction with different databases. ServBay comes pre-installed with MySQL, PostgreSQL, MongoDB, Redis, Memcached, and more for seamless local development and testing.
NoSQL Database Example
ServBay includes Redis and Memcached by default, and the required PHP extensions are pre-installed.
Memcached Example
Configure
.env
Set up Memcached connection details in your
.env
file (if not already configured):dotenvCACHE_STORE=memcached MEMCACHED_HOST=127.0.0.1 MEMCACHED_PORT=11211 # ServBay default Memcached port
1
2
3Using Memcached
In your routes or controllers, use Laravel’s Cache facade to interact with Memcached:
In
routes/web.php
, add a sample route:phpuse Illuminate\Support\Facades\Cache; Route::get('/memcached-test', function () { // Store data in cache, expires in 10 minutes (600 seconds) Cache::put('servbay_memcached_key', 'Hello from Memcached on ServBay!', 600); // Retrieve data from cache $value = Cache::get('servbay_memcached_key'); if ($value) { return "Memcached Data: " . $value; } else { return "Memcached data does not exist or has expired."; } });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Visit
https://servbay-laravel-test.local/memcached-test
to test.
Redis Example
Configure
.env
Set up Redis connection details in your
.env
file (if not already configured):dotenvCACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null # ServBay default Redis has no password REDIS_PORT=6379 # ServBay default Redis port
1
2
3
4Note: If you configure both Memcached and Redis as cache stores, ensure only one is active in
.env
(CACHE_STORE
) by commenting out the other.Using Redis
In your routes or controllers, use Laravel’s Redis or Cache facades to interact with Redis:
In
routes/web.php
, add a sample route:phpuse Illuminate\Support\Facades\Redis; // Alternatively, use the Cache facade, if CACHE_STORE=redis // use Illuminate\Support\Facades\Cache; Route::get('/redis-test', function () { // Using Redis facade Redis::set('servbay_redis_key', 'Hello from Redis on ServBay!'); $value_redis = Redis::get('servbay_redis_key'); // Or use Cache facade (if CACHE_STORE=redis) // Cache::put('servbay_redis_key', 'Hello from Redis on ServBay!', 600); // $value_cache = Cache::get('servbay_redis_key'); return "Redis Data: " . $value_redis; });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Visit
https://servbay-laravel-test.local/redis-test
to test.
Relational Database Example (MySQL & PostgreSQL)
ServBay comes with MySQL and PostgreSQL database servers pre-installed. Laravel’s Eloquent ORM makes it easy to connect and interact with these databases.
First, use ServBay’s database management tools (such as phpMyAdmin, Adminer, pgAdmin, or command-line clients accessible via the ServBay control panel) to create a database named servbay_laravel_app
.
Then, use Laravel’s migration system to create the database table structure.
Create a Migration File
In the project root, run this Artisan command to create a new migration:
bashphp artisan make:migration create_accounts_table --create=accounts
1This creates a new migration file under
database/migrations
.Edit the Migration File
Open the newly created migration file (file name format is similar to
YYYY_MM_DD_HHMMSS_create_accounts_table.php
) and edit theup
method as follows:php<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('accounts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); // Add created_at and updated_at columns }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('accounts'); } };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29Save the file.
Run the Migration
In the project root, run the migration to create the
accounts
table:bashphp artisan migrate
1If the database is properly configured and
servbay_laravel_app
exists, this command will create theaccounts
table and also Laravel’s default tables:users
,password_reset_tokens
,failed_jobs
,cache
,cache_locks
,jobs
,job_batches
, etc.
MySQL Example
Configure
.env
for MySQLEnsure your
.env
database configuration points to MySQL:dotenvDB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 # ServBay default MySQL port DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password # ServBay default MySQL password
1
2
3
4
5
6Insert Example Data
In
routes/web.php
, add a route to insert data into theaccounts
table:phpuse Illuminate\Support\Facades\DB; Route::get('/mysql-add-account', function () { DB::table('accounts')->insert([ 'name' => 'ServBay Demo User', 'email' => 'demo-user@servbay.test', 'created_at' => now(), 'updated_at' => now(), ]); return 'Account added to MySQL!'; });
1
2
3
4
5
6
7
8
9
10
11Visit
https://servbay-laravel-test.local/mysql-add-account
to add a record.Read Example Data
In
routes/web.php
, add a route to read data from theaccounts
table:phpuse App\Models\Account; // If you have created an Account model // Or directly use the DB facade Route::get('/mysql-accounts', function () { // Using DB facade $accounts = DB::table('accounts')->get(); // Or with Eloquent ORM (if Account model exists) // $accounts = Account::all(); return $accounts; });
1
2
3
4
5
6
7
8
9
10
11
12Visit
https://servbay-laravel-test.local/mysql-accounts
to view theaccounts
table data.
PostgreSQL Example
Configure
.env
for PostgreSQLTo use PostgreSQL, update the database settings in your
.env
file:dotenvDB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 # ServBay default PostgreSQL port DB_DATABASE=servbay_laravel_app DB_USERNAME=root # ServBay default PostgreSQL user DB_PASSWORD=password # ServBay default PostgreSQL password
1
2
3
4
5
6Note: When switching database connections, you may need to re-run
php artisan migrate:refresh
orphp artisan migrate
to recreate the table structure in PostgreSQL.Insert Example Data (PostgreSQL)
In
routes/web.php
, add a route to insert data into theaccounts
table:phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql-add-account', function () { DB::table('accounts')->insert([ 'name' => 'ServBay PG Demo User', 'email' => 'pg-demo-user@servbay.test', 'created_at' => now(), 'updated_at' => now(), ]); return 'Account added to PostgreSQL!'; });
1
2
3
4
5
6
7
8
9
10
11Visit
https://servbay-laravel-test.local/pgsql-add-account
to add a record.Read Example Data (PostgreSQL)
In
routes/web.php
, add a route to read data from theaccounts
table:phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql-accounts', function () { $accounts = DB::table('accounts')->get(); return $accounts; });
1
2
3
4
5
6Visit
https://servbay-laravel-test.local/pgsql-accounts
to view theaccounts
table data.
Summary
With ServBay, you can easily set up a local development environment to create and run Laravel projects. ServBay’s built-in PHP, Composer, Caddy web server, and various databases and caching services greatly simplify environment configuration. In just a few steps, you can create a new Laravel project, configure your web server, and begin development—all while easily integrating and testing database and caching features. ServBay is committed to delivering an efficient, user-friendly local development experience for developers.
If you encounter any issues during setup or usage, refer to ServBay’s official documentation or seek help from the community.