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 PHPIntroduction
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.
Video Tutorial Demonstrating the Process on Shared Hosting
Keep scrolling below unless you'd prefer to watch a video tutorial demonstrating the process on a shared hosting plan based on cPanel.
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
or
/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 MySQL/MariaDB or Postgresql with schema sizes as big as 5GB unlike most shared hosting servers.
- A user-friendly GUI tool for editing DNS records of your domains.
- Memcached server and a couple of other modern caching tools
- And more…
In conclusion, deploying your CodeIgniter 4 project to production can seem a daunting task for beginners, but with the help of this comprehensive guide, the process becomes accessible and manageable. From configuring your .env file to using Composer for handling dependencies and setting up your project on a shared hosting server, each step is essential in ensuring a seamless deployment. Keep in mind that the use of tools like CodeIgniter Wizard can simplify your upgrade process, and shared hosting services such as A2 Hosting can provide additional features that can further streamline your work. It's important to remember that like any development task, successful deployment relies on attention to detail, understanding of your tools, and patience. With these tips and instructions at your disposal, you're now ready to take your CodeIgniter 4 project to the production stage. Happy coding and prod deployment!
Coolars
Great walkthrough of the CodeIgniter 4 production deployment process! Your explanations were clear and easy to understand, which made the whole process less intimidating. The recommendation for A2 Hosting seems solid with all the advanced features you mentioned. However, I'm wondering if you have any advice for troubleshooting common issues during deployment? For instance, what would be your go-to strategies when facing database connection errors or problems with file permissions?Ozar
Thank you for your kind words and insightful questions! Troubleshooting deployment issues is a vast topic, and strategies can differ based on the issue at hand. 1 Database Connection Errors: These are commonly due to incorrect credentials in your .env or database configuration file. Always ensure that your database credentials (hostname, username, password, database name) are correctly specified. Sometimes, the problem might be from the server itself, so checking the server's error logs can be helpful. If you're migrating a database, make sure the database schema is properly set up in the production environment. 2 File Permissions: Problems with file permissions are usually related to the "writable" directory in CodeIgniter 4. This folder must be writable by the web server. Depending on the server setup, the permission could be set to 755 or 777, though 777 is less secure. If you're deploying on a Unix-based system, you can set the permissions via terminal using 'chmod' command. On shared hosting, you can typically adjust these settings through the control panel provided by your hosting service. These are general strategies, and specific issues might require further troubleshooting. Also, keep an eye on the logs, as they can provide valuable insights about what's going wrong. In the context of deployment, a clear understanding of your server environment and how it interacts with your application is essential.