IncludeBeer

The checklist of things to check when your CodeIgniter 4 web application is not working

Published on 7 June 2020
Article's image
Image credit: Ilya Pavlov

Check for errors in Apache logs

The first thing to check when a website is not working is the web server logs. One of the most popular servers is Apache. There are other popular servers such as Ngynx and IIS, but I will focus on Apache. Check for errors in the error.log file. If there are problems with file permissions, missing files, or syntax errors in the PHP code, or if you have the infamous blank page in your web browser, chances are error messages are written to the error.log file.

Depending on your installation, the error logs can be in different places or have a different name than error.log. The common locations to check are:

/var/log/apache/error.log
/var/log/apache2/error.log
/var/log/httpd/error.log

On macOS, if you are using MAMP, the log is located there:

/Applications/MAMP/logs/apache_error.log

If you host your site with DreamHost, the error log is located in the home directory of the user under which your website is running:

/home/username/logs/domain-name.com/http/error.log
/home/username/logs/domain-name.com/https/error.log

If you cannot find the location of the error log, create a test page and call the phpinfo() function. Look for the word error_log in this page. This will tell you the directory and the file name.

<?php
phpinfo();
?>

Check for errors in the CodeIgniter 4 logs

In a CodeIgniter 4 application, the logs are located in the writable/logs directory. They can be viewed with a FTP software which will open it in a text editor. But the way I prefer is to use the command line by connecting to the server with an SSH terminal. We can then use the cat, grep and tail commands to view the content.

$ cd my-website/writable/logs
$ cat log-2020-05-01.log
INFO - 2020-05-01 10:13:57 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2020-05-01 10:13:57 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2020-05-01 10:13:58 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
DEBUG - 2020-05-01 10:52:31 --> Test
INFO - 2020-05-01 10:52:31 --> Controller "App\Controllers\BlogController" loaded.
DEBUG - 2020-05-01 10:52:31 --> Call to some undefined function...
CRITICAL - 2020-05-01 10:52:31 --> Call to undefined method App\Controllers\BlogController::some_undefined_function()
#0 /Applications/MAMP/htdocs/my-website/system/CodeIgniter.php(910): App\Controllers\BlogController->index()
#1 /Applications/MAMP/htdocs/my-website/system/CodeIgniter.php(398): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\BlogController))
#2 /Applications/MAMP/htdocs/my-website/system/CodeIgniter.php(306): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#3 /Applications/MAMP/htdocs/my-website-public/index.php(45): CodeIgniter\CodeIgniter->run()
#4 {main}

If the application is running in debug mode the file can be very large. To extract only the errors, we can use grep -v. In CodeIgniter 3 the errors were all prefixed with the word ERROR. But in CodeIgniter 4 there are now several types of errors. It is therefore easier to just eliminate the INFO and DEBUG entries. It also allows us to see the stack trace. The -v option does a reverse search and returns all the lines, except the lines containing the words DEBUG and INFO:

$ grep -v 'DEBUG\|INFO' log-2020-05-01.php
CRITICAL - 2020-05-01 10:52:31 --> Call to undefined method App\Controllers\BlogController::some_undefined_function()
#0 /Applications/MAMP/htdocs/my-website/system/CodeIgniter.php(910): App\Controllers\BlogController->index()
#1 /Applications/MAMP/htdocs/my-website/system/CodeIgniter.php(398): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\BlogController))
#2 /Applications/MAMP/htdocs/my-website/system/CodeIgniter.php(306): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#3 /Applications/MAMP/htdocs/my-website-public/index.php(45): CodeIgniter\CodeIgniter->run()
#4 {main}

You can also view errors in real time with the tail -f command. This displays the contents of the file as new lines are written to it. We can therefore refresh the web page in the browser and see errors appear in the terminal as they occur:

$ tail -f log-2020-05-01.php

If you do not want to be flooded with various information messages, you can then combine the two commands together to show only errors:

$ tail -f log-2020-05-01.php | grep -v 'DEBUG\|INFO'

Adjust the logging level of CodeIgniter 4

We can adjust the type of information that is written to the log by modifying the threshold parameter in the app/Config/Logger.php file or logger.threshold in .env. Possible values are 0 to 9. The system writes all messages at or below the specified threshold. For example, in a development environment we can set it to 9 so that all types of messages are written to the log. In production we could set it to 4 to log only the errors, that is all the messages from 1 to 4:

Code Level Description
0 Disables logging, Error logging TURNED OFF.
1 emergency Emergency Messages - System is unusable.
2 alert Alert Messages - Action Must Be Taken Immediately.
3 critical Critical Messages - Application component unavailable, unexpected exception.
4 error Runtime Errors - Don't need immediate action, but should be monitored.
5 warning Warnings - Exceptional occurrences that are not errors.
6 notice Notices - Normal but significant events.
7 info Info - Interesting events, like user logging in, etc.
8 debug Debug - Detailed debug information.
9 All messages

Check file permissions

If the files and directories in the web application do not have the correct permissions, this can cause problems. Particularly for the writable directory. It must be writable by the web server (Apache or other). Otherwise, CodeIgniter will not be able to write its log, session and cache files. This is a problem that seems to occur quite frequently during a new installation of CI4. Unfortunately, many people suggest the easy way to set permissions to 777. But this is really not a good practice to follow. I suggest setting read and write permissions for the file owner, and read only for other users. That is 644 for the files and 755 for the directories. Obviously, it all depends on the configuration of your web server. Please make any adjustments that seem safest to you.

Without going into too much detail, permissions are defined as follows: 4 = read, 2 = write, 1 = execute. So read and write access (4 + 2) is defined as 6. We use three digits to define the permissions of the file owner, group members and the rest of the world. So the owner has read and write access rights (6), group members have read only access (4), and the rest of the world also (4) = 644. For directories, they have the peculiarity that you need execution permission to be able to go to the directory and access its content.

To modify the whole hierarchy of files and directories with these permissions, use the chmod command to change the permissions and the find -type f command to find the files and find -type d to find the directories to modify:

$ find /Applications/MAMP/htdocs/my-website/ -type f -exec chmod 644 {} \+
$ find /Applications/MAMP/htdocs/my-website/ -type d -exec chmod 755 {} \+

For more information, read the answers to these two questions:

Check the .htaccess file

Make sure that the .htaccess file is present in the public directory of your CI4 application.

Check that the content of the file corresponds to the .htaccess file of CodeIgniter 4: https://github.com/codeigniter4/CodeIgniter4/blob/develop/public/.htaccess

Check the directory name for the application in the index.php file

Check this block of code in the file public/index.php. Make sure the name and location of the app directory is correct. If you have kept the original structure, it is already good. But if you have installed the application in a different location, change this line.

<?php
// Location of the Paths config file.
// This is the line that might need to be changed, depending on your folder structure.
$pathsPath = realpath(FCPATH . '../app/Config/Paths.php');
// ^^^ Change this if you move your application folder

Check the .env file and the files in app/Config

In CodeIgniter 4, the configuration can be done in the .env file or in the files in the app/Config directory. Make sure the database credentials are correct, as well as the other configuration parameters. In particular the variable CI_ENVIRONMENT. This variable can be defined in the .env or .htaccess file. To see detailed error messages and to access the Toolbar, set the value to development. This value should be used only in development and test environments. In production, always set it to production. Otherwise you will expose sensitive information and it could be a security issue.

# .env file
CI_ENVIRONMENT = development
# .htaccess file
SetEnv CI_ENVIRONMENT development

Be careful to name the file .env with a dot at the beginning of the name. The file provided by CI4 is only a sample file and is named env without the dot. The use of this file is optional. If you decide to use it, you must make a copy called .env. Otherwise, you can simply ignore this file and directly modify the files in the app/Config directory.

$ cp env .env

How to fix the "No input file specified" error

The error "No input file specified" can occur if the Apache server is configured in CGI or FastCGI mode. I am not an Apache expert and I could not explain why. But I had this problem with my hosting provider while on my local server it worked without problems. The solution I found is to add a ? so that index.php becomes index.php? in the public/.htaccess file:

  RewriteRule ^(.*)$ index.php?/$1 [L]

See the answers to this question on Stackoverflow: https://stackoverflow.com/questions/14555996/no-input-file-specified