How to Install CodeIgniter on an Ubuntu 12.04 Cloud Server

About CodeIgniter


CodeIgniter  is an open source web application framework for PHP that is small in size but very powerful in utility. Its goal is to enable people to write their applications much faster than normal by providing a set of libraries and helpers for the most common tasks. It is based on the  Model-View-Controller  approach for a great separation of logic from presentation. 

This tutorial will show you how to setup CodeIgniter on your cloud server using the command line. It will also go through some of the initial configuration you are likely to want to make. 

This tutorial assumes that you are already running your own VPS with root access and have LAMP (Linux, Apache, MySQL and PHP) installed on it. You can consult this tutorial  that will get you going with the latter if you haven't already.

Installing CodeIgniter (CI)


First, you need to navigate to your cloud server's directory root folder, for instance:
cd /var/www

Download the latest stable release of CI. Go to the <a href="http://ellislab.com/codeigniter/user-guide/installation/downloads.html" target="" _blank"="" style="color: rgb(0, 136, 204); text-decoration: none; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 17px; line-height: 23px;">downloads page  of the CI website and check for the latest release. Then, run the following command to download it:
wget http://ellislab.com/asset/ci_download_files/reactor/CodeIgniter_2.1.2.zip

Make sure you replace the name of the zip file at the end of the URL with the latest stable release (Current version) you find there. 

If you run the  ls  command, you should see the zip file in your folder. Unzip it with the following command:
unzip CodeIgniter_2.1.2.zip

If you get the " unzip: command not found " error, it means you don't have  Unzip installed. Just run the following command to install it:
sudo apt-get install unzip

Now try it again. 

If you run the  ls  command, you'll notice the new CodeIgniter folder extracted there. You can rename that folder to  codeigniter  (or whatever you want) with the following command:
mv /var/www/CodeIgniter_2.1.2 /var/www/codeigniter

Now you can point your browser to that folder:
<your_domain>/codeigniter

There, you will see the CodeIgniter welcome message. 

This message is produced by an example Controller you can find in the application/controllers  folder called  welcome.php . This Controller just loads a view located in the  application/view s folder that contains a simple HTML message.

Configuration


Now that CodeIgniter is set up, you should do some initial configuration for your application. If you plan to work with a database, you need to set one up and provide the information for CodeIgniter to be able to communicate with it. Please consult  this tutorial  in case you are unfamiliar with how to create a MySQL database. Once you have that, edit the following file (make sure you replace the " codeigniter " folder name with the one you have installed it in):
nano /var/www/codeigniter/application/config/database.php

Then find the following block and edit it to include your database information:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'your_username';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'your_database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Save the file, exit and you are done with the database configuration. Next, open the config.php  file and make some changes there:
nano /var/www/codeigniter/application/config/config.php

Now, set your base url. Find this block and edit according to your situation:
$config['base_url'] = 'http://www.example.com';

This is what you would set if you had the  example.com  domain name pointing to the folder in which you have installed CodeIgniter. In other words, if you had created a virtual host for the  example.com  domain name, that would have its document root in, say  /var/www/codeigniter . To learn how to create virtual hosts in Apache, you can consult  this tutorial

Save the file and exit.


Side Note: Virtual Hosts

In the URLs listed below, the document root for code igniter is pointing to /var/www/codeigniter . This can be done by changing the document root in the virtual host file:
sudo nano /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
        DocumentRoot /var/www/codeigniter
        [.......]
<VirtualHost *:80>


The next thing you will probably want to change is to remove the  index.php  segment you need to put in the URL right before your Controller name. You see, the CodeIgniter URL has the following structure:
base url / index.php / controller name / controller function / controller parameter 1 / controller parameter 2 / controller parameter etc 

To test this out, you can open the  welcome.php  controller file:
nano /var/www/codeigniter/application/controllers/welcome.php

And below the index function, add another function like so:
public function test($param) {
  echo $param;
}

If you point your browser to  http://www.example.com/index.php/welcome/test/3 , you should see displayed the number 3 on the page. Replace the last segment and it will display that instead. And that's pretty cool. 

But you may want to remove the " index.php " part so your URL looks nice and clean. There are 2 steps to do this. First, reopen the config file you had previously edited:
nano /var/www/codeigniter/application/config/config.php

Find the following code:
$config['index_page'] = 'index.php';

And replace it with:
$config['index_page'] = '';

Save and exit. Now CodeIgniter will no longer automatically include " index.php " in the URL. However, that's not enough. It just means that you will get a bunch of " Page not found " errors if you simply omit it from the URL. You will also need to create an  .htaccess  file to handle some redirects for you. 

Note:  To use the  .htaccess  functionality, you'll need  mod_rewrite  enabled on your Apache server. To check for this, run the following command:
apache2ctl -M

If you see  rewrite_module  in the list, you are good to go. If not, just enable it with the following command:
a2enmod rewrite

And restart Apache for the changes to take effect:
sudo service apache2 restart

You can now continue. Create the  .htaccess  file in the CodeIgniter root folder (next to the  index.php  file):
nano /var/www/codeigniter/.htaccess

And paste in the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L] 

Ensure that your .htaccess file is enabled by setting  AllowOverride  to  All  in the virtual hosts file:
<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
</Directory>

Save the file and exit. Now, if you visit the site in the browser, you don't need to include  index.php  in the url:
http://www.example.com/welcome/test/3 

And your URL looks much better. You are ready to start developing your PHP application using the wonders of CodeIgniter.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值