Creating and Running a Laravel Project in ServBay
Overview
ServBay is a powerful local web development environment designed specifically for macOS. It comes bundled with a range of popular software packages including PHP, Node.js, Python, Go, Java, multiple 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 using the ServBay environment. We'll make use of ServBay's built-in PHP environment, the Composer package manager, and robust "Website" (formerly "Host") management to streamline the entire process.
What is Laravel?
Laravel is a popular open-source PHP web application framework developed by Taylor Otwell. It follows the MVC (Model-View-Controller) architectural pattern and provides a wealth of out-of-the-box features aimed at streamlining common web development tasks such as authentication, routing, session management, caching, and database operations. Known for its elegant syntax, rich feature set, and strong community support, Laravel is a top choice for building modern and maintainable web applications.
Key Features and Advantages of Laravel
- Elegant Syntax: Clean, expressive code that enhances development efficiency and readability.
- Eloquent ORM: A powerful ActiveRecord implementation for simple and intuitive database interaction.
- Artisan Console: A suite of helpful CLI tools to handle migrations, code generation, running tests, and more.
- Blade Templating Engine: Clean templating syntax for building dynamic views quickly and intuitively.
- Rich Ecosystem: Thousands of official and third-party packages easily integrated through Composer.
- Vibrant Community Support: An active community providing abundant resources, tutorials, and solutions.
Advantages of Developing Laravel Projects with ServBay
ServBay streamlines Laravel development by providing the following benefits:
- Integrated Environment: Pre-installed with multiple PHP versions, Composer, and popular database and cache services. No need for manual installation or configuration.
- Easy Management: Conveniently switch PHP versions, manage website configurations, and start/stop services via ServBay’s graphical interface.
- Pre-configured Web Server: Utilizes Caddy by default, with optimized settings for popular PHP frameworks (including Laravel) to make web server setup effortless.
- HTTPS Support: Automatically configures SSL certificates (issued by ServBay User CA) for
.local
domains. HTTPS is enabled by default, ensuring a secure and production-like local development experience.
Creating a Laravel Project
ServBay recommends storing your website projects in the /Applications/ServBay/www
directory. This helps keep your file structure clean and projects well organized.
Verify Composer Installation
ServBay comes with Composer already installed—you don’t have to set it up separately. You can verify Composer is available via the terminal:
bashcomposer --version
1If the command shows a version number, Composer is ready to use.
Create a New Laravel Project
Open your terminal, navigate to the ServBay recommended directory, and use Composer to create a new Laravel project. Let’s name the project directory
servbay-laravel-app
:bashcd /Applications/ServBay/www # Create the project directory mkdir servbay-laravel-app # Enter the project directory cd servbay-laravel-app # Use Composer to create a new 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 along with its dependencies.
Enter Project Directory
Confirm your current terminal location is the root of your newly created Laravel project:
bashcd /Applications/ServBay/www/servbay-laravel-app
1
Initial Configuration
Generate the Application Key
Laravel uses an application key to secure user sessions and encrypted data. In your project root (
/Applications/ServBay/www/servbay-laravel-app
), run the Artisan command to generate an app key:bashphp artisan key:generate
1This command will generate and set the
APP_KEY
in your.env
file.Configure Environment Variables (
.env
)Laravel uses the
.env
file for managing environment-specific settings such as database connections and app URLs. Open the.env
file in your project root and configure as needed. Make sure the following basic settings are correct:dotenvAPP_NAME=ServBay Laravel Demo APP_ENV=local APP_KEY=base64:... # This is generated by php artisan key:generate APP_DEBUG=true APP_URL=https://servbay-laravel-test.local LOG_CHANNEL=stack # Database configuration example (MySQL) DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app # Your desired database name DB_USERNAME=root # ServBay default database user DB_PASSWORD=password # ServBay default database password # Cache/queue config example (Redis) CACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null # ServBay Redis defaults to no password REDIS_PORT=6379 # Cache/queue config example (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_*
as appropriate for your setup. ServBay’s default database credentials can be found in the ServBay control panel.
Configuring the Web Server (Add a Website in ServBay)
Use ServBay’s website management to route your domain to the public
directory of your Laravel project.
Open the ServBay Control Panel
Start the ServBay app and open the control panel.
Add a New Website
Go to the “Websites” section in the ServBay control panel. Click the add button (usually a
+
or similar) to create a new website entry.Fill in Website Information
Configure your website as shown below:
- Name:
My First Laravel Dev Site
(any name helpful for identification) - Domain:
servbay-laravel-test.local
(the domain you wish to use in your browser) - Site Type:
PHP
- PHP Version: Pick a PHP version compatible with your Laravel version, e.g.
8.3
. - Site Root:
/Applications/ServBay/www/servbay-laravel-app/public
(Important: Must point to thepublic
folder inside your Laravel project root)
For in-depth steps and options, see ServBay’s official guide on Adding Your First Website (look for the English version link).
- Name:
Save and Apply
Save your website configuration. ServBay will automatically update the web server settings (Caddy by default). If prompted, restart the web server as instructed.
Verifying Basic Access
At this point, your Laravel project should be accessible via your configured domain.
Open Your Browser
Enter your configured domain, e.g.
https://servbay-laravel-test.local
, into your browser.Check the Results
If everything is set up correctly, you should see Laravel's welcome page.
Add a Simple Example Route
For a clear verification, you can add a simple route in routes/web.php
that returns "Hello ServBay!".
Open the routes/web.php
file in your project directory and add or modify this:
<?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!'; // Add or modify this line
});
// ... other routes
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Save the file, then visit https://servbay-laravel-test.local
again. You should now see Hello ServBay!
in your browser.
Database Integration Examples
Laravel features a robust database abstraction layer and ORM (Eloquent), making it easy to interact with a variety of databases. ServBay comes pre-installed with MySQL, PostgreSQL, MongoDB, Redis, and Memcached—perfect for local development and testing.
NoSQL Database Examples
ServBay has Redis and Memcached already integrated, and the necessary PHP extensions are pre-installed.
Memcached Example
Configure
.env
Add Memcached connection settings to 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
Try this route in your routes or controllers to interact with Memcached via Laravel’s Cache facade:
Add the following to
routes/web.php
:phpuse Illuminate\Support\Facades\Cache; Route::get('/memcached-test', function () { // Store data in cache for 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
Add Redis connection settings to your
.env
file (if not already configured):dotenvCACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null # ServBay Redis defaults to no password REDIS_PORT=6379 # ServBay's default Redis port
1
2
3
4Note: If you have both Memcached and Redis configured as cache stores, ensure only one is active (comment out the other) in your
.env
.Using Redis
Use Laravel’s Redis facade or the Cache facade to store and retrieve data from Redis.
Add the following route in
routes/web.php
:phpuse Illuminate\Support\Facades\Redis; // Or use Cache facade, provided CACHE_STORE=redis // use Illuminate\Support\Facades\Cache; Route::get('/redis-test', function () { // Using the Redis facade Redis::set('servbay_redis_key', 'Hello from Redis on ServBay!'); $value_redis = Redis::get('servbay_redis_key'); // Or using the 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 try it out.
Relational Database Examples (MySQL & PostgreSQL)
ServBay comes with MySQL and PostgreSQL servers pre-installed. Laravel’s Eloquent ORM can easily connect to both.
First, create a database for your project. You can use ServBay’s database management tools (like phpMyAdmin, Adminer, pgAdmin, accessible from the ServBay control panel) or a command-line client to create the servbay_laravel_app
database.
Next, we’ll use Laravel migrations to create a database table.
Create a Migration
In your project root, run this Artisan command to create a new migration:
bashphp artisan make:migration create_accounts_table --create=accounts
1This will generate a migration file in
database/migrations
.Edit the Migration
Open the newly created migration file (named like
YYYY_MM_DD_HHMMSS_create_accounts_table.php
) and update theup
method to define the structure: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(); // Adds 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 your project root, run this Artisan command to create the
accounts
table:bashphp artisan migrate
1If the database is configured correctly and
servbay_laravel_app
exists, this command will create theaccounts
table and 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
has settings for MySQL:dotenvDB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 # ServBay's default MySQL port DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password # ServBay's default MySQL password
1
2
3
4
5
6Insert Example Data
Add a route in
routes/web.php
to insert a record into theaccounts
table:phpuse Illuminate\Support\Facades\DB; Route::get('/mysql-add-account', function () { DB::table('accounts')->insert([ 'name' => 'ServBay Demo User', 'email' => '[email protected]', '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 insert a row.Read Example Data
Add another route to retrieve data from the
accounts
table:phpuse App\Models\Account; // If you’ve created an Account model // Or just use the DB facade Route::get('/mysql-accounts', function () { // Using the DB facade $accounts = DB::table('accounts')->get(); // Or using 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 data.
PostgreSQL Example
Configure
.env
for PostgreSQLIf you want to use PostgreSQL, update your
.env
accordingly: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: After switching database connections, you may need to run
php artisan migrate:refresh
orphp artisan migrate
again to set up the table structures in PostgreSQL.Insert Example Data (PostgreSQL)
Add a route in
routes/web.php
to insert a record into theaccounts
table:phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql-add-account', function () { DB::table('accounts')->insert([ 'name' => 'ServBay PG Demo User', 'email' => '[email protected]', '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 insert a row.Read Example Data (PostgreSQL)
Add another route to retrieve data from the
accounts
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 the data.
Summary
With ServBay, you can easily set up a local development environment on macOS for creating and running Laravel projects. ServBay’s pre-integrated PHP, Composer, Caddy web server, and numerous database and cache services greatly simplify the entire configuration process. With just a few straightforward steps, you can spin up a new Laravel project, configure the web server, and kick-start development—with seamless integration and testing for multiple database and cache technologies. ServBay is committed to providing developers with an efficient and user-friendly local development experience.
If you encounter issues, consult ServBay’s official documentation or seek support from the community.