Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Deployment

Deployment is moving the project from your computer to a server where visitors can see it. The built-in php spark serve server is not suitable for that: on a live server the site is served by the Apache or Nginx web server together with PHP. The main thing is to set the site root and the settings correctly.

The site root

The web server must point at the public folder, not at the project folder. All other folders — app, writable, vendor and the .env file — stay above the root and cannot be reached from a browser.

Nothing has to be written for Apache: public already contains an .htaccess file that sends requests to index.php. For Nginx the rule is set in the site configuration:

server {
    server_name example.com;
    root /var/www/blog/public;
    index index.php;
 
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
 
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

The try_files line means: if there is no file with this address, pass the request to index.php, that is to the framework.

The .env file on the server

The repository has no .env file, so it is created on the server by hand:

CI_ENVIRONMENT = production
 
app.baseURL = 'https://example.com/'
 
database.default.hostname = localhost
database.default.database = blog
database.default.username = blog_user
database.default.password = strong-password

In production mode error details are not shown to visitors and the debug toolbar is off. app.baseURL is the real address of the site: all links are built from it.

Deployment steps

The commands are run on the server in the project folder:

git pull
composer install --no-dev --optimize-autoloader
php spark migrate
php spark optimize
StepWhy
git pullGet the fresh code
composer install --no-devInstall packages without development tools
php spark migrateUpdate the database structure
php spark optimizeCache the settings and file locations

The same four commands are repeated on every update of the site.

Permissions on the writable folder

The web server must be able to write to writable: the cache, logs, sessions and uploaded files go there. On a Linux server this is done like so:

chown -R www-data:www-data writable
chmod -R 775 writable

www-data is the user the web server usually runs as; on your server the name may differ.

Checks after deployment

What to checkHow
The site opensThe home page and a post page
The environmentphp spark env prints production
The log is writtenFiles appear in writable/logs
Private files are unreachableThe address /.env returns 404

Common mistake

development mode on a live server shows any visitor the text of errors with file paths and database queries, and the debug toolbar shows the session contents. Make sure .env on the server says CI_ENVIRONMENT = production and that the site root is the public folder, not the project folder.

Thanks for reading!

Donate to the site — we work hard for you.