Deploying Laravel application to nearlyfreespeech.net

Categories:
Deploying Laravel application to nearlyfreespeech.net

I recently created a Laravel application as a side-project to re-learn the framework. (I'll post more about it later. It's a URL Shortener). The documentation for getting this application was mostly correct. However, I discovered at least one issue because I wanted to save money and use a SQLite database instead of the more costly MySQL one.

Transfer files

Get your files over to the site by rsync-ing the directory to the protected area:

# Replace $ACCOUNT with your NFSN account username and
# $HOST with your assigned server hostname.
$ rsync -avz --exclude='.git' \
  --exclude='node_modules' \
  --exclude='storage/logs/*' \
  --exclude='storage/framework/cache/*' \
  --exclude='storage/framework/sessions/*' \
  --exclude='storage/framework/views/*' \
  --exclude='.env' --exclude='vendor' \
  --exclude='.DS_Store' --exclude='*.log' \
  . $ACCOUNT@$HOST:/home/protected/laravel-app/

Update .env

Depending on your application this could be complicated or easy. Copy your .env.example to .env and make any changes necessary.

Set file system permissions

I found 3 directories I needed to update, if you aren't using the default sqlite database you can skip that one.

#!/bin/bash

directories=(
    "database"        # ⚠️ where the sqlite database goes
    "storage"         # logs/cache
    "bootstrap/cache" # cache (duh)
)

for directory in "${directories[@]}"; do
    chown -R web "$directory"
    chmod -R 775 "$directory"
done

Get public directory configured

The public directory is locked down so it can't be moved or overwritten. I moved everything in my Laravel app's public directory into the nearlyfreespeech one, deleted the empty directory, and then linked to the canonical one. Effectively it lets the public directory be in 2 places at the same time.

From inside the laravel-app directory:

$ ln -s ../../public public

Rebuild application

You can forego this step if you transfer your vendor directory and run npm run build first too. From the Laravel app directory:

  1. composer install

  2. npm install

  3. npm run build

  4. php artisan optimize

.htaccess

Either move or copy the provided .htaccess file to public. Laravel requires its own .htaccess rules for routing (otherwise all requests won’t be directed through index.php).

Conclusion

That should be it. Test your application and get it running. I found a problem with a models generated attributes--💡_I had to add the $appends property_--but in general it seems to be working well.