Installing Smarty in Cpanel

At my workplace, Smarty is used to divide the work between back-end and front-end engineers. I wanted to fiddle around with it on my free time, but there weren’t any concrete step-by-step instructions for installing Smarty in cPanel among the first few search result links on Google.

Here is what I did by following these basic instructions from Smarty’s Basic Installation page:

A typical shared host file structure looks like this in cPanel 11:

/home/username
--/access_logs
--/etc
--/mail
--/public_ftp
--/public_html
--/temp
--/www

I downloaded the latest Smarty package which in my case is Smarty 3.0.7 and uploaded the compressed file into the etc folder. I decompressed the file using the handy extract feature in the cPanel menu. I ended up with this structure:

/home/username
--/access_logs
--/etc
----/Smarty-3.0.7
------/demo
--------/cache
--------/configs
--------/templates
--------/templates_c
--------/index.php
--------/index_php_template.php
------/libs
--------/plugins
--------/sysplugins
--/mail
--/public_ftp
--/public_html
--/temp
--/www

Next, I moved the content from demo to public_html so it can be accessible to the world. This left me with this structure:

/home/username
--/access_logs
--/etc
----/Smarty-3.0.7
------/demo
------/libs
--------/plugins
--------/sysplugins
--/mail
--/public_ftp
--/public_html
----/cache
----/configs
----/templates
----/templates_c
----/index.php
----/index_php_template.php
--/temp
--/www

At this point, you should change the permissions on the cache and templates_c folders to 777 for caching purposes.

Smarty also instructs its users to either define a constant for the path to the Smarty libraries or include the absolute path in php.ini. For those who are forgetful like myself, I chose to do the latter.

If you do not have access to the actual php.ini or your host is inflexible, you can just create one in your public_html directory. Add this line to the file:

include_path = ".:/usr/lib/php:/usr/local/lib/php:/home/username/etc/Smarty-3.0.7/libs/"

The path to your PHP libraries can either be found through the PHP Information menu item on cPanel’s main page or executing phpinfo() in a script.

To hide php.ini from prying eyes, edit your .htaccess file and drop this at the end:

<FilesMatch "(\.ini)$">
Order allow,deny
</FilesMatch>

The above will return a 403 Forbidden error to anyone who tries to access files ending in .ini.

Some of you may not see the .htaccess file in cPanel. To make it viewable, you would of had to check the See hidden files checkbox on the popup after clicking on File Manager. To make the popup appear again, go to your cPanel main menu and click on the reset all interface settings link in the footer. You can also gain access to the .htaccess file by navigating to your website through an FPT program.

Now edit the index.php that is in the public_html directory. Replace this line:

require_once('../libs/Smarty.class.php');

with:

require_once('Smarty.class.php');

This can be done because the absolute path to the Smarty libraries had been established in the php.ini file earlier.

Navigate to your site in your browser and the example Smarty page should pop up. If you receive an error about a popup init in the header.tpl, just go to the file under the templates directory and remove the line. Supposedly, Smarty packages after 3.0.7 will have the line removed.

And there we go. Fun for the entire family.

Leave a Reply

Your email address will not be published. Required fields are marked *