Demystifying a frequently asked question by many...

How to deploy your CodeIgniter 4 project to production

Demystifying a frequently asked question by many...

A confusing topic for novice CodeIgniter developers clarified for multiple scenarios on how to deploy apps to production

published on 5 May 2022 in CodeIgniter Web Development PHP

Introduction

This article describes the common steps needed for your CodeIgniter 4 project to get ready for production deployment. Although CodeIgniter Wizard is extensively mentioned throughout the article, the solutions described here can be applied to almost any CodeIgniter4 project.

CodeIgniter Wizard creates Composer-based CI4 projects with the base framework libraries and files residing in the vendor directory and it includes the .env, composer.json and composer.lock files in the root of the project so that you can not only change the essential project configuration by simply editing the .env file in an editor, but also open the project root in a command-line terminal (or “Command Prompt” in Windows) and run composer commands.

The convenience of PHP composer-based projects proves itself when it comes to adding 3rd-party libraries compatible with your project’s version or simply when you want to instantly change the version of the CodeIgniter framework – typically for upgrading it to the latest version with as fewest hassles as possible.

The easiest and simplest way is, of course, upgrading your project with CodeIgniter Wizard, however, as it will save the labor of doing some manual work before running composer update like the one you need to do when upgrading from 4.1.9 to 4.2.0.

Configure your .env file for production

By default the environment variable named CI_ENVIRONMENT in the ENVIRONMENT section of your .env file is set to “development” .

#----------------------------------------------------------
# ENVIRONMENT
#----------------------------------------------------------
CI_ENVIRONMENT = development

For easy switching between development and production, you may want to comment the line for development and insert a new line like so:

# CI_ENVIRONMENT = development
CI_ENVIRONMENT = production

Neither using Composer, nor CodeIgniter Wizard? You should be all set if you had developed your project in a virtualized host environment, or Nginx or Vagrant as described here. You can safely ignore or skip the next section if your CodeIgniter project is not composer-based.

Use Composer to remove development dependencies

This step applies only to CodeIgniter4 projects that were created using PHP Composer, or any project created with CodeIgniter Wizard 1.5 or later.

Open the root directory (of a copy) of your project in a command line terminal, and then run the following command:

composer update --no-dev

This, of course, requires you have PHP Composer installed, and it will remove a bulk of files that would only be needed to facilitate development (such as PHPUnit, etc).

Upload the project (folder) to the production web server

Assuming that you are not using an automatic deployment from a version control system, you have yet to upload the project files to the production web server manually via FTP or the “File Manager” of your hosting server’s cPanel, for example. Thanks to HTTP/2, uploading hundreds of files to a remote web server should only take a few minutes (if not seconds).

Web server’s public html directory (which might be named as one of public_html, htdocs, www or wwwroot) should be your CodeIgniter4 project’s public directory.

app/
    Config/
    Controllers/
    Database/
    Filters/
    Helpers/
    Language/
    Libraries/
    Models/
    ThirdParty/
    Views/
public/ <- public_html or htdocs or www should point here 
           or vice versa
vendor/ (if composer-based, otherwise: system/)
writable/

If you have a dedicated server or VPS (or your own server instance in the Cloud), the public directory should be the public folder one-level below your project directory, not the entire directory containing /app and /writable directories. On the other hand, if you are using shared hosting, follow the steps in the next paragraph.

Deploying your CodeIgniter4 project to a shared hosting server

If you have a shared hosting account, the root directory you could access would typically look like the following:

/home/youraccountname/

and the public html or htdocs folder would be at:

/home/youraccountname/public_html
/home/youraccountname/www  (symbolic link to public_html)
Using shared hosting for a single web project (or the first one for the first time)

If you are using the entire shared hosting account for a single web project or this is the first time you are setting up anything in this account, follow the steps below:

First, upload everything inside your CodeIgniter project’s public folder to public_html or www directory (they would be pointing to the same path in the file system) on the server.

Next, upload your CodeIgniter project folder (excluding the public folder) to /home/youraccount/.

Then, simply skip to the “Final Step”.

Using shared hosting for multiple websites and/or projects

If you are using multiple websites and/or web apps on a single shared hosting account, create either a new add-on domain or a subdomain dedicated solely to your CodeIgniter project, and make sure its public htdocs directory is different than that of other websites or web applications you have setup in the hosting account. That is, if public_html (or its equivalent) is used for an existing site or web app, the domain for this CI4 project should have its public directory should be at a different location. If you haven’t set up anything yet on this shared hosting account before, you can go back to the previous sub-title and apply the instructions there, and later for your next projects you can apply the instructions in this section.

Now, assuming existing public_html (or its equivalent whatever it is called) is already used for another website or app, create a new domain and have it point to /home/youraccount/public_html_other/yourcodeigniter4projectpublic/ as its public docs directory (so that you can keep adding new projects under “/home/youraccount/public_html_other/” with different folder names) .

Next, upload the contents of CodeIgniter project’s public folder to /home/youraccount/public_html_other/yourcodeigniter4projectpublic/.

Then, create a new directory in /home/youraccount/ and name it ‘ci4projects’.

And then, upload your CodeIgniter project folder (excluding the public folder) to /home/youraccount/ci4projs/.

If you have done this correctly, a directory structure similar to the following should be formed:

/home/youraccount/ci4apps/yourci4project/
/home/youraccount/ci4apps/yourci4project/app
/home/youraccount/ci4apps/yourci4project/vendor (or
/home/youraccount/ci4apps/yourci4project/system    )
/home/youraccount/ci4apps/yourci4project/writable

Then edit the index.php file inside /home/youraccountname/public_html (in which case the fully qualified file path would be /home/youraccountname/public_html/index.php )

Find the following lines:

// Load our paths config file
// This is the line that might need to be changed, depending on your folder structure.
require FCPATH . '../app/Config/Paths.php';
// ^^^ Change this line if you move your application folder

Change

require FCPATH . '../app/Config/Paths.php';

to:

$pathsConfig = FCPATH . '../../ci4apps/yourci4project/app/Config/Paths.php';

Note the URL of this domain (or subdomain) as you will need it in the final step, which is next.

The Final Step

Your domain name which would consequently affect your web app address should be evident depending on how you use your hosting server.

If you are using multiple websites and/or web apps on a single shared hosting account, it is assumed in the context of the how-to’s of this article that you have determined the correct domains and the correct URLs pertaining to them by now.

As a final step, edit the .env file of the CodeIgniter project, changing the line

app.baseURL = 'http://localhost:8080'

to

app.baseURL = 'http://whatever.your.domain.is'

and you are done!

When a user visits “http://whatever.your.domain.is”, they will see what’s displayed by the home controller.

For shared hosting, Ozar endorses A2 Hosting that offers amazing features in addition to cPanel:

  • Drag and drop file manager to upload and decompress large archives on the server.
  • Unlimited free SSL certificates (Let’s Encrypt) on most plans
  • Your own Git-based remote version repository (you can commit cpanel.yaml files to directly publish your PHP project from your own remote repo!)
  • Ability to run NodeJS, Python and Ruby apps via ‘Passenger’.
  • A couple of different automatic backup mechanisms for your files and databases.
  • Support for MariaDB or Postgresql with unlimited table sizes unlike most shared hosting servers.
  • A user-friendly GUI edit DNS records of your domains.
  • Memcached server
  • And more…
Share this on

What do you think?