CppCMS C++ web application

CppCMS web application

Develop and maintain CppCMS C++ web application on FreeBSD. A step-by-step article shows how to create, code, and deploy the CppCMS web application on the production FreeBSD server.

Topics covered:
  • 1.1C++ web application directory
    • 1.1.1Web application service
    • 1.1.2Main application service
  • 1.2Build CppCMS application
  • 1.3Start CppCMS application
  • 1.4Application monitoring

Affiliate links

Setting up a CppCMS C++ web application server requires a VPS with root access. Use our affiliate links to purchase a VPS or cloud server from third-party vendors. The affiliate commissions we earn facilitate, Free website access for everyone.

The affiliate links are listed in alphabetical order without any favor. Users are encouraged to refer to the Global Webdynamics LLP Terms of Service governing the Third-party vendors.

1.1C++ web application directory

To learn how to install, configure, and run the CppCMS C++ web application server, refer to the chapter Install CppCMS on FreeBSD.

Create a CppCMS application directory ( /usr/local/gwinc/web/example ) under the web application root directory ( /usr/local/gwinc/web ). You may choose any directory structure as per your naming convention.

Terminal ~ 1.1-1
$ mkdir -p /usr/local/gwinc/web/example

Create source code directories ( code/inc and code/src ) and an HTML template directory ( data/tpl ) under the web application directory. The bin and build directories are application binary files and build directories. The deploy/release directory will deploy the C++ application to the production server.

Terminal ~ 1.1-2
$ mkdir -p /usr/local/gwinc/web/example/code/{inc,src}
$ mkdir -p /usr/local/gwinc/web/example/data/tpl
$ mkdir -p /usr/local/gwinc/web/example/{bin,build}
$ mkdir -p /usr/local/gwinc/web/example/deploy/release

The C++ web application tree directory structure looks like as shown below.

Terminal ~ 1.1-3
$ tree -L 2 /usr/local/gwinc/web/example
/usr/local/gwinc/web/example/
├── bin
├── build
├── code
│   ├── inc
│   └── src
├── data
│   └── tpl
└── deploy
    └── release

10 directories, 0 files

1.1.1Web application service

The initial implementation of the C++ web application will be simplistic and display a Hello, World! text on the example.com website. The quick-start guide won't have URL routers, controllers, models, or views.

Create web application service files ( inc/WebApp.hpp and src/WebApp.cpp ) under the C++ web application directory. You may name the files as per your naming convention.

Terminal ~ 1.1-4
$ nano -w -c /usr/local/gwinc/web/example/code/inc/WebApp.hpp
#ifndef GWINC_WEBAPP_HPP
#define GWINC_WEBAPP_HPP
#include <string>
#include <cppcms/application.h>

namespace gwinc
{

using std::string;

/// Web application service.
/// @class WebApp
class WebApp : public cppcms::application
{
    public:

    /// Non-conversion constructor.
    /// @param val Web application service.
    explicit WebApp(cppcms::service& srv);

    /// Virtual destructor.
    virtual ~WebApp();

    /// This is called implicitly by CppCMS application.
    /// @param url A url-path.
    virtual void main(string url) override;
};

} // namespace gwinc

#endif // GWINC_WEBAPP_HPP
Terminal ~ 1.1-5
$ nano -w -c /usr/local/gwinc/web/example/code/src/WebApp.cpp
#include "inc/WebApp.hpp"
#include <cppcms/http_response.h>

namespace gwinc
{

WebApp::WebApp(cppcms::service& srv) :
cppcms::application(srv)
{

}

WebApp::~WebApp()
{

}

void WebApp::main(string url)
{
    // Response data.
    string resData;

    if(url == "/")
    {
        resData = "<h3>Welcome!, C++ web application.</h3>\n";
    }
    else if(url == "/hello-world")
    {
        resData = "<h3>Hello, World!</h3>\n";
    }

    // Send response data to Lighttpd web server.
    this->response().out() << resData;
}

} // namespace gwinc

1.1.2Main application service

Create a CppCMS application service to mount the web application class ( WebApp ) and run the C++ application server service that services the request from the Lighttpd web server through FastCGI.

Terminal ~ 1.1-6
$ nano -w -c /usr/local/gwinc/web/example/code/inc/main.hpp
#ifndef GWINC_MAIN_HPP
#define GWINC_MAIN_HPP
#include <stdexcept>
#include <iostream>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include "inc/WebApp.hpp"

using std::cout;
using std::endl;
using std::exception;
using gwinc::WebApp;

#endif // GWINC_MAIN_HPP
Terminal ~ 1.1-7
$ nano -w -c /usr/local/gwinc/web/example/code/src/main.cpp
#include "inc/main.hpp"

int main(int argc, char** argv)
{
    cout << endl << "app_name" << " " << "app_version" << endl;

    try
    {
        cppcms::service srv(argc, argv);
        srv.applications_pool().mount(cppcms::create_pool<WebApp>());
        srv.run();
    }
    catch(const exception& e)
    {
        cout << "Web application error:" << endl;
        cout << e.what() << endl;
    }
}

1.2Build CppCMS application

Build a CppCMS C++ web application with the IDE of your choice, or you can use Makefile, CMake, or compile the project using command line build tools. The build directory naming convention and the build directory structure depend on the IDE or the build tools used to compile the application.

Terminal ~ 1.2-1
$ cd /usr/local/gwinc/web/example
$ clang++ -Wall -std=c++14 -fexceptions -fvisibility=hidden -Wno-deprecated -Wno-c++17-extensions -O2 -I./code -I/usr/local/include -I/usr/include/c++/v1 -c code/src/main.cpp -o build/main.o
$ clang++ -Wall -std=c++14 -fexceptions -fvisibility=hidden -Wno-deprecated -Wno-c++17-extensions -O2 -I./code -I/usr/local/include -I/usr/include/c++/v1 -c code/src/WebApp.cpp -o build/WebApp.o
$ clang++ -o bin/example build/main.o build/WebApp.o -L/usr/local/lib -lcppcms -lbooster -s

Copy the C++ web application binary file ( example ) from bin directory to the web application directory ( /usr/local/gwinc/web/example ).

Terminal ~ 1.2-2
$ cp /usr/local/gwinc/web/example/bin/example /usr/local/gwinc/web/example
$ chmod 755 /usr/local/gwinc/web/example/example

1.3Start CppCMS application

The CppCMS application configuration file ( web-config.json ) under the web application directory ( /usr/local/gwinc/web/example ) and the Lighttpd virtual host configuration file ( vhosts.d/example.com.conf ) are required to be set up before starting the lighttpd web server.

Start the Lighttpd web server. When the lighhtpd service is started, the web server will run the CppCMS example web application process using FastCGI protocol through a Unix socket and the website can be accessed through the URL https://example.com.

Terminal ~ 1.3-1
$ sudo service lighttpd start

Access the website example.com through any web browser, or for quick testing, use the wget network tool to download the website content.

Terminal ~ 1.3-2
$ wget --no-check-certificate -qO - https://example.com
<h3>Welcome!, C++ web application.</h3>

$ wget --no-check-certificate -qO - https://example.com/hello-world
<h3>Hello, World!, C++ web application.</h3>

1.4Application monitoring

The socket root is a system temporary directory ( /tmp ) as defined in the Lighttpd configuration file ( lighttpd.conf ). List all CppCMS FastCGI socket file descriptors.

Terminal ~ 1.4-1
$ ls -l /tmp/*-fcgi-socket-0
...
srwxr-xr-x  1 www  wheel  0 Aug 15 16:38 /tmp/example-fcgi-socket-0
...

Using top command, check whether Lighttpd and the CppCMS application processes are running. Refer to the last column COMMAND, where lighttpd and example processes will be listed.

Terminal ~ 1.4-2
$ top -U www
last pid: 13690;  load averages:  0.26,  0.33,  0.25; battery: 98%
109 processes: 2 running, 106 sleeping, 1 zombie
CPU:  4.2% user,  0.0% nice,  1.3% system,  0.0% interrupt, 94.5% idle
Mem: 351M Active, 1635M Inact, 103M Laundry, 553M Wired, 174M Buf, 1262M Free
Swap: 4034M Total, 1816K Used, 4032M Free

  PID USERNAME    THR PRI NICE   SIZE    RES STATE    C   TIME    WCPU COMMAND
...
13571 www           1  20    0    14M  4204K kqread   1   0:00   0.00% lighttpd
...
13575 www          21  52    0    65M    16M kqread   1   0:00   0.00% example
...

Affiliate links

Setting up a CppCMS C++ web application server requires a VPS with root access. Use our affiliate links to purchase a VPS or cloud server from third-party vendors. The affiliate commissions we earn facilitate, Free website access for everyone.

The affiliate links are listed in alphabetical order without any favor. Users are encouraged to refer to the Global Webdynamics LLP Terms of Service governing the Third-party vendors.