Live Server Transfer of a CI Project from Localhost – The Efficient Way

Live server transferring of a web project makes the website ready for production. This is the last process in order to avail the website for the general public after building and testing the application on the local server. Despite the platform and tools used to develop the website, the live server transfer of a project from localhost through FTP has a common procedure to follow.

The simplicity of CodeIgniter framework, ease of learning and use as well as flexibility make it the favorite framework of many developers to create web applications in PHP. Additionally, developing CI projects locally increases the speed of application development. However, we’ve seen many novice developers making numerous changes in project files in order to make it live.

Live Server Transfer of a CI Project from Localhost

What are We Doing for Live Server Transfer

CodeIgniter offers an easy mechanism to maintain parameters for development as well as production environments. You should take advantage of it to simplify the process of live server transfer of the CI project. In this article, we’re explaining you the efficient way CodeIgniter offers to easier the live server transfer process and making the site production ready with minor changes.

First of all, we will understand how does CodeIgniter handle multiple environments. Also, we will create separate configuration files for the production as well as development environments. Further, we will set up a .htaccess file for the live server after uploading files through FTP. Finally, we will change the environment from development to production.

So the article contains four major steps for a successful transfer of a project from localhost to the live:

  • Understanding how does CodeIgniter handle multiple environments
  • Creating environment specific configuration and database files
  • Transfer files to the live web server and updating .htaccess
  • Changing the environment to production for the live site

How does CodeIgniter Handle Multiple Environments

CodeIgniter has a decent and efficient way to handle multiple environments. This offers us a different system behavior for the environment the application is running. CI has the primary configuration files in the application/config path. We recommend you use these files for configuration in the production environment.

For an environment-specific configuration file, we need to create the respective configuration file as application/config/{ENVIRONMENT}/{FILENAME}.php. So for a development environment, we have to first create the directory application/config/development.

Remember that CodeIgniter first loads global configuration files that are present directly inside the application/config directory. Then it tries to load the configuration files for the current environment. So no need to load all the configuration files in the environment directory nor all items in each of the file. However, there are exceptions too and you will get this sooner.

The config.php file is the first file we amend in order to the project work in CI. Specify parameters like base_url, index_page etc. in this file for the production environment. CodeIgniter will use this file to process requests on the live server.

Now, to create a development-only config.php that will work in the development environment only:

  • Create the directory application/config/development/
  • Create the file config.php there
  • From the primary config.php file, copy array parameters that need to change and paste to the newly created file and update values

So my development specific config.php file generally looks like this:

Do the same with other configuration files that change as per the environment. Additionally, specify only changing items, not all the items present in the respective global configuration file. We’re explaining this with the database.php file.

Setting Up the database.php File for Live Website and Localhost

The production and development environments mostly have separate database configurations. You have two approaches to set up these database connections in CI.

1. Use an Environment Specific database.php File

In this approach, you specify database credentials for the live website in the primary database.php file. Also, for the local development, you create the same in the environment specific directory with the changed credentials.

So a database file for the development environment in the application/config/development/ directory would be:

Also, there is something to note. CodeIgniter doesn’t load the primary database.php file first if the current environment specific one is present. It loads only the current environment specific database.php and uses given information to set up a database connection.

Due to this reason, you need to assign the active_group and query_builder variables even they are present and the same in the primary file.

Additionally, this behavior might appear for a few other files as well, just we haven’t checked that.

2. Use Different active_group for Database Connection

Alternatively, rather specifying an environment specific one, you can use different active_group for connections in the primary database.php file. Also, you would need to provide credentials for each group there.

In this case, the application/config/database.php file would look like:

Here, despite the environment, the system will try to connect with the database using the credentials given for the active_group. Currently, it is set to dev. After the transfer to the live server, I will change the active group to pro there.

Transfer Files to the Live Server and Update .htaccess

You have done building testing debugging the site and ready to make it live. So transfer the CI project files and resources to the live server. Since you’re using environment-specific direction, you need minor changes to let your live web server respond requests. The first one is the .htaccess file.

The .htaccess file in the production environment stands to differ from the development one. Since we use SEO-friendly URLs, we need index.php prepended to let CodeIgniter server the request. Below are the rules we use in the file. Although, they vary with hosts.

Additionally, you might also need www or non-www redirections for SEO perspective. The given links provide you easy rewrite rules to manage them.

Changing Environment for the Live Web Server

The second thing to change is the index.php file at the document root of the project on the live web server. First, we need to change the environment from the development to production. So look for the following line in the file:

The easiest way to change the environment is hardcoding ‘production’ there as follow:

The only drawback to this method is you have to take care of this line again if you upgrade the CI framework.

Alternatively, you can set a server environment variable named CI_ENV in the .htaccess file. The Apache implementation is as below. In this case, you don’t need to touch the index.php file unless you decide to change the application and system directories location.

Or if you have full access to the server, the virtual host configuration is below:

Additionally, you might also need to change the active_group value if you’re using different active_group for database connection as described earlier.

Now try to access the website using the browser. The site should load fine. So these are steps to perform a live server transfer and minimum efforts to let Codeigniter work there. You have also learned the efficient handling of multiple environments by CodeIgniter.

CodeIgniter has far less overhead involved compared to WordPress when it comes to the live server transfer from the localhost. However, sometimes CI fails to serve requests in the production environment, mostly due to different host configurations. Are you facing any such trouble? Do let us know. We welcome your feedback as well.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.