Statamic CMS, Laragon and shared hosting

| permalink | statamic, cms, dev

Update

This was an experiment, I've since changed to Kirby CMS because it is simply too painful trying to use Statamic CMS with shared hosting and without Composer.

I was clearly trying to jam a square peg into a round hole.

Why bother with Shared Hosting?

It's cheap. That's it.

Statamic Solo is free, that's a big incentive for a lot of people.

Hosts like Digital Ocean or Linode is way too technically involved for people who wants to dabble in making websites, and using Laravel Forge or Site Pilot just adds too much cost on top for what is likely just a personal site and playground.

So you're left with shared hosting and Statamic Solo, and this is probably the most comprehensive guide to it.

URL redirect is needed when Statamic's public folder is not the site root

Statamic has too many hardcoded internal logic and links that assume it lives in site root, therefore it's not practical to modify it to be accessed from a subdirectory (I tried, and I gave up).

So in both production and development, you will need to either make the public folder the site root, or redirects site root to it.

Because shared hosts usually force you to use public_html folder as the site root, you will have to do a direct.

.htaccess example for public_html, redirect everything to the public folder:

RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

Optionally, you can add the following conditions

# enforce rule only on specific domain (for multi-domain hosting)
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]

# enforce rule only when url doesn't contain public folder after domain
RewriteCond %{REQUEST_URI} !^public/

In my case, I had Statamic installed in a subfolder for testing purpose.

I added the condition to only apply the redirect when URL does not contain specific path to the folder in case someone wants to directly link to it.

Note: [NC] means case-insensitive (no case) and [L] means last as in the last rule to be applied.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^statamic/public/ [NC]
RewriteRule ^(.*)$ statamic/public/$1 [L]

Local development with Laragon

This guide is written for Windows 10 and Laragon.

With Laragon you can skip redirects, because if you use the virtual host (Menu -> Preferences... -> Auto virtual hosts) Laragon automatically turns all folders under www site root into their own sites with the domain name of {name}.test.

In addition, Laragon automatically maps site root to the public folder, if it exists.

The magic happens in C:\laragon\etc\apache2\sites-enabled\auto.statamic.test.conf

<VirtualHost *:81> 
    DocumentRoot "C:/laragon/www/statamic/public/"
    ServerName statamic.test
    ServerAlias *.statamic.test
    <Directory "C:/laragon/www/statamic/public/">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

# If you want to use SSL, enable it by going to Menu > Apache > SSL > Enabled

Note that if Laragon is serving from a specific port, you also need to use that port when using the domain name, such as statamic.test:82. Or else you will be accessing the default port 80, which is likely being used by IIS, Microsoft's bundled server that comes with Windows 10.

Setting up Laragon

First, download and install Laragon, for this guide you'd want to use PHP and Apache (you can also use Nginx, but you're on your own regarding redirects).

Then, download the thread safe version of the latest PHP (or non-thread-safe if using Nginx), extract as a new folder into C:\laragon\bin\php

C:\laragon\etc\apache2\mod_php.conf

    Replace "php8_module" with "php_module", this is a change for PHP8.

    So the loader line should look like this:

    LoadModule php_module "C:/laragon/bin/php/php-8.1.4-Win32-vs16-x64/php8apache2_4.dll"

Enable extensions within C:\laragon\bin\php\php-8.1.4-Win32-vs16-x64\php.ini

    Uncomment the extensions needed by Statamic at https://statamic.dev/requirements

Next, download the latest certificate and replace the old certificate located at C:\laragon\etc\ssl\cacert.pem, this will prevent cURL error 60.

   GuzzleHttp\Exception\RequestException

  cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://repo.packagist.org/p2/statamic/starter-kit-cool-writings.json

  at C:\laragon\www\statamic\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:211
    207▕
    208▕         // Create a connection exception if it was a specific error code.
    209▕         $error = isset($connectionErrors[$easy->errno])
    210▕             ? new ConnectException($message, $easy->request, null, $ctx)
 ➜ 211▕             : new RequestException($message, $easy->request, $easy->response, null, $ctx);
    212▕
    213▕         return P\Create::rejectionFor($error);
    214▕     }
    215▕

Finally, update composer and install Statamic via CLI per instruction from the Statamic documentation.

Moving site between development and production

You want to be sure to change the APP_URL in the .env file to the site root (example - dev: http://mysite.test, prod: http://mysite.com), it's located on same level as the public folder. The site might appear to work even if this isn't changed, but this is used by Laravel and calls to config('app.url') or env('APP_URL') will fail otherwise.

Make sure that any redirect you've done isn't accidentally copied between environments.

Updating Statamic on Shared Hosting

Easiest way would be just to update locally and copy the site over to production, however I think a lot of Shared Hosting does offer shell access such as Namecheap

Some people report their process getting killed due to high memory usage, so YMMV.