Getting Started With CodeIgniter: Part 1 - Installation

CodeIgniter is an “Open Source Web Application Framework that helps you write kick-ass PHP programs.” The website says it all. CodeIgniter is BALLER. However, it’s documentation is awesome after you get going, but getting going is a little tougher. That’s what I’m here for. Hopefully, after this tutorial, you’ll know your way around CodeIgniter and have a pretty good feel for the MVC architecture.

In part one, we’re going to learn about installation and configuration of CI, and also get a feel for the folder structure (which always seems to scare people away). So let’s get going!

Some Background Info

For those unfamiliar with PHP frameworks or the MVC (Model, View, Controller) concept in general, here’s a little background. MVC architecture is a tried and true method of organizing source code and program files. Basically, it separates the front end from the back end.

CodeIgniter implements the MVC architecture pretty well, and then throws in TONS of PHP classes and helpers to get things done more quickly. You’ve got an image manipulation class that gives you functions to resize, crop, and watermark an image easily. You’ve got a captcha class that makes it pretty simple to generate captchas for web forms. You’ve even got a ZIP encoding class that makes it simple to create zip packages dynamically in your web site. I wouldn’t be surprised if CI 2.0 came complete with kitchen_sink.php! Shut up. That was funny.

Taking A Look Around

Now let’s take a look at CodeIgniter to see what we’re working with. Download the newest version from the CI site and extract it onto your server somewhere. Now open it up with a file browser and take a looksee!

You should see four things: A “system” folder (which contains pretty much EVERYTHING, including CI’s code and the folders to put your code into), a user guide (which you can delete because the whole thing is available on the CI website), a license.txt, and an index.php (which routes each request to wherever it needs to go.

Open up the system folder. Now, see the “application” folder? That’s your home base. Don’t touch any of the other folders in the system directory unless you’re trying to figure out how something works. As a matter of fact, don’t even do that. It’s scary. Just open the application folder. There are quite a few folders inside here. In the beginning, the only ones that you need to be worried about are config, models, views, and controllers. As you develop in your CI skills, you may develop your own libraries or helpers which go in their appropriate folders, but don’t get ahead of yourself! Patience is key! KEY!!!

Configuration and Installation

The first folder we’re going to look into is the config folder. Specifically, we’re going to mess with config.php, autoload.php, and database.php. Open up config.php and look around. This is the main place to screw around with options that CI gives you. First of all, change the value of $config['base_url'] to wherever your CI install is. If you’re running on localhost and you put it in a folder called “ci”, for example, you’ll want to use “http://localhost/ci/”. If it’s a live site, it will be something like “http://www.yoursite.com/your_ci_directory/”. Whatever it is, MAKE SURE you include the forward slash after the directory name, and MAKE SURE you include “http://”.

Moving along, we see that $config[’index_page’] is set to “index.php”. This is fine for now, because by default, CodeIgniter routes everything through the index.php page at the document route. This means that your sites will look like “www.yoursite.com/index.php/some_page”. If you want to take index.php out of the address (I always do! It’s ugly!), you can change $config[’index_page’] to an empty string (just two quotation marks with nothing in between them for those who have never ever coded before ever) instead of “index.php” and make a “.htaccess” file to remove it from the url like the CI wiki talks about.

Scoot on down to $config[’log_threshold’] = 0; and change it to 1. This will just enable error messages like PHP errors to be logged. Any lower than 1 and you won’t see any logs, any higher than 1 and your logs will fill up like crazy. So stick to 1. It’s just good practice.

UPDATE: Apparently some servers have a problem with setting log_threshold to 1, so if you get nothing but blank pages after the install, change it back to 0 and see if that fixes it. If anyone can explain this, please let me know.

UPDATE #2: Thanks to cheekygeek (see comments below), we have an answer.

A solution to the log_threshold site-breaking problem is to make the system/logs folder world-writable. However, making folders in web space world-writable (777) is never a good idea…the (more secure, I think) fix on a UNIX-based system is to change the GROUP ownership on the logs file to the web server user. If you are using Apache, you’ll find it defined in the httpd.conf.

Thanks for the info, cheekygeek!

Back to business, go ahead and change the encryption key ($config[’encryption_key’]) to a random 32 character alphanumeric string. Lots of applications like user authentication or anything to do with passwords use the encryption key by sticking it on the end of the password (it’s called a salt) and then hashing it for security. It just makes password storage a little safer.

Next we’re going to get out of config.php and move along to database.php, also in the config folder. If your application isn’t going to use the database, you can Open that one up. Most users will just have to change the first four database config options (hostname, username, password, database). If you’re not on MySQL, you’ll have to change dbdriver, and if you are prefixing all of your tables with something, you’ll have to change dbprefix. All the rest should be alright.

Finally, we’ll open up autoload.php. This mostly just tells CI which of the helpers and libraries you’d like to have automatically loaded. Helpers and libraries can be loaded on a controller by controller basis, but if there are any that you tend to use in most controllers, you might as well just autoload them. If your site uses a database whatsoever, you should probably autoload “database” in the library section. I also usually autoload “url” in the helper section. Visit the CodeIgniter user guide to see what all of these things do. You’ll probably need to spend a little while familiarizing yourself with all of these in order to take the full advantage of CI. NOTE: At the top of the User Guide, there’s a little “Table of Contents” button that brings a dropdown navigation menu. I mention it here because I spent the first two weeks banging my head on a wall until I found that.

Now fire up the browser, point it to wherever you installed CI with index.php on the end (something like http://localhost/ci/index.php). NOTE: If you used a .htaccess to take index.php out of the url, you’ll obviously won’t need to put it in the address. Did you get the welcome page? If you do, you’re rockin’ the CI! How does it feel! YESSSS! If you didn’t, post a comment here or post on the awesome CodeIgniter forums and it shouldn’t be too hard to work out.

By default, CI loads the controller “welcome.php”. To change this to your own home page, open up “routes.php” in the config folder and change the default controller. We’ll talk more about controllers (and models and views too!) in the next section so stay tuned.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Fark
  • Furl
  • YahooMyWeb

14 Responses to “Getting Started With CodeIgniter: Part 1 - Installation”

  1. cheekygeek Says:

    A solution to the log_threshold site-breaking problem (found on the CodeIgniter forum searching for “log_threshold”) is to make the system/logs folder world-writable. However, making folders in web space world-writable (777) is never a good idea (unless you like redirect spammers coming along and installing potentially embarrassing stuff inside your web space). DAMKIKT :)

    Not sure if Windows users have this problem, but the (more secure, I think) fix on a UNIX-based system is to change the GROUP ownership on the logs file to the web server user. If you are using Apache, you’ll find it defined in the httpd.conf. On Macs, it is: http://www.

    chgrp www /path/to/your/codeigniter/system/logs

    If the logs folder is owned by root, you will need to instead:

    sudo chgrp www /path/to/your/codeigniter/system/logs
    (and authenticate). Next you need to modify the read/write/execute permissions on the logs folder so that the web server user has full access:
    chmod 775 /path/to/your/codeigniter/system/logs
    Now you can set your config.php file log_threshold to something other than zero and the application still works, but you haven’t left it world-writable for spammers.

    Hope someone finds this useful.

  2. cheekygeek Says:

    In the comment above the line that says
    On Macs, it is: http://www
    should read
    On Macs, it is “www”.
    Not sure how the http got in there!

  3. Mike Says:

    @cheekygeek

    Thanks for the info! You have officially been blockquoted :).

    P.S. I hang out a pretty good bit on the CI forums. Are you ever around there? My name is MCrittenden.

  4. ClearPixels » Blog Archive » Now learning: CodeIgniter Says:

    […] to spend a lot of time in learning a framework, go for CodeIgniter. You will love it. Follow this link for a crash course. It is extremely well written and easy to understand. Share and Enjoy: […]

  5. Philippine Developer Says:

    Post more CI pages. This is very helpful!

  6. Mike Says:

    @Phillipine

    What would you like to see? After the first 5 CI posts, I couldn’t think of anything else that needed explaining. Let me know what you want and I’ll get on it.

    Thanks for reading :).

  7. Nick Twigg Says:

    Yup, went to load it for the first time and got a white screen. Checked out all my config, everything looked good, and thought I’d try the logs fix mentioned above. That fixed it perfectly! Now - On to Part 2!

    Thanks!

    Nick

  8. Mike Says:

    @Nick

    Yep, the blank screen isn’t too helpful for debugging. Glad I could help.

  9. ndemet Says:

    Thanks for the CodeIgniter tutorials and cheekygeek for the solution above.

    After using cheekygeek’s solution (works great, thanks) I now noticed a log file. Researching the log reveals a similar read only problem with the cache directory. Using cheekygeek’s solution on the cache directory worked just as well as for the log directory, thanks.

  10. Mike Says:

    @ndemet

    That’s interesting. Thanks for the heads up.

  11. Surekha Matte Says:

    I am new to CodeIgniter, this is very helpfull for me.

  12. Simon Says:

    Hey! Great little quick start! I’ve been coding php for years but just recently started using CI. It’s the community support (like this) that really makes CI stand out.

  13. featureBlend Says:

    Thanks for sharing here. Woah how’d you miss the TOC reference with the jquery dropdown? That was the first thing i saw. But good that you pointed that out, cuz that little black box can be hard to see.

  14. Chris Says:

    Thanks so much for the tutorial. This was very helpful and well thought out. Just as an fyi to anyone setting up CI on a godaddy server: I just had a nightmare trying to get CI installed and working. (turns out my problem was that my server was with Godaddy) Finally found the fix in the CI Wiki ( http://codeigniter.com/wiki/Godaddy_Installaton_Tips/ ) Thanks again for the intro tutorial.

Leave a Reply