wtorek, 28 marca 2017

lazarus + Brook framework: reactivation :-)

It was long time, when I decided to create website application using Lazarus and Brook framework. In the mean time a lot of happen in my life, so this idea wasnt realized.
Now, 3 years later, we have a new versions of Lazarus IDE and Brook framework. And maybe will be enough time to do it again :-)

I founded very nice book: "CMS design using PHP and JQuery" by Kae Verens so I decided to try make similar thing using Lazarus and Brook to learn how CMS works and how Brook works. It will be big challenge, because Im not professional developer, but hobbyist :-)

It's the big difference between developing PHP application and Pascal application. In first case, the application is run through php module of Apache, and it's interpreted by server before pages are showed by client request.
Pascal application is compiled, so Im not sure if all techniques useful for PHP could be transformed to use in Pascal web application. We will see.


Configuration of local development environment 

I have installed Linux Mint 18 on my laptop, Apache 2.4 and Mysql rdbms.
Before we start developing our application some preparation steps are necessary.

Step 1. Apache configuration 

We have few ways for using our application: we can make is as standalone application with embedded web server, we can build it as an Apache module or use CGI or FastCGI standard to serve our web application to the world.
Maybe I will write about all this cases later, but for this blog, I decided to make it as FastCGI application. So, first I need to  install suitable modules for Apache.
They are two modules for fastCGI applications:
  •  libapache2-mod-fastcgi
  • libapache2-mod-fcgid 
 I choose fcgid apache module, discussions about both you can find in the Internet. Yet another module will be usefull: mod-rewrite. In my system it is provided with apache2 installation. Next step will be enabling this modules. In the console window we write:

a2enmodule rewrite
a2enmodule fcgid

after that restarting of apache2 service will be necessary. For example:

apachectl restart

And last step remain: configure virtual site to test my application. I created config file mylazcms.conf in the /etc/apache2/sites-avaiable directory with this content:

<VirtualHost mylazcms.localhost>
   ServerName mylazcms.localhost
    DocumentRoot  /var/www/mylazcms/public

    <IfModule mod_fcgid.c>
    

    <Directory /var/www/mylazcms/appbf/>
           AddHandler fcgid-script .fcgi
           Options +ExecCGI -Indexes
           AllowOverride all
           Order allow,deny
           Allow from all
    </Directory>
    </IfModule>


   ScriptAlias /myapp/ "/var/www/mylazcms/appbf/"
   ErrorLog ${APACHE_LOG_DIR}/mylazcms.error.log
   LogLevel warn rewrite:trace3 fcgid:info
   CustomLog ${APACHE_LOG_DIR}/mylazcms.access.log combined

ServerSignature Off


</VirtualHost>


You should look for documentation of virtual host in Apache, below few explanations.
Because I have some other www application on my laptop, I named this one as mylazcms.localhost. This need to make some changes in the host file, located in my system in the /etc directory. In this file I added line:

mylazcms.localhost 127.0.0.1
 
for server name <-> IP number translation.

In the /var/www/ - location of all www sites for Apache server in Linux Mint OS -  (this will be specific for your system configuration, you should check documentation) I created directory mylazcms and subdirectories: public and appbf. So directory structure  looks like this:

  - /var/www
         - /mylazcms 
               -/public
               -/appbf


In the appbf directory we will put our application, this location should be available only for Apache, not for users for security reasons.
Directory public will be available for everyone.

This lines can serve our application with Apache:

AddHandler fcgid-script .fcgi
Options +ExecCGI -Indexes

First line define all applications with extension *.fcgi  to be handled through fcgid module, the second defines directory /var/www/mycms/appbf as directory of executable files.

Line:

ScriptAlias /myapp/ "/var/www/mylazcms/appbf/"

hides real path to our application under alias /myapp so we can call it under address like this : www.ourserver.com/myapp


Next lines define separated log file and access log file for our application in apache2 log directory.
Line:

LogLevel warn rewrite:trace3 fcgid:info

help us catch more information from fcgid module and rewrite module. It's for debug purpose only .

Step 2: Lazarus and Brook installation and configuration

On the page http://www.lazarus-ide.org/index.php?page=downloads you can find links for download all necessary packages specific for your operation system.
You have to install three packages: lazarus, fpc, fpc-src.
When Lazaus IDE start first time, you can choose location of FPC (FreePascal compiler)  and FP packages source directories. IDE needs to know where they are. It should be founded by Lazarus, if everything gone well.

Now we have to download and install Brook framework. When Im writing this, is available 3.0 stable release of Brook. You can find it here: https://silvioprog.github.io/brookframework/. You can download packed sources or use git for this. After downloading and unpacking it's time to run Lazarus IDE. In the main menu choose: Package->Open package file (.lpk) and find brookex.lpk in /your-directory-with brook/packages directory.
Open and install in the IDE. Lazarus will be recompile yourself and when appear again, you should find new items in the main menu File->New... In the tree should be visible Brook framework items.

Now it's time for:

First fastcgi application

This is first step on the way to create web application with Lazarus/Brook :-)
So, in the main menu of IDE, choose  File->New... and from Brook framework branch choose Full CGI/FastCGI application. Small wizard will open, that help you to make base configuration of your application.

I chose FastCGI, UTF-8 options. next name of our application: testbrook for example, and path for the public directory for our application: /var/www/mylazcms/public - the same as in the virtual host configuration as DocumentRoot. 

In the next window we can configure actions of our application for requests.
I added default get action only for test. Next and OK create default test project.
Now build application, in main menu: Run->Build. Save project as testbrook.lpi.
This steps make testbrook application in the directory of testbrook.lpi file.
Change name of this file, save it as: testbrook.fcgi.
Copy this file to the /appbf subdirectory of your webapp, in my case: /var/www/mylazcms/appbf.
Restart the apache service as before, and open your browser.
In the address bar type: mylazcms.localhost/myapp/testbrook. This should show you the default page:




This is old name of my testing project on this screenshot, I named it mybrtest before I started write this :-)


So, it is all folks! Its working! :-)

2 komentarze:

  1. Nice tutorial. Thanks...
    Do you plan to continue adding more complex examples?
    Thanks. Eduardo.

    OdpowiedzUsuń
  2. I agree. This is excellent. Please build some more demos.

    OdpowiedzUsuń