Hosting CodeIgniter inside a sub directory with Nginx


 

As I mentioned in my previous post, I had been setting up an old Codeigniter web app today in my local machine. This app was hosted with the Apache web server and it was running on PHP 5.6.


I tried to put this in a VM in my local machine using Vagrant. However, I faced some issues with this setup as I wanted to make it run with Nginx instead of Apache, and that too, inside a sub directory. The main problem I had was with the URL rewrites. In Apache, I was able to write an httaccess file and that would simply handle the rewrites. 


However after some trial and error efforts, it turned out that it was really easy. Even easier than I actually thought. All I had to do was set the correct paths in the nginx config.

 

My CI app as residing inside a directory called 'admin'. So in my nginx host config file, I provided a location block as below.

 

location /admin {
        try_files $uri $uri/ /admin/index.php?/$query_string;
}


and voila, it started working. 

 

I thought of adding this to my blog as a note to myself. Anyway, If you ever come across this post, and have any issues, feel free to leave a comment.

Fix - CodeIgniter session not set with PHP 7.x


Today I had to do some updates to an old web app I made with CodeIgniter. The original app was made around 5 years back and there has been no changes to it until this one. All this time it was living in one of my old web servers with PHP 5.6 and had no issues at all. However, when I tried to setup this in my local machine (inside Vagrant, with PHP 7.x), I found some issues with the sessions. I noticed the sessions are not getting persisted between the redirects. 

 

After some research, I found out that the issue was actually with the PHP version. Luckily, I didn't have to change it to an old PHP version. There was an easy fix (somewhat surprising), and I got it working in no time. 

 

If you ever face this issue, all you got to do is: in your CI folder, open the CI/system/libraries/Session/Session.php file and look for a line which says:

 

ini_set('session.name', $params['cookie_name']);

and change it to

 

ini_set('session.id', $params['cookie_name']);

 

and that's it. Your app should start working fine :)

Pretty print Curl JSON responses in terminal

Today I wanted to test one of my APIs and used curl to get the response in the terminal. The output wasn't clear at all because it usually returns everything in one line. 

 

Fortunately, there was a way to pretty-print the response, as in real json format. All you have to do is pipe the json_pp command after your curl command.

 

Ex: 


curl --location --request GET 'http://my.api.local/api' --header '...' | json_pp

 

and that's it. You should see a formatted json response.

 

Pretty print Curl JSON responses

Hope this will help someone. Please feel free to comment and let me know if you know any other better way :)