Creating and Running a PHPixie Project on macOS with ServBay
PHPixie is a lightweight, high-performance PHP framework designed for the rapid development of web applications. It follows the HMVC (Hierarchical Model-View-Controller) architecture, providing a clean code structure and efficient processing power. PHPixie is favored by many developers for its simplicity, flexibility, and impressive speed.
Key Features and Advantages of PHPixie
- Lightweight: The PHPixie core system is minimal, containing only the essential components needed for building applications, resulting in fast startup and execution.
- High Performance: Engineered for efficiency, this framework can handle concurrent requests effectively, making it suitable for performance-critical applications.
- Easy to Learn: Offers a clean, intuitive API and thorough documentation so developers can onboard quickly.
- Flexible: Its loosely coupled architecture allows developers to freely select and integrate third-party libraries and components as needed.
- Active Community Support: PHPixie enjoys a vibrant developer community that offers a wealth of third-party extensions and resources.
PHPixie empowers developers to efficiently build high-quality, high-performance web applications, suitable for everything from small projects to large-scale enterprise systems.
Creating and Running a PHPixie Project with ServBay
This guide walks you through setting up and running a PHPixie project in a local web development environment provided by ServBay. We'll utilize ServBay's integrated PHP environment, Composer dependency manager, and "Websites" feature to configure the web server, enabling easy local access and seamless integration with databases and caches.
Prerequisites
- ServBay should be installed and running on your macOS system. ServBay integrates PHP, Composer, various databases (MySQL, PostgreSQL, MongoDB, Redis, etc.), and the Caddy web server.
- Basic command line knowledge is required.
Creating a PHPixie Project
Recommended Project Location
ServBay recommends storing your local website projects in the /Applications/ServBay/www
directory for easier management and access control within ServBay.
- Verify Composer Installation: ServBay comes pre-installed with Composer; no need for additional setup. You can verify this by running
composer --version
in the Terminal. - Create Project Directory: Create a new directory for your PHPixie project in the recommended root location and navigate to it:bash
cd /Applications/ServBay/www mkdir servbay-phpixie-app cd servbay-phpixie-app
1
2
3 - Create a PHPixie Project via Composer: Inside the project directory, run the Composer command to fetch the PHPixie project template:bashComposer will download PHPixie and its dependencies into your current directory.
composer create-project phpixie/project .
1
Configuring the Web Server
Use ServBay's Websites feature to set up the Caddy web server and correctly route requests to your PHPixie project.
Open the ServBay application interface.
Navigate to the Websites list.
Click to add a new website entry.
Fill in the site configuration:
- Name: e.g.,
My First PHPixie Dev Site
(this is an internal identifier within ServBay). - Domain: e.g.,
servbay-phpixie-test.local
(your local development domain used in the browser). - Website Type: Select
PHP
. - PHP Version: Choose your preferred PHP version, e.g.,
8.3
. - Document Root: Point to your project's
web
directory:/Applications/ServBay/www/servbay-phpixie-app/web
.
Why the
/web
directory?The entry script for a PHPixie project is located at
web/index.php
. For security and best practices, set your web server's root to this directory rather than the project root—this prevents direct web access to sensitive files such as configs or the vendor directory.- Name: e.g.,
Save your configuration. ServBay will automatically configure Caddy and update your local hosts file (if necessary), mapping
servbay-phpixie-test.local
to your ServBay server.
For step-by-step details, refer to Adding Your First Website.
Adding Sample Code
To verify that your project is running correctly and demonstrate database/cache integration, let's add some sample code to your project.
Create a Sample Controller: Add the following to
src/App/HTTP/Controller/Home.php
. Create the file and directories if they do not exist.php<?php namespace App\HTTP\Controller; use PHPixie\HTTP\Request; use PHPixie\Template; use PHPixie\Database\Query; // Import Query class class Home extends \PHPixie\Controller { protected $template; public function __construct(Template $template) { $this->template = $template; } // Default homepage action public function action_index(Request $request) { // Render the assets/templates/app/home.php template return $this->template->render('app:home'); } // Memcached example action public function action_memcached(Request $request) { $cache = $this->components->cache(); $cache->set('key', 'Hello Memcached from ServBay!', 60); // Store data, valid for 60 seconds $value = $cache->get('key'); // Retrieve data return $this->response()->string($value); // Return string response } // Redis example action public function action_redis(Request $request) { $redis = $this->components->redis(); $redis->set('key', 'Hello Redis from ServBay!'); // Store data $value = $redis->get('key'); // Retrieve data return $this->response()->string($value); // Return string response } // MySQL/PostgreSQL example to add a user public function action_add_user(Request $request) { // Use database component to get a query builder $query = $this->components->database()->query(); // Insert a new user into the 'users' table $query->insert('users')->data([ 'name' => 'ServBay Demo User', 'email' => 'demo-' . uniqid() . '@servbay.test', // Use a unique email ])->execute(); // Execute insert return $this->response()->string('User added successfully.'); // Return success message } // MySQL/PostgreSQL example to list users public function action_list_users(Request $request) { // Use database component to get a query builder $query = $this->components->database()->query(); // Query all users from the 'users' table $users = $query->select('*')->from('users')->execute()->fetchAll(); // Execute and fetch all return $this->response()->json($users); // Return user list as JSON } }
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67Create a Sample Template: Add the following to
assets/templates/app/home.php
. Create the file and directories if they do not exist.php<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Welcome to PHPixie on ServBay</title> <style> body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1 { color: #333; } p { margin-bottom: 15px; } code { background-color: #f4f4f4; padding: 2px 5px; border-radius: 4px; } ul { list-style: none; padding: 0; } li { margin-bottom: 10px; } li a { text-decoration: none; color: #007bff; } li a:hover { text-decoration: underline; } </style> </head> <body> <h1>Welcome to PHPixie on ServBay</h1> <p>This page is being generated dynamically by your PHPixie application running on ServBay.</p> <h2>Sample Feature Links:</h2> <ul> <li><a href="/home/memcached">Test Memcached</a></li> <li><a href="/home/redis">Test Redis</a></li> <li><a href="/home/add_user">Add a user to the database</a></li> <li><a href="/home/list_users">List users from the database</a></li> </ul> <p>Please ensure that you have completed the required database and cache configuration steps.</p> </body> </html>
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
29
30
31
32
33
Accessing the Site
After configuring the ServBay website and adding the sample code, you can now access your site using the configured domain:
- Open your browser and navigate to
https://servbay-phpixie-test.local
. - You should see a page titled "Welcome to PHPixie on ServBay" with several sample feature links.
If you cannot access the page, check the following:
- Is ServBay running?
- Have you configured the website correctly in ServBay, especially the domain, type, and document root?
- Does your hosts file resolve
servbay-phpixie-test.local
correctly? (ServBay typically manages this automatically.) - Is the required PHP version enabled in ServBay?
Database and Cache Integration Examples
PHPixie comes with built-in support for multiple database and caching systems. ServBay provides packages for MySQL, PostgreSQL, Redis, Memcached, and more, making integration straightforward.
Important: Before performing database operations, be sure to start the relevant database service in ServBay (such as MySQL or PostgreSQL), and create a database for your project. For example, the sample uses a database called servbay_phpixie_app
. You can use database management tools (like Sequel Ace, Postico, TablePlus, etc.) or command-line clients to create your database. The default database user is typically root
with password password
(confirm this with your ServBay setup).
Configuring the Database Connection
Depending on your database (MySQL or PostgreSQL), edit the assets/config/database.php
file at the project root. ServBay's default DB address is usually 127.0.0.1
.
MySQL Example (assets/config/database.php
):
<?php
return [
'default' => [
'driver' => 'pdo',
// Adjust the connection string to match your DB and ServBay config
'connection' => 'mysql:host=127.0.0.1;dbname=servbay_phpixie_app',
'user' => 'root', // ServBay default user
'password' => 'password', // ServBay default password (change as needed)
'options' => []
]
];
2
3
4
5
6
7
8
9
10
11
12
PostgreSQL Example (assets/config/database.php
):
<?php
return [
'default' => [
'driver' => 'pdo',
// Adjust the connection string to match your DB and ServBay config
'connection' => 'pgsql:host=127.0.0.1;dbname=servbay_phpixie_app',
'user' => 'root', // ServBay default user
'password' => 'password', // ServBay default password (change as needed)
'options' => []
]
];
2
3
4
5
6
7
8
9
10
11
12
Creating Database Tables (using Migrations)
PHPixie offers command-line tools for managing database migrations—a way to version-control your database schema.
- Open Terminal: Switch to your PHPixie project directory:bash
cd /Applications/ServBay/www/servbay-phpixie-app
1 - Generate a Migration File: Use the PHPixie CLI to generate a migration—for example, to create a
users
table:bashThis will create a new PHP migration file inphp pixie generate:migration create_users_table
1assets/migrations
, named with a timestamp. - Edit the Migration File: Open the new migration file in
assets/migrations
(e.g.,YYYY_MM_DD_HHMMSS_create_users_table.php
) and define the schema in theup()
anddown()
methods:php<?php use PHPixie\Database\Migration; use PHPixie\Database\Schema\Table; class CreateUsersTable extends Migration { public function up() { // Use schema builder to create 'users' table $this->schema->create('users', function(Table $table) { $table->increments('id'); // Auto-increment primary key $table->string('name'); // Name column $table->string('email')->unique(); // Email column, unique $table->timestamps(); // created_at and updated_at timestamps }); } public function down() { // Use schema builder to drop the 'users' table $this->schema->drop('users'); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 - Run the Migration: From your project root, run:bashIf successful, you'll see a confirmation message in your terminal. Verify that the
php pixie migrate
1users
table is created in yourservbay_phpixie_app
database using a DB management tool.
Using Relational Databases (MySQL/PostgreSQL)
In the sample controller (src/App/HTTP/Controller/Home.php
), the action_add_user
and action_list_users
methods illustrate how to interact with MySQL or PostgreSQL using PHPixie's database component.
- Add a user: Visit
https://servbay-phpixie-test.local/home/add_user
to insert a new user record. - List users: Visit
https://servbay-phpixie-test.local/home/list_users
to retrieve all user records as JSON.
Configuring and Using NoSQL Databases (Memcached/Redis)
ServBay ships with Memcached and Redis packages (and their PHP extensions) pre-installed. Just configure your PHPixie project to use them.
Memcached Configuration (assets/config/cache.php
):
Edit or create assets/config/cache.php
:
<?php
return [
'default' => [
'driver' => 'memcached',
'servers' => [
[
'host' => '127.0.0.1', // ServBay Memcached default host
'port' => 11211, // Default port
'weight' => 100,
],
],
],
];
2
3
4
5
6
7
8
9
10
11
12
13
14
Redis Configuration (assets/config/redis.php
):
Edit or create assets/config/redis.php
:
<?php
return [
'default' => [
'hostname' => '127.0.0.1', // ServBay Redis default host
'port' => 6379, // Default port
'timeout' => 0,
'database' => 0, // Redis DB index
],
];
2
3
4
5
6
7
8
9
10
Using Memcached/Redis:
In the sample controller (src/App/HTTP/Controller/Home.php
), the action_memcached
and action_redis
methods demonstrate how to use PHPixie's cache component (which can be configured for Memcached or Redis).
- Test Memcached: Visit
https://servbay-phpixie-test.local/home/memcached
to write a key-value pair to Memcached, read it back, and display the value. - Test Redis: Visit
https://servbay-phpixie-test.local/home/redis
to write and read a key-value pair via Redis.
Make sure the Memcached and Redis services are running in ServBay.
Summary
With these steps, you've successfully set up, configured, and run a PHPixie project locally on macOS using ServBay. You've learned how to create a new project using Composer, configure the web server with ServBay's Websites feature, set up essential database and cache integrations, and verify that everything works.
ServBay's integrated stack greatly simplifies PHP web development on macOS—letting you focus on writing code instead of spending time setting up your environment. We hope this guide helps you get started quickly with PHPixie for your next web project!