nativeInstallerwamp

Get started with Smarty

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "installdir/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

Approach A: Bitnami installations using system packages

The Smarty template engine is not installed by default in Bitnami installations using system packages. However, it can be easily enabled using Composer. In this guide, you will learn how to install Smarty project.

Installation

NOTE: This section assumes that the application project directory will be located at installdir/projects/APPNAME. Remember to replace the APPNAME placeholder with the application name, such as myapp.

  • First, create a project directory where to store your application. Run the following commands:

    $ sudo mkdir -p installdir/projects/APPNAME
    $ sudo chown $USER installdir/projects/APPNAME
    
  • Create a sample Smarty project. Follow these steps:

    • Create required directories to be used by Smarty, and ensure the directories used for caching and template compilation can be written to by the web server:

      $ cd installdir/projects/APPNAME
      $ mkdir templates configs public templates_c cache
      $ sudo chown daemon:daemon templates_c cache
      
    • Create the installdir/projects/APPNAME/public/index.php file with the following contents:

      <?php
      
      // Load Smarty installed via Composer
      require_once('../vendor/autoload.php');
      Smarty_Autoloader::register();
      
      $smarty = new Smarty();
      
      // Configure Smarty
      $smarty->setTemplateDir('../templates');
      $smarty->setCompileDir('../templates_c');
      $smarty->setCacheDir('../cache');
      $smarty->setConfigDir('../configs');
      
      // Define a sample variable to be used by the Smarty template
      $smarty->assign('name', 'Alice');
      
      // Render the Smarty template
      $smarty->display('index.tpl');
      
    • Create a sample Smarty template file at installdir/projects/APPNAME/templates/index.tpl:

      <html>
        <head>
          <title>Smarty</title>
        </head>
        <body>
          Hello, {$name}!
        </body>
      </html>
      
  • Install Smarty to your project. Run the following command:

    $ cd installdir/projects/APPNAME
    $ composer require smarty/smarty
    
  • Create the Apache configuration for your application. Follow these steps:

    • Create the installdir/apache2/conf/vhosts/APPNAME-vhost.conf file containing the application’s HTTP virtual host configuration with the configuration block shown below:

      <VirtualHost 127.0.0.1:80 _default_:80>
        ServerAlias *
        DocumentRoot /opt/bitnami/projects/APPNAME/public
        <Directory "/opt/bitnami/projects/APPNAME/public">
          Options -Indexes +FollowSymLinks -MultiViews
          AllowOverride All
          Require all granted
        </Directory>
      </VirtualHost>
      
    • Create the installdir/apache2/conf/vhosts/APPNAME-https-vhost.conf file containing the application HTTPS virtual host configuration with the configuration block shown below:

      <VirtualHost 127.0.0.1:443 _default_:443>
        ServerAlias *
        DocumentRoot installdir/projects/APPNAME/public
        SSLEngine on
        SSLCertificateFile "installdir/apache2/conf/bitnami/certs/server.crt"
        SSLCertificateKeyFile "installdir/apache2/conf/bitnami/certs/server.key"
        <Directory "installdir/projects/APPNAME/public">
          Options -Indexes +FollowSymLinks -MultiViews
          AllowOverride All
          Require all granted
        </Directory>
      </VirtualHost>
      
  • Finally, restart the Apache server:

    $ sudo installdir/ctlscript.sh restart apache
    

You can now verify that the example application is working by visiting its URL using your browser at http://localhost/. You should see the text Hello, Alice!

Approach B: Self-contained Bitnami installations

The Smarty framework is installed in the frameworks/smarty directory of the installation directory. This directory includes two example applications, located in the sample/ and demo/ directories respectively. In this guide, you will learn how to enable a Smarty project.

Activation and Testing

To enable the example application, edit the Apache configuration file at installdir/apache2/conf/bitnami/bitnami-apps-prefix.conf and uncomment the following line

Include "installdir/frameworks/smarty/conf/httpd-prefix.conf"

Then, restart the Apache server.

$ sudo installdir/ctlscript.sh restart apache

You can now verify that the example application is working by visiting its URL using your browser at http://localhost/smarty.

Here is an example of what you might see:

Smarty welcome page

To enable the second example application, follow these steps:

  • Edit the Apache configuration file at installdir/frameworks/smarty/conf/httpd-prefix.conf and modify it to point to the demo/ directory, as shown below:

    Alias /smarty/ "installdir/frameworks/smarty/demo/"
    Alias /smarty "installdir/frameworks/smarty/demo"
    Include "installdir/frameworks/smarty/conf/httpd-app.conf"
    
  • Edit the installdir/frameworks/smarty/conf/httpd-app.conf file and modify the directive to reflect the new path:

    <Directory "installdir/frameworks/smarty/demo">
    ...
    </Directory>
    
  • You should also modify the permissions and ownership of the demo/ directory so that it is writable by the Web server user:

    $ cd installdir/frameworks/smarty
    $ sudo chmod -R 775 demo
    $ sudo chown -R $USER:daemon demo
    
  • Restart the Apache server.

    $ sudo installdir/ctlscript.sh restart apache
    

You can now verify that the second example application is working by visiting the same URL as before. Here is an example of what you should see.

Smarty welcome page

More Information

Learn more about developing applications with Smarty at https://www.smarty.net/quick_install.

Last modification July 29, 2020