I needed automated site spin-ups for Flarum on my Mac localhost.
I now type mkdev foo
and it creates installed Flarum site foo.test
with database foo
in web directory foo
.
This setup is wildly insecure and should not be used for anything but localhost development.
Here's how I wired it up on my Mac:
First, I use ~/code
for repos and ~/sites
for web roots. I therefore have a development copy of Flarum running at ~/code/flarum
, directly wired into my other repos.
Second, I need a template for a simplified webroot. I create ~/sites/_copy
and add to it:
- A symlink to
../../code/flarum/public/assets
- A symlink to
../../code/flarum/flarum
(for the CLI)
- A symlink to
../../code/flarum/public/web.config
- A modified copy of
index.php
(A symlink usually won't work for this)
- A config template for Flarum
local_setup.yaml
To create the modified index.php
, simply add this on the second line (after the <?php
):
define('LOCALHOST_NAME', str_replace('.test', '', $_SERVER['SERVER_NAME']));
The config template local_setup.yaml
contents:
debug: true
baseUrl: http://NAME.test
databaseConfiguration:
driver: mysql
host: localhost
port: 3306
database: NAME
username: somename
password:
prefix: FLA_
adminUser:
username: somename
password: admin
email: your@email.com
You need to replace both username fields (and probably the email).
Do NOT replace the NAME
in the database & baseUrl fields.
Third, I need a custom ~/code/flarum/config.php
that uses the dynamically detected site name and uses it:
<?php
if (!defined('LOCALHOST_NAME')) {
// CLI fix
$paths = explode('/', getcwd());
define('LOCALHOST_NAME' , array_pop($paths));
}
return array (
'debug' => true,
'database' =>
array (
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => LOCALHOST_NAME,
'username' => 'linc',
'password' => 'admin',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => 'FLA_',
'strict' => false,
'engine' => null,
'prefix_indexes' => true,
),
'url' => 'http://' . LOCALHOST_NAME . '.test',
'paths' =>
array (
'api' => 'api',
'admin' => 'admin',
),
'headers' =>
array (
'poweredByHeader' => true,
'referrerPolicy' => 'same-origin',
),
);
Then, make a COPY of this file at ~/code/flarum/_mkdev_config.php
.
Fourth, create the file ~/bin/mkdev
(and use chmod +x ~/bin/mkdev
to make it executable):
# usage: `mkdev sitename`
# sitename = folder, domain{.test}, and db name
# copy files
sudo cp -R ~/sites/_copy ~/sites/$1
sudo chown -R linc ~/sites/$1
# database
sudo echo "create database $1" | mysql -u linc
# rm config so flarum installs
rm ~/code/flarum/config.php
# set custom config yaml
sed -i '' 's/NAME/'"${1}"'/g' ~/sites/"${1}"/local_setup.yaml
# flarum setup
cd ~/sites/$1
php flarum install -f ~/sites/$1/local_setup.yaml
# restore the main config from backup
cp ~/code/flarum/_mkdev_config.php ~/code/flarum/config.php
# go there in Firefox
open -a Firefox.app http://$1.test
You need to replace linc
above with your Mac username and your MySQL username, respectively.
Fifth, you either need to add 127.0.0.1 foo.test
to /etc/hosts
every time you use this tool, or you need to install dnsmasq (see below). Replace foo
with the name you used for the mkdev
command.
That's it. You should get a clean install of Flarum wired into your dev copy every time you run the command.